Learn about the use of break and continue statements in the Swift language _swift

Source: Internet
Author: User

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:

Break


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:

Continue


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

Related Article

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.