Break
The break statement immediately ends the execution of the entire process control. The break statement can help you to end the loop or switch execution prematurely in a switch statement or in a loop statement.
Break in a loop Statement ( broke in a looping statement)
When you use break in a loop statement, the execution of the loop ends immediately, and jumps to the first line of code after the loop body. The loop will no longer traverse execution.
Break in a switch Statement (Switch statement break)
When using Break,break in a switch statement, the first line of code is immediately followed by a switch and jumps to the switch statement brace (}).
This method can be used to ignore one or more cases. Since the switch statement executes, there can be no empty case, so sometimes a case needs to be ignored. You can omit this part from the break statement as the entire execution of the case; Once the switch is matched to this case, the break statement ends the switch execution immediately.
Note the point:
A switch case if there is only a comment, the compiler error is reported. The comment is not a statement, nor does it let the case be skipped, and the break statement is necessary if you want to ignore it.
Take a look at the following code example:
Let Numbersymbol:character = "three"//Simplified Chinese for the number 3
var possibleintegervalue:int?
Switch Numbersymbol {
Case "1", "?", "One", "?": "
Possibleintegervalue = 1
Case "2", "?", "Two", "?":
Possibleintegervalue = 2
Case "3", "?", "Three", "?":
possibleintegervalue = 3
Case "4", "?", "Four", "?":
Possibleintegervalue = 4
Default
Break
}
If let IntegerValue = possibleintegervalue {
println ("the integer value of \ (Numbersymbol) is \ (IntegerValue).")
} else {
println ("An integer value could not being found for \ (Numbersymbol).")
}
Prints "The integer value of three is 3."
In this code, check Numbersymbol, whether it is Latin, Arabic, Chinese, or a number from 1 to 4 in the Thai four languages. If the match is reached, the switch's case statement sets an int? optional variable of type Possibleintegervalue to handle the corresponding value.
After the switch statement is executed, the code uses an optional binding to determine whether the value is found. The variable possibleintegervalue has an implicitly initialized nil value, so if an optional binding can determine success, possibleintegervalue changes The amount of four case in switch must set an actual value.
In the above code example, Luo lists all possible characters that are not practical. Therefore, the case statement of Deafult handles all mismatches, and the default cases do nothing but use the break statement to end switch execution.
Fallthrough ( landing )
In Swift, the switch statement does not process the following case from one case to the bottom case. Instead, the entire switch statement ends after the first matching case is completed, and in the C switch, Prevent this drop execution by inserting a break in each case. Swift avoids this default landing execution to be more accurate and predictable, which avoids the wrong execution of multiple case.
If you really need an execution case like C that also landed sequentially, you can use the keyword Fallthrough.
The following code example uses the keyword Fallthrough to create a text description of a number:
Let Integertodescribe = 5
var description = "The number \ (Integertodescribe) is"
Switch Integertodescribe {
Case 2, 3, 5, 7, 11, 13, 17, 19:
description + = "A prime number, and also"
Fallthrough
Default
description + = "an integer."
}
println (description)
Prints "The number 5 is a prime number, and also an integer."
Note the point:
The keyword Fallthrough does not detect the switch's case condition, it only lands on execution. As in C, after executing from one case code, execute the next case code block directly.