1 counting loops within a specific range, structure for
for loopVar in startNumber ... endNumber
The keyword in is followed by a starting number, three periods, and an ending number, for example:
var loopCount: Int = 0
for loopCount in 1 ... 10 {
print ("# \ (loopCount)")
}
2 variant syntax
for loopCount in 1 .. <10 {
// print
}
3 old fashioned for loop
for loopCount = 0; loopCount <10; loopCount ++) {
// do something
}
Get started with playground
Process control is decision
if (true / false) {
// do sth
} else
{
// do other thing
}
swift comparison operator
==,! =,>, <,> =, <=
if true / false {
// do sth1
} else if true / false {
// do sth2
} else if true / false {
// do sth3
} else {
// do oter thing
}
switch case default is similar to C, but it is not limited to integer numbers and enums. It can also be used for String. In addition, switch-case does not require break in Swift
for three in threeArray {
switch three {
case "Cak":
print ("Furniture")
case "Pecan":
print ("Pie")
case "Maple":
print ("Syrup")
default:
print ("Wood")
}
}
while loop
while someCondition {
// do sth
}
repeat {
// do sth
} while someCondition
Break out of the loop using break
// Todo. Why is there no continue in swift, if it just exits the current loop?
"Swift Basic Tutorial 2nd" Loops and Control Structures