Welcome to Swift (initial translation and annotations of Apple's official Swift document 28)---199~208 page (fourth--process Control)

Source: Internet
Author: User

Value Bindings (bound value)

In a switch case, you can bind one or more values to a temporary constant or variable in the case body, which becomes the bound value.

Code Sample:

Let Anotherpoint = (2, 0)

Switch Anotherpoint {

Case (Let x, 0):

println ("On the x-axis with an X value of \ (x)")

Case (0, let y):

println ("on the y-axis with a Y value of \ (y)")

Case let (x, y):

println ("somewhere else at (\ (x), \ (y))")

}

Prints "on the x-axis with an X value of 2"

This switch statement determines whether the point is on the x-axis, on the y-axis, or elsewhere.

The constants x and y are defined in the three case statements, and they temporarily receive one or two values from the tuple anotherpoint. In the first case, the "case (Let x, 0) matches any point where the Y value is 0, and assigns the X value of the point to the temporary constant x, the same, In the second case, the case (0, let y) matches any point with an X value of 0 and assigns the Y value of the point to the temporary constant Y.

Once the temporary constants are defined, they can be used in the case code block.

Note that this switch statement does not have a default case. The last cases, the scenario let (x, y) defines a tuple that matches all the conditions of the rest of the values, so the SWITHC statement does not end up with a default.

In the preceding code example, X and Y are constants defined by using the keyword let, because they do not need to be modified in the case code block, but they can also be defined as variables using the var keyword. If you do this, a temporary variable will The corresponding value is created and initialized. Any changes to this variable are confined to the code body of the case.

Where

A switch case can use where to check for additional conditions:

Let Yetanotherpoint = (1,-1)

Switch Yetanotherpoint {

Case let (x, y) where x = = y:

println ("(\ (x), \ (y)) is on the line x = = y")

Case let (x, y) where x = =-Y:

println ("(\ (x), \ (y)) is on the line x = = y")

Case let (x, y):

println ("(\ (x), \ (y)) is just some arbitrary point")

}

Prints "(1,-1) is on the line x = = y"

This switch statement determines whether a point is in a green line (where x = = y), whether it is in a Purple Line (where x = =-y), or something else.

Control Transfer statements (controlling jump statements)

Controlling jump statements alters the order in which code executes, and the code jumps from one place to another. There are four control jump statements in Swift:

Continue

Break

Fallthrough

Return

The control, break, and Fallthrough statements are described here, and return is described in the section of the function.

Continue

The continue statement tells the loop to stop the loop that is in progress and start the next loop, which has never left the loop.

Note the point:

In the For-condition-increment loop, the incermen calculation is still performed after the continue statement is called. The loop itself continues to execute, but the code in this loop is ignored.

The following code example demonstrates the removal of all vowels and empty glyd from a lowercase string to create an encrypted phrase:

Let Puzzleinput = "great minds think alike"

var puzzleoutput = ""

For character in Puzzleinput {

Switch Character {

Case "A", "E", "I", "O", "U", "":

Continue

Default:

Puzzleoutput + = character

}

}

println (Puzzleoutput)

Prints "Grtmndsthnklk"

In the above code, calling the Continue keyword, when matched to a vowel or a space, ends the loop and begins the next traversal loop. This ensures that the switch code block matches (or ignores) only vowels and spaces.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.