Answer
- if
- if-else
- if-else ladder
- switch
let isAlien: Bool = true
// if
if isAlien {
print("Alien")
}
// if - else
if isAlien {
print("Alien")
} else {
print("Probably Human")
}
// if - else ladder
if isAlien {
print("Alien")
} else if isAlien != true {
print("Probably Human")
} else {
print("What else?")
}
// switch
switch isAlien {
case true:
print("Alien")
case false:
print("Probably Human")
}
Answer
- Process of iterating through all elements of a collection in an efficient way. It is faster than general for loop.
let numbers: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in numbers {
print(number) //1, 2, 3, 4, 5, 6, 7, 8, 9
}
Answer
- While is
entry control
loop whereas do-while isexit control
loop entry control
loop versusexit control
loop
// Enter Loop
var i: Int = 0
while i < 5 {
print(i)
i += 1
}
// Exit Loop
var j: Int = 0
repeat {
print(i)
i += 1
} while i < 5
Answer
- Switch is one of the conditional statements. It is an alternate to general if-else ladder statement.
let input: Int = 10
switch input {
case 0:
print("Value falls under 0")
case 1...5:
print("Value falles between 1 to 5")
case 6,7,8,9:
print("Value falls between 6 to 9")
case 10:
print("Value is 10")
default:
print("Not matched anywhere")
}
Warning
- You don't need to use break but you can use break keyword when you don't want to execute code.
Professional Tips
- Use switch when handling network status code.
Answer
- Default case is necessary in switch case, otherwise, you will see
Switch must be exhasutive
message.
Answer
- Fall-through executes the next followed case irrespective of case matching. Place dependent case below the fall-through case
let input: Int = 15
switch input {
case 0:
print("Value falls under 0")
case 11...15:
print("Value falls between 11 to 15")
fallthrough
case 9:
#warning("This line will be executed when value is 11...15!!!")
print("Value is 9 and this will be executed when value is between 11...15")
case 10:
print("Value is 10")
default:
break
}
Section 3, Conditional Statement
Section 10, static type vs dynamic type
Section 15, higher order function