[Swift learning] Swift programming tour-control flow (9), swift tour

Source: Internet
Author: User
Tags case statement switch case

[Swift learning] Swift programming tour-control flow (9), swift tour

Swift provides a process control structure similar to the C language, including for and while loops that can execute tasks multiple times, and select to execute if and switch statements of different code branches based on specific conditions, there are also break and continue statements that redirect the control process to other codes.

In addition to the traditional for conditional increment loop in C, Swift also adds a for-in loop to traverse arrays, dictionaries, and ranges more easily ), string and other sequence types. Swift switch statements are more powerful than C statements. In C language, if a certain case accidentally misses the break, this case will "Drop" The Next case. Swift does not need to write the break, so this will not happen. Case can also match more types, including range matching, tuple, and specific type descriptions. The matching values in the switch case statement can be determined by temporary constants or variables in the case body, or the where clause can describe more complex matching conditions. For-In LoopIt can traverse numeric ranges, array elements, and character strings. Traversal range
for index in 1...5 {    print("\(index) times 5 is \(index * 5)")}// 1 times 5 is 5// 2 times 5 is 10// 3 times 5 is 15// 4 times 5 is 20// 5 times 5 is 25

 

Traverse Arrays

let names = ["Anna", "Alex", "Brian", "Jack"]for name in names {    print("Hello, \(name)!")}// Hello, Anna!// Hello, Alex!// Hello, Brian!// Hello, Jack!

 

Traverse dictionary

let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]for (animalName, legCount) in numberOfLegs {    print("\(animalName)s have \(legCount) legs")}// ants have 6 legs// cats have 4 legs// spiders have 8 legs

 

 

While Loop

While loop runs a series of statements until the condition turns to false. This type of loop is applicable when the number of iterations before the first iteration is unknown. Swift provides two while loops (while, repeat-while ).

 

While

A normal while loop checks whether the condition is true before iteration.

  

 

Repeat-while

The difference between it and while is that before judging the loop condition, the code block of a loop is executed first, and then the loop is repeated until the condition is false.

 

  Condition Statement

If statement

Like c, we will not introduce it here

 

Switch statement

The switch statement tries to match a value with several pattern statements. The switch statement executes the corresponding code based on the first matching mode. When there are many possible cases, the switch statement is usually used to replace the if statement.

The switch statement consists of multiple cases. To match certain more specific values, Swift provides several more complex match mode switch statements that must be complete. This means that each possible value must have at least one case block corresponding to it. If it is impossible to cover all values, you can use the default block to meet this requirement. The default block must be at the end of the switch statement. Unlike the switch statement in C and Objective-C, in Swift, when the code in the matching case block is executed, the program terminates the switch statement instead of executing the next case block. This means that you do not need to explicitly use the break statement in the case block. This makes the switch statement safer and easier to use, and avoids errors caused by forgetting to write the break statement.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.