Break statement
The break statement in the C programming language has the following two uses:
When a break statement is encountered in a loop, the loop terminates immediately, and program control continues after the Loop statement (Exit Loop).
It can be used to terminate the case in the switch statement (in the next section).
If you use a nested loop (that is, a loop in another loop), the break statement stops the execution of the most inner loop and begins executing the code block after the next line of code block.
Grammar
The syntax for the break statement in Swift programming is as follows:
Copy Code code as follows:
Flow chart
Instance
Copy Code code as follows:
Import Cocoa
var index = 10
do{
index = index + 1
if (index = = 15) {
Break
}
println ("Value of index is \ (index)")
}while Index < 20
When the above code is compiled and executed, it produces the following results:
The value of index is one value of index is + is value of the index is
14
Continue statement
The continue statement in the Swift programming language tells the loop to stop the statement that is executing and start again in the next iteration of the loop.
For a For loop, the continue statement makes the conditional test and increment portion of the loop execute. For a while and do ... while loop, the continue statement causes the program control to go to the conditional test.
Grammar
The syntax for the continue statement in Swift is as follows:
Copy Code code as follows:
Flow chart
Instance
Copy Code code as follows:
Import Cocoa
var index = 10
do{
index = index + 1
if (index = = 15) {
Continue
}
println ("Value of index is \ (index)")
}while Index < 20
When the above code is compiled and executed, it produces the following results:
The value of index is one value of index is value of the index is value of the index is *
16
value of index is value of the index is + value of index is
20