Swift Quick Start (iv) Process Control

Source: Internet
Author: User

Related articles
Swift QuickStart (i) First Swift program
Swift Quick Start (ii) Basic data types
Swift Quick Start (c) operator

1. Branching structure

Swift provides two common branch control structures: The IF statement and the Swich statement. In general, use the IF statement when the conditions are simple and may be small, and consider using the Swich statement when the conditions are more complex.

If condition statement

There are three forms of the IF statement
The first type:

ifexpression{ statements...}

The second type:

if  expression{ statements...}else{ statements...}

Third Kind

if  expression{ statements...}elseif expression{ statements...}else{ statements...}

A simple example:

var30if20{    print("年龄大于20岁")}else{    print("年龄小于等于20岁")}
Switch Branch Statement

The statement format for the switch syntax is:

switchexpression{case value1:statements...case value2 ,value3:statements...default:statements...}

To give a simple example:

varScore = +Switchscore{ Case ...:Print("Excellent") Case Bayi...:Print("Good") Case ...:Print("Medium") Case ...:Print("Pass") Case 0..< -:Print("Failed")default: Break}

With the switch statement, it is important to note that the switch statement is automatically terminated when any of Swift's case blocks are executed, so it is required that each case block contain at least one statement, or it will result in a compilation error.

2. Cyclic structure

The looping statement may include the following 4 parts:

    • Initialization statement (init_statements): Some initialization operations are completed before the loop begins.
    • Loop condition (test_expression): Determines whether the loop body is executed.
    • Loop Body (body_statements): The body of the loop.
    • Iteration Statement (iteration_statements): A variable that is typically used to control the loop condition so that the loop ends at the appropriate time.
While Loop statement

The syntax format for the WHILE Loop statement:

[init_statements]while test_expression{statements[iteration_statements]}

To give a simple example:

// 循环的初始化条件count0whilecount10{    print("count:\(count)")    // 迭代语句    count++}
Do and loop statements

The Do While loop executes the loop body before judging the loop condition, and if the loop condition is true, the next loop is executed, otherwise the loop is aborted. The syntax format for the Do While loop is as follows:

[init_statements]do{ statements[iteration_statements]}while test_expression

To give a simple example:

// 循环的初始化条件var1do{    print("count: \(count)")    // 循环迭代语句    while10
For Loop statement

The basic syntax format for A For loop is as follows:

[init_statements][test_expression][iteration_statements]{ statements}

To give a simple example:

forcount0count10count++{    print("count: \(count)")}
For-in Loop Statements

The for-in loop is designed to iterate over the contained elements, such as scopes, sequences, and collections. The basic syntax format for the for-in loop is as follows:

forin 范围|集合{ statements}

The following program uses the For-in loop to iterate through the range:

fornumberin1..3{    print(number)}
3. Control Loops

Swift provides break and continue to control loops, and return can end the loop by ending the entire method.

end a loop with a break
forvar010 ; i++{    print("i的值是:\(i)")    if3    {        // 执行该语句时将结束循环        break    }}
ignoring the remainder of the loop with continue
forvar03 ; i++{    print("i的值是\(i)")    if1    {        // 忽略本次循环的剩下语句        continue    }    print("continue后的输出语句")}

The result of the output is:
The value of I is 0
Output statements after the continue
The value of I is 1
The value of I is 2
Output statements after the continue

From the running result, when I equals 1 o'clock, the program does not output "Continue output statement", because the program executes to continue, ignoring the code after the continue statement in this loop.

End method with return
func test(){    forvar i = 0; i < 10 ; i++    {        print("i的值是:\(i)");        if i == 1        {            return;        }        print("return后的输出语句")    }}test()

The program above, when I equals 1 o'clock the program will completely end. Although return is not a keyword specifically for controlling loop statements, it is true that a loop can be ended with a return statement.

Swift Quick Start (iv) Process Control

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.