All the news came together this morning to report that the Netherlands defeated Spain 5-1. I didn't think it was wrong. I suddenly thought that if the Chinese team vs the Dutch team would not lose much, I watched the live video of yesterday's competition. It was just like the news and reports. I was suddenly touched by changes everywhere on the court, you should also pay attention to those who buy the lottery. Don't listen too much to the predictions of some experts.
The following is a picture of the football lottery I bought (I think I bought it)
Continue to share the circular statements in the swift getting started article.
I. Cyclic 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; variables ++
Example
// ------ For usage: for var I = 0; I <5; I ++ {println ("I = \ (I )")} // ------- for two usage methods var j = 0for j = 0; j <5; j ++ {println ("I = \ (j )")}
For in usage
Description of the for variable or temporary variable in format: When the for in statement is executed, the corresponding values in the set are assigned to the variable or temporary variable in sequence.
Example
First usage of for in string Traversal
// ------------ First usage of for in string traversal var str = "ABC" // str is a string variable even if the character set is combined/* 1: str is a character set combination, temp is a temporary variable (not required) 2: when the program executes the for in statement, it will assign the characters in the character set to the Temporary Variable temp */for temp in str {println ("temp = \ (temp )")}
Running result
temp=Atemp=Btemp=C
For in 2nd usage traversal sequence:
First, let's talk about the concept sequence of integer in swift, which is represented by three vertices...
Var A = 1... 5 //... three vertices indicate the sequence from 1 to 5 (1, 2, 3, 4, 5 ).
Example
// ------------ The second method of for in traversing the sequence/* 1:1... 5 indicates the sequence from 1 to 5, that is, the 1-5 set 2: temp is the temporary variable 3: The for in statement is executed, and then the corresponding value of the 1-5 set, assign temporary variable temp */for temp in 1... 5 {println (temp )}
Running result
12345
While statement
Format while Boolean {} Description: The while statement is stopped only when the Boolean value after while is false. Otherwise, the while statement is always executed.
Import Foundationvar I = 0/* The while statement */while (I <3) {println ("I = \ (I)") appears only when I <3 is false )") I ++} running result I = 0i = 1i = 2
Do while statement
Format: do {} while Boolean value description: 1: Now execute do Statement 2: then execute the while statement
3: If the Boolean value after the while statement is false, stop the do while statement. Otherwise, the do while statement will be executed all the time.
Import Foundationvar I = 1/* 1: first execute Statement 2 in do {}, then value while Statement 3: when I <3, The do while statement stops */do {println ("I = \ (I)") I = I + 1} while (I <3) running result I = 5i = 4
Condition Statement
If the if Statement (as mentioned earlier) is not clear, go to Quick Start-basic type (3)
Switch statement
Format: switch (variable) {case variable value: Execution Method case variable value: Execution method default: Execution method} Description: 1: the switch Condition Statement must contain at least one case statement and one default statement. 2: the case must be followed by the execution method. 3: variable values can be multiple or one, and multiple variables are separated by commas (,). 4: variable value can be of any type
Example;
Var I = 1 switch (I) {case 0: // The method println ("I = \ (I)") case 1, 2 corresponding to a variable after case is executed when I = 0: // case is followed by two variables. Multiple variables are separated by commas. When I = 1 and 2, the corresponding method println ("I = \ (I)") under the case statement is executed )") default: // If I is not equal to 0, 1, 2, execute the corresponding method println ("default")} under the default statement to run the result I = 1
Switch sequence matching
// ------- The first usage range matches var I = 75 switch (I) {case 1... 50: // a sequence is followed by a case. The sequence is a set variable. When I is in the range of 1 to 50, the corresponding method println ("1... 50-> I = \ (I) ") case 50... 100: // a sequence is followed by a case. The sequence is a set variable. When I is in the range of 1 to 100, the corresponding method println ("50... 100-> I = \ (I) ") default: // If I is not equal to the range from 1 to 100, run the corresponding method println (" default ") under the default statement ")}
Running result
50... 100-> I = 75
Switch tuples matching
Import Foundation // ------- first usage tuples match let str = (1, 2) // str is the tuples variable switch (str) {case (0... 1, 0... 1): // If the str value range of the tuples (0 to 1, 0 to 1) println ("(0... 1, 0... 1) --> str = \ (str) ") case (1... 2, 1... 2): // If the str value range of the tuples (1 to 2, 1 to 2) println ("(1... 2, 1... 2) --> str = \ (str) ") default: println (" default ")}
Running result
(1...2,1...2)-->str=(1, 2)
If you are not sure about the swift language, you can add QQ 1436051108 to anyone interested in the World Cup.
In the subsequent articles, I wrote the swift language I learned to form a series. Because it is a new language, it is inevitable that there are deficiencies. Please give me your comments. You can also add QQ 1436051108 for discussion. If you have any questions, you can also send a message to me via QQ. I will reply to you immediately after seeing it.