iOS Development Swift Chapter-(vi) Process Control

Source: Internet
Author: User

iOS Development Swift Chapter-(vi) Process Control

I. Process Control in Swift

The process structure supported by Swift is as follows:

Loop structure: For, for-in, while, do-while

Select structure: If, switch

Note : These statements must be followed by curly braces {}, which are not required in the C language

description : In contrast to the C language, the usage is basically the same as: for, while, Do-while, if

So, just focus on for-in and switch to

II. Structure of For-in

Simple to use:

For-in and Range operators

For I in 1...3 {

println (i)

}

Assign values in order from the range to I, and execute 1 cycles per 1 values

The length of the range is the number of times the loop body executes

code example:

Tip : If you don't need to use a value in a range, you can use the underscore _ to ignore

For _ in 1...3 {

println ("*********")

}

code example:

  

Note :i is a constant and its value cannot be changed.

Third, the use of switch

1. Examples of use:

1Let grade ="B"2 SwitchGrade {3  Case "A":4println"Excellent grade")5  Case "B":6println"Good grade")7  Case "C":8println"Medium Grade")9 default:Tenprintln"Unknown level") One}

The differences between the 2.switch statements in Swift and C:

In C, if there is no break at the end of the case, then the next or default statement is executed

In Swift, there is no need to add a break after each case, and the switch statement is automatically exited by default after executing the code corresponding to the case

3.switch Point of attention

In swift, there must be a statement that can be executed after each case

1Let grade ="B "2 SwitchGrade {3  Case "A":4  Case "B":5println"Good grade")6 default:7println"Unknown level")8}

Description : The second line of code will error

Multi-condition matching for 4.case

1 case can be followed by a number of matching conditions, the conditions are separated by commas,

1Let score = the2 SwitchScore/Ten {3  Case Ten,9:4println"Excellent")5  Case 8,7,6:6println"Pass")7 default:8println"inferior lattice")9 }Ten //The printing result is: excellent

Range Matching for 5.case

You can fill in a range as a match condition after a case

1Let score = the2 SwitchScore {3  Case  -... -:4println"Excellent")5  Case  -... the:6println"Pass")7 default:8println"inferior lattice")9 }Ten //The printing result is: excellent

Note :

Switch to ensure that all possible cases are handled, or the compiler will directly error

Therefore, the default must be added here, or there will be some less processing situation

6.case Matching tuple

Case can also be used to match tuples. For example, determine if a point is in the blue rectangle in the right image.

1Let point = (1,1)2 SwitchPoint {3  Case(0,0) :4println"this point is on the Origin .")5  Case(_,0) :6println"this point is on the x-axis.")7  Case(0, _) :8println"this point is on the Y axis.")9  Case(-2...2, -2...2) :Tenprintln"this point is inside the rectangle") One default: Aprintln"this point in other locations") -}

  

The role of _ in line 5th (2 ways of understanding)

(1) can match any value

(2) ignore the corresponding position tuple element

Value binding of 7.case

At the same time as case matching, you can bind the value in switch to a specific constant or variable to use in the statement following the case

1Let point = (Ten,0)2 SwitchPoint {3  Case(Let X,0) :4println"this point is on the x axis and the x value is \ (x)")5  Case(0, let y):6println"this point is on the Y axis and the Y value is \ (y)")7  CaseLet (x, y):8println"The x value of this point is \ (x), and the Y value is \ (y)")9 }Ten  One //Print: This point is on the x-axis and the X-value is ten

8.where

The switch statement can use where to increase the condition of the judgment. Like judging whether a point is on the Green Line or the Purple Line of the right image.

1var point = (Ten, -Ten)2 SwitchPoint {3  CaseLet (x, y)wherex = =y:4println"this point is on the Green Line.")5  CaseLet (x, y)wherex = =-y:6println"this point is on the Purple Line.")7 default :8println"this point is not on these 2 lines.")9 }Ten //Print: This point is on the Purple Line.

  

The role of 9.fallthrough

After executing the current case, the case or default statement after Fallthrough is executed

1Let num = -2var str ="\ (num) is a"3 Switchnum {4  Case 0... - :5str + ="between the 0~50"6 Fallthrough7 default :8str + ="integer"9 }Ten println (str) One //Printed: 20 is an integer between the 0~50

Note : The case condition after fallthrough cannot define variables and constants

10. Tags

Use one of the 1 functions of the label: can be used to explicitly specify which loop to exit

1 //do 2 sets of push-ups, each group of 3, finish a group on a break2 Group:3  for_inch 1...2 {4      forIteminch 1...3 {5println"do 1 push-ups")6         ifitem = =2 {7              BreakGroup8         }9     }Tenprintln"take a break .") One}

The output is

Do 1 push-ups

Do 1 push-ups

code example:

  

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.