Branch statements
1. Conditional statements If-else, basic grammatical forms such as the following. The part of "[]" can be omitted.
If boolean-expression {statement1; } [Else if Boolean-expression {statement2;} ] [Else {statement3; }
Instance:
var number1 = 0;var number2 = 1; var max = 0; If Number1 >number2 {max = number1;} else if number1 < number2 {max = number2;} else {max = number1;} println ("Themaximum is \ (max)")
2. The multi-branch statement switch its syntax forms such as the following:
Switch some value to consider {case value 1:respond to value 1 case value 2, value 3:respond to value 2 or 3default: Otherwise, do something else}
Each case does not need to explicitly join the break, with at least one statement per case.
To be able to compare whatever type.
Example 1:
Let Somecharacter:character = "E" switch somecharacter {case "a", "E", "I", "O", "U": println ("\ (somecharacter) Isa vowel") Case "B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "s", "T", "V", "w", "X", "Y", "Z":p rintln ("\ (som Echaracter) is a consonant ") default:println (" \ (Somecharacter) are not a vowel or
Example 2:
Let count = 3_000 let countedthings = "stars inthe Milky a" varnaturalcount:stringswitch count {case 0:naturalcount = " No ' case 1...3:naturalcount = ' A few ' case 4...9:naturalcount = ' several ' case 10...99:naturalcount = ' tens of ' case 100...9 99:naturalcount = "Hundreds of" case 1000...999_999:naturalcount = "thousands of" Default:naturalcount = "millions and mill Ions of " } println (" There is \ (Naturalcount) \ (countedthings). ")
Looping statements
While statement
Do-while statements
For statement
In statement
While loop
While condition{statements}vari = Varr = 0 vars= 0 vart = 0while I < + {r= i/100 s= (i-r*)/t= I -R* 100-s* if (i = = R * r* r + s* s *s + t* T * t) {println ("i = \ (i)")}i++}do while loop do {statements} while Conditionvari = Varr = 0 vars= 0 vart = 0do {r= i/100 s= (i-r*)/t= i-r* 100-s* if (i = = R * r* R + s* s *s + t* T * t) {println ("i = \ (i)")} i++} while I < 1000
For loop
for initialization; Condition Increment {statements}vari= 8 Varr = 0 vars= 0forvarj = 0; J <=i; j++{r = J * J S=j * J * JPRINTLN ("integer: \ (j)"); println ("corresponding squared sum: \ (r)") println ("corresponding cubic and: \ (s)")}
For In loop
Used to traverse the collection.
1. Traversing range
Forindex in1 ... 5{println ("\ (index) times 5 is \ (Index * 5)")}
2. Ignore loop variables
Letbase = 3letpower = Varanswer = 1for_ in 1...power {answer *=base} println ("\ (base) to the power of \ (power) is \ (answer) " )
3. Iterating through an array
Letnames =["Anna", "Alex", "Brian", "Jack"] forname innames {println ("Hello, \ (name)!")}
4. Traversing a dictionary
Letnumberoflegs =["Spider": 8, "Ant": 6, "Cat": 4] for (Animalname, Legcount) innumberoflegs {println ("\ (animalname) S has \ (legcount) legs ")}
5. Traversing a string
Letnumberoflegs =["Spider": 8, "Ant": 6, "Cat": 4] for (Animalname, Legcount) innumberoflegs {println ("\ (animalname) S has \ (legcount) legs ")} letnumberoflegs =[" Spider ": 8," Ant ": 6," Cat ": 4] for (Animalname, Legcount) innumberofl EGS {println ("\ (animalname) s has \ (legcount) legs")}
1.12.3 Jump Statement
? Continue
? Break
? Fallthrough
? Return
1.continue instance:
for var i = 0; I < 100; i++ {if I% = = 0 {continue;} println ("i = \ (i)");}
2. Break instance:
for var i = 0; I < 10;i++ {if i = = 3 {break;} println ("i = \ (i)");} println ("Game over!");
3. Fallthrough Example:
Let Integertodescribe = 5 var description = ' thenumber \ (integertodescribe) is ' switchintegertodescribe {case 2, 3, 5, 7, One, all, 19:description + = "A prime number, and also" fallthroughdefault:description + = "an Integer."} println (description)
Swift Exchange Discussion Forum Forum: http://www.cocoagame.net
Welcome to the Swift Technology group: 362298485
Swift Control statements