Begin to share the looping statements in the swift introductory chapter.
One: Circular statements
1:for usage 2:for in usage 3:while usage 4:do while usage
For usage
General format
Common formats: (for many formats) for variables; variables < a value; variable + +
Example
------for a usage for var i = 0; I<5 i++ { println ("i=\ (i)") }//-------for two uses var j = 0for j = 0; j<5; j + + { println ("I=\ (j)") }
Usage of for in
The format for variable or temporary variable in the collection description: When the For statement is executed, the corresponding value in the collection is assigned to the variable or temporary variable in turn
Example
The first usage string traversal for
------------the first usage string of for in is to traverse var str = "ABC"//str is a string variable even if the character set/*1:STR is a character set, temp is a temporary variable (no need to define) 2: When the program executes the for-in statement Assigned the characters in the character set to the temporary variable temp*/for temp in str { println ("temp=\ (temp)")}
Run results
Temp=atemp=btemp=c
The 2nd usage of for in iterates over the sequence:
Let's start with a concept sequence in Swift for shaping ... Three points to indicate
var A = 1...5//... Three dots represent a sequence of 1 to 5
Example
The second usage of------------for in traversal sequence/*1:1 ... 5 represents a sequence of 1 to 5, that is, the set of 1-5 2:temp is a temporary variable 3: Executes a For in statement that is the corresponding value in the set of 1-5, assigned to the temporary variable temp*/for temp in 1...5{ println ( Temp)}
Run results
12345
While statement
Format while Boolean {} Description: Stop the While statement only if the Boolean value after the while is false, or the while statement is always executed
Import Foundationvar i = 0/* jumps out of the while statement */while (i<3) { println ("i=\ (i)") i++} Run results only if i<3 is false i=0i= 1i=2
Do While statement
3: If the Boolean value behind the while statement stops the do while statement, the Do and do statement is executed
Import Foundationvar i = 1/*1: Executes the DO {} inside Statement 2: Then the value while Statement 3: When the i< 3, the Do While statement stops */do {println ("i=\ (i)") I=i+1}while (i <3) Run result i=5i=4
Conditional statements
If statement (already mentioned earlier) if not clear, please go to swift Introductory article-basic type (3)
Switch statement
Format: switch (variable) {case variable Value: execution method Case variable Value: execution method Default: Execute Method} Description: 1:switch conditional statement with at least one case statement and default Statements must be followed by the execution Method 3: Variable values can be multiple or one, multiple variables separated by commas 4: Variable value can be any type of 2:case
Example
var i = 1switch (i) {case 0: //case followed by a variable when i=0 is executing the corresponding method under the case statement println ("i=\ (i)") Case 1, 2:
//case followed by two variables, multiple variables separated by commas when I=1 and 2 execute the corresponding method under the case statement println ("i=\ (i)") Default: //If I is not equal to 0,1,2, Execute the corresponding method under the default statement println ("Default")} to run the result I=1
Switch sequence Matching
-------The first usage range matches var i = 75switch (i) {case 1...50: //case followed by a sequence, the sequence is a set variable when I is executing in the range 1 to 50 The case statement below corresponds to the method println ("1...50-> i=\ (i)") Case 50...100: //case followed by a sequence, the sequence is a set variable When I executes a case statement under the range 1 to 100, the corresponding method println ("50...100-> i=\ (i)") is default: //If I is not equal to 1 to 100 range, execute The default statement below corresponds to the method println ("Default")}
Run results
50...100-> i=75
Switch tuple matching
Import foundation//-------The first usage tuple match let str = (UP)//STR is a tuple variable switch (str) {case (0...1,0...1)://If the range of tuple variable str (0 to 1, 0 to 1) println ("(0...1,0...1)-->str=\ (str)") Case (1...2,1...2): If the range of the tuple variable str (1 to 2, 1 to 2) println ("( 1...2,1...2)-->str=\ (str) ") default: println (" Default ")}
Run results
(1...2,1...2)-->str= (1, 2)
If you are afraid of the swift language, friends who are interested in the World Cup can also add me QQ 1436051108, we can pull together the World Cup
later in the article, I went back to learn from the Swift language of knowledge written to form a series. Since it is a new language, it is inevitable that there are deficiencies, and you are welcome to advise me. can also add my QQ 14,360,511,081 discussion, if you have any problem, you can also directly in QQ message sent to me, I saw the first time after you reply
Swift Entry-Looping statements