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:
1 Let grade = "B" 2 switch grade {3 case "A": 4 println ("excellent grade") 5 case "B": 6 println ("Good grade") 7 Case "C": 8 P Rintln ("Medium Grade") 9 default:10 println ("Unknown Level") 11}
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
1 Let grade = ' B ' 2 switch grade {3 case ' A ': 4 Case "B": 5 println ("Good Grade") 6 Default:7 println ("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,
1 Let score = 2 Switch SCORE/10 {3 case ten, 9:4 println ("excellent") 5 case 8, 7, 6:6 println ("pass") 7 Default:8
PRINTLN ("fail") 9}10//Printed results are: excellent
Range Matching for 5.case
You can fill in a range as a match condition after a case
1 Let score = 2 Switch score {3 Case 90...100:4 println ("excellent") 5 case 60...89:6 println ("pass") 7 default:8< C13/>PRINTLN ("fail") 9}10//Printed results are: excellent
Attention:
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.
1 Let points = (1, 1) 2 switch point {3 case (0, 0): 4 println ("This point at Origin") 5 case (_, 0): 6 println ("This point is on the x-axis") 7 Case (0, _): 8 println ("This point on the y-axis") 9 case ( -2...2, -2...2): println ("This point in the rectangular box") Default:12 println (" This point in other locations ") 13}
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
1 Let points = (0) 2 Switch point {3 case (let x, 0): 4 println ("This point on the x axis, x value is \ (x)") 5 case (0, let y): 6 pri Ntln ("This point on the y-axis, y-value is \ (y)") 7 Case-let (x, y): 8 println ("The X value of this point is \ (x), Y value is \ (y)") 9}10 11//print: This point is on the x-axis, the X value is 10
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.
1 var point = ( -10) 2-Switch Point {3-case-let (x, y) where x = = Y:4 println ("This dot is on the Green Line") 5 case let (x, y) where x = =-y:6 println ("This point on the Purple Line") 7 Default:8 println ("This point is not on these 2 lines") 9}10//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
1 Let num = 2 var str = "\ (num) is a" 3 switch num {4 case 0...50:5 str + = "0~50 between" 6 Fallthrough 7 default: 8 str + = "integer" 9}10 println (str) 11//Print: 20 is an integer between 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 break 2 group:3 for _ in 1...2 {4 for item in 1...3 {5 println ("Do 1 push-ups") 6 if Item = = 2 {7 break group 8 } 9 }10 println ("Rest") 11}
The output is
Do 1 push-ups
Do 1 push-ups
code example:
iOS Development Swift Chapter-(vi) Process Control