Seventh chapter: Control transfer statements

Source: Internet
Author: User

Control Transfer Statements

The control transfer statement changes the order in which you execute the code, through which you can implement the code jump. Swift has four kinds of control transfer statements.
    • Continue
    • Break
    • Fallthrough
    • Return
we will discuss the continue, break, and Fallthrough statements below. The return statement will be discussed in the function section. Continuecontinue tells a loop to stop this loop iteration and start the next loop iteration again. It's like saying, "I'm done with this loop iteration," but I'm not going to leave the whole loop body.  Note: In a for-condition-increment loop body, the iteration increment will still be evaluated after the continue statement is called. The loop body continues to work as usual, just the execution code in the loop body is skipped. The following example removes the vowel and space characters from a lowercase string, creating a vague phrase:
Let Puzzleinput = "great minds think alike"var puzzleoutput = "" For character in puzzleinput {switch charact Er {case "a", "E", "I", "O", "U", "" ": Continue default: Puzzleoutput + = character}}println (Puzzleo Utput)//prints "Grtmndsthnklk"     

In the above code, as long as the match to the vowel letter or the space character, call the continue statement, so that the loop iteration end, the next iteration of the new loop. This behavior causes switch to match the vowel and space characters without processing, rather than having each matched character printed.

BreakThe break statement immediately ends the execution of the entire control flow. You can use the break statement when you want to end a switch code block or a loop body earlier. use break in the loop bodywhen a break is used in a loop body, the execution of the loop body is immediately interrupted and then jumps to the first line of code after the curly brace (}) that represents the end of the loop body. No more code for this loop iteration will be executed, and there will be no next iteration of the loop generation. Using break in a switch code blockwhen a break is used in a switch code block, execution of the switch code block is immediately interrupted and jumps to the first line of code after the curly brace (}) that represents the end of the switch code block.  This feature can be used to match or omit one or more branches. Because the swift language switch needs to include all branches and does not allow empty branches, it is sometimes necessary to deliberately match or omit a branch in order to make your intentions more apparent. Then when you want to ignore a branch, you can write a break statement within that branch. When that branch is matched, the break statement within the branch immediately ends the switch code block. Note : When a switch branch contains only comments, it is reported as a compile-time error. Comments are not code statements and do not allow the switch branch to achieve the ignored effect. You can always use break to ignore a branch. The following example uses switch to determine whether a character value represents one of the following four languages. For brevity, multiple values are included in the same branch case.
Let Numbersymbol:character ="three" //Simplified Chinese for the number 3Possibleintegervalue:int?SwitchNumbersymbol { Case "1","?","a","?": Possibleintegervalue=1 Case "2","?","two","?": Possibleintegervalue=2 Case "3","?","three","?": Possibleintegervalue=3 Case "4","?","Four","?": Possibleintegervalue=4default: Break}ifLet IntegerValue =Possibleintegervalue {println ("The integer value of \ (Numbersymbol) is \ (integervalue).")} Else{println ("An integer value of could not being found for \ (numbersymbol).")}//Prints "The integer value of three is 3."
This example checks whether Numbersymbol is 1 in Latin, Arabic, Chinese or Thai ... 4 One. If it is matched to, the switch branch statement sets an integer value to the int? type variable possibleintegervalue. when the switch code block finishes executing, the next code uses an optional binding to determine if ' possibleintegervalue ' has ever been set to a value. Because it is an optional type, ' Possibleintegervalue ' has an implicit initial value of nil, so only when Possibleintegervalue has been given a value in the first four branches of the switch code block, The optional bindings will be judged to be successful. in the example above, it is impractical to enumerate all of the possibilities of character, so use the default branch to include all occurrences where there are no matches to characters. Since this default branch does not need to perform any action, it only writes a break statement. Once it falls into the default branch, the break statement completes all code operations on that branch, and the code continues down to start executing the IF let statement. Fallthroughswitch in the swift language does not fall from the previous case branch into the next case branch. Instead, as soon as the first matching case branch completes the statement it needs to execute, the entire switch code block completes its execution. In contrast, the C language requires that you display the Insert break statement to the end of each switch branch to prevent automatic fall into the next case branch. This avoidance of the default fall into the next branch of the swift language means that its switch function is clearer and more predictable than the C language, and avoids the errors that can be caused by unintentionally executing multiple case branches.  If you do need a C-style fall-in (Fallthrough) feature, you can use the Fallthrough keyword in each case branch that requires that feature. The following example uses Fallthrough to create a descriptive statement of a number.
Let Integertodescribe =5var description="The number \ (integertodescribe) is"SwitchIntegertodescribe { Case 2,3,5,7, One, -, -, +: Description+="a prime number, and also"Fallthroughdefault: Description+="An integer."}println (description)//Prints "The number 5 is a prime number, and also an integer."

Note:The Fallthrough keyword does not check the next match condition that will fall into the executed case. Fallthrough simply allows code execution to continue to connect to the execution code in the next case, which is the same as the switch statement attribute in the C language standard.

Label StatementIn the swift language, you can nest loop bodies and switch code blocks in the loop body and switch block to create complex control flow structures. However, both the loop body and the switch code block can use the break statement to prematurely end the entire method body. Therefore, it is useful to indicate which loop body or switch block of code The break statement wants to terminate. Similarly, if you have many nested loop bodies, it can also be useful to indicate which loop body the continue statement wants to affect.  to do this, you can use tags to mark a loop body or switch block of code, and when you use break or continue, you can control that tag to represent the object's interruption or execution.  A tagged statement is generated by placing a label in front of the same line in the keyword of the statement, and with a colon followed by the label. The following is a while loop body syntax, the same rules apply to all the loop body and switch code block.
 while condition {    statements}

The following example calls the break and continue statements in a tagged while loop body, which is an adapted version of the Snake-ladder game in the above section. This time, the game adds an extra rule:-In order to win, you must just fall in the box 25.  If a roll of dice makes you move beyond box 25, you must roll the dice until the number of dice you throw is just so you can fall into grid 25.  The game board is the same as before:

Source:http://www.cocoachina.com/ios/20140611/8769.html

2015-03-19

21:57:54

Seventh chapter: Control transfer statements

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.