In Swift, control flow refers to the flow of execution through a program based on conditions, loops, and other control structures. Here’s a summary of the control flow constructs in Swift
1. Conditional Statements:
if statement: Executes a block of code only if a condition is true.
if-else statement: Executes one block of code if a condition is true and another block if it’s false.
if-else statement: Allows multiple conditions to be checked sequentially, executing the block associated with the first true condition.
2. Switch Statement:
- switch statement: Evaluates an expression against different cases and executes the block associated with the matched case. It supports a variety of patterns and can include default cases and fallthrough behavior.
3. Loops:
for-in loop: Iterates over a sequence, such as an array or a range, executing a block of code for each element.
while loop: Executes a block of code repeatedly as long as a condition is true.
repeat-while loop: Similar to the while loop, but it guarantees the block of code is executed at least once, even if the condition is initially false.
4. Control Transfer:
break statement: Terminates the execution of a loop or switch statement and transfers control to the next line of code.
continue statement: Skips the remaining code in a loop iteration and moves to the next iteration.
fallthrough statement: Used in a switch statement to execute the code in the next case block, even if it doesn’t match the condition.
5. Early Exit:
- guard statement: Provides an early exit from a block of code if a condition is not met. It is often used to validate input or perform optional binding.
6. Defer Statement:
- defer statement: Executes a block of code just before the current scope is exited, regardless of how the scope is exited. It is commonly used for cleanup tasks and ensures that necessary actions are performed before leaving a scope.
7. Checking API Availability:
- if #available statement: Allows conditional execution of code based on the availability of specific APIs on different versions of the operating system or frameworks. It ensures that code is only executed when the required APIs are available.
1. What value does the function calculate() return?
func calculate() -> Int {
var result = 0
for i in 1...5 {
if i % 2 == 0 {
result += i
} else {
result -= i
}
}
return result
}
a) 3
b) -3
c) 0
d) 6
Ans: b) -3
The for loop iterates over the range 1…5. When i is even, it adds to the result; when i is odd, it subtracts from the result. The final value of result is -3.
2. Which of the following control flow statements in Swift can be used to exit the current iteration of a loop?
a) break
b) continue
c) return
d) All of the above
Ans: d) All of the above.
3. What is the output?
func printNames() {
let names = ["John", "Amy", "Emily", "Michael"]
for name in names {
if name == "Amy" {
continue
}
print(name)
if name == "Emily" {
break
}
}
}
a) John
b) John Emily
c) John Amy Emily
d) John Amy Emily Michael
Ans: b) John Emily
The for-in loop iterates over the names array. When name is “Amy”, the continue statement skips the remaining code and moves to the next iteration. So, “Amy” is not printed. When name is “Emily”, the break statement terminates the loop. Therefore, only “John” and “Emily” are printed.
4. What is the primary difference between the if
and guard
statements in Swift?
a) if
for early exit, while the guard
is for condition checking.
b) if
for multiple conditions checking, while the guard
for optional unwrapping.
c) if
must have an else
, while guard
need not.
d) if
can be used in any scope, while the guard
can only be used in functions or methods.
Ans: a) if
for early exit, while the guard
is for condition checking.
5. What is the output?
func calc() {
defer {
print("Step 3")
}
print("Step 1")
defer {
print("Step 4")
}
print("Step 2")
}
a) Step 1 Step 2 Step 3 Step 4
b) Step 1 Step 2 Step 4 Step 3
c) Step 2 Step 1 Step 3 Step 4
d) Step 2 Step 1 Step 4 Step 3
Ans: b) Step 1 Step 2 Step 4 Step 3
The defer statements are executed in the reverse order they are written, but after the current scope is exited. The defer block acts like a stack.
6. What is the output?
func calc() -> Int {
var x = 10
repeat {
x += 5
} while x < 30
return x
}
a) 10
b) 15
c) 25
d) 30
Ans: d) 30
The repeat-while loop increments x by 5 in each iteration. It continues until x becomes 30 or more.
7. What is the output when executed on iOS 16?
func checkOS() {
let condition = false
if #available(iOS 14, *) {
print("Running on iOS 14 or later")
} else {
print("Running on earlier iOS version")
}
}
a) Running on iOS 14 or later
b) Running on earlier iOS version
Ans: a) Running on iOS 14 or later
8. What is the output?
func printChars() -> String {
var result = ""
for i in 1...3 {
switch i {
case 1, 3:
result += "A"
case 2:
result += "B"
default:
result += "C"
}
}
return result
}
a) ABC
b) AB
c) ABA
Ans: c) ABA
9. What is the output?
func example() {
var x = 10
defer {
x += 5
}
if x > 5 {
print(x)
return
}
print(x - 5)
}
a) 10
b) 15
c) 5
Ans: a) 10
Here the function returns before any mutation to value X hence the print inside if statement prints 10
Visit https://quizzesforyou.com for more such quizzes
References: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow
Top comments (0)