Swift control flow resembles Java and C# but adds powerful switch pattern matching and guard statements for early exits.
if, guard, and switch
let score = 85
if score >= 90 { print("A") }
else if score >= 80 { print("B") }
else { print("Review") }
switch score {
case 90...100: print("Excellent")
case 80..<90: print("Good")
default: print("Keep practicing")
}
Loops
for item in collection, while, and repeat-while cover iteration. Ranges: 1...10 (closed), 1..<10 (half-open).
Important interview questions and answers
- Q: guard vs if?
A:guardexits early when a condition fails—keeps the happy path unindented; common for optional unwrapping. - Q: Does switch fall through?
A: No implicit fall-through—each case must exit or usefallthroughexplicitly.
Self-check
- What range operator includes 10 as the upper bound?
- Does Swift switch require
breakby default?
Tip: guard keeps happy-path code unindented—like early returns in Kotlin and C#.
Interview prep
- guard vs if?
guard exits early when condition fails—keeps happy path flat.
- Switch fall-through?
No implicit fall-through—must use fallthrough explicitly.