Swift Learning Swift Programming Journey---Control Flow (ix)

Source: Internet
Author: User
Tags case statement switch case

Swift provides a C-like process control structure that includes a for and while loops that can perform tasks multiple times, select if and switch statements that execute different branches of code based on specific criteria, as well as break and continue statements that control the flow to other code.

In addition to the traditional for conditional increment loop in C, Swift also adds a for-in loop to make it easier to iterate over the group (array), dictionary (dictionary), Range (range), string, and other sequence types. Swift's switch statement is more powerful than the C language. In the C language, if a case accidentally misses a break, it "falls into" the next case,swift without writing a break, so this "fall in" situation does not occur. The case can also match more type patterns, including scope (range) matching, tuple (tuple), and a specific type of description. The matching value in a switch case statement can be determined by a temporary constant or variable inside the case body, or a more complex match condition can be described by the WHERE clause. for-in CycleIt can traverse the numeric range, array element, character of the string. Traversal range
 for inch 1... 5 {    print ("\ (index) times 5 is \ (Index * 5)")}//  1 Times 5 is 5//  2 times 5 is ten//  3 times 5is//  4 Tim Es 5is +//  5 times 5 is

Iterating through an array

Let names = ["Anna","Alex","Brian","Jack"] forNameinchNames {print ("Hello, \ (name)!")}//Hello, anna!.//Hello, alex!.//Hello, brian!.//Hello, jack!.

Traverse Dictionary

Let Numberoflegs = ["Spider":8,"Ant":6,"Cat":4] for(Animalname, Legcount)inchNumberoflegs {print ("\ (animalname) s has \ (legcount) legs")}//ants have 6 legs//Cats have 4 legs//spiders have 8 legs

While loop

The while loop runs a series of statements until the condition becomes false. This type of loop is suitable for use in cases where the number of iterations before the first iteration is unknown. Swift provides 2 while loops (While,repeat-while).

While

Normal while loop, which checks if the condition is true before iterating

  

Repeat-while

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

  Conditional statements

If statement

Same as C, no introduction here.

Switch statement

The switch statement attempts to match a value to a number of patterns (pattern). The switch statement executes the corresponding code according to the first successful pattern. The If statement is usually replaced with a switch statement when it is possible to have more cases.

Switch statements consist of multiple case. To match some of the more specific values, Swift provides several more complex matching patterns that the switch statement must be complete. This means that every possible value must have at least one case block corresponding to it. In cases where it is not possible to cover all values, you can use the default block to satisfy the requirement, which must be on the last side of the switch statement. Unlike the switch statements in C and Objective-c, in Swift, when the code in the matching case block finishes executing, the program terminates the switch statement without continuing the next case block. This means that you do not need to explicitly use the break statement in a case block. This makes the switch statement more secure, easier to use, and avoids errors caused by forgetting to write a break statement.

Swift Learning Swift Programming Journey---Control Flow (ix)

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.