To process the conditional logic, use the IF and switch to process the loop logic, using for-in, for, while, and Do-while. Brackets with conditions or loops can be added without adding. The curly brackets that handle the logical body must be added.
let individualScores = [75431038712]var0forin individualScores { if50 { 3 else { 1 }}println(teamScore)
Attention
The above code, Temascore oneself occupies a line of code. This is a simple way to see the value of a variable in playground.
In the IF statement, the condition must be a Boolean expression-meaning that it is wrong to be written like if score {...} because it cannot be implicitly converted to a value of 0.
You can use if and let together to handle potentially ambiguous values. These values are represented as optional values. An optional value either contains a value or nil, indicating that the value is ambiguous. It is optional to mark this value with a question mark (?) after the type of the value.
varString"Hello"println(optionalString == nil)varString"John Appleseed"var"Hello!"iflet name = optionalName { "Hello, \(name)"}
Give it a try.
Change Optionalname to nil. What is the value of greeting? and one more else.
Statement, if Optionalname is nil, set greeting to a different value.
If the optional value is nil, the condition is false and the code in the curly brackets is skipped. Otherwise, the optional value is turned on (unwrapped, Bo main note: Here the unwrapped really do not know how to translate better, the original meaning of the word for open, expand, direct translation come to feel strange, temporarily called open bar, seemingly some articles translated as parsing) and assigned to let behind the constant, This constant will allow the opening value to be used in the code block.
Switch supports any kind of data and a large number of comparison operations--it is not limited to integer types and test equality.
LetVegetable ="Red pepper"SwitchVegetable { Case "Celery": LetVegetablecomment ="Add some raisins and make ants on a log." Case "Cucumber","Watercress": LetVegetablecomment ="That's would make a good tea sandwich." Case LetXwhereX.hassuffix ("Pepper"): LetVegetablecomment ="is it a spicy \ (x)?"default: LetVegetablecomment ="Everything tastes good in soup."}
Give it a try.
Try to get rid of the default part. See what's wrong with it?
Take a look at how let is used to assign the value of a matching pattern part to a constant.
When the code that matches the switch case finishes executing, the program jumps out of the switch statement. The next case is not executed, so there is no need to jump out of switch at the end of each case code.
Use for-in to traverse the dictionary by providing a set of names for each key-value pair. Dictionaries are unordered collections, so their keys and values are traversed in alphabetical order.
Let interestingnumbers = ["Prime": [2,3,5,7, One, -],"Fibonacci": [1,1,2,3,5,8],"Square": [1,4,9, -, -],]var largest =0 for(Kind, numbers)inchinterestingnumbers { for Number inchNumbers {if Number> Largest {largest = Number}}}println (largest)
Give it a try.
Add a variable to track which number is the largest and what is the largest number?
Use while to repeatedly execute a piece of code until the condition changes. The condition of the loop can also be put to the last, to ensure that the loop executes at least once.
var2while100 { 2}println(n)var2do { 2while100println(m)
You can maintain an index in the loop--or use it. < Create a set of indexes or write a display of initialization, condition, and progression. Both of these loops are the same:
0fori0..<4{ firstForLoop += i}0fori0i4; ++i{ secondForLoop += i}println(secondForLoop)
Use.. < created index ignores maximum value, with ... The index created contains the maximum value.
Swift Journey (ii) control flow