Lesson 3 โข Beginner
Control Flow ๐
Make decisions with if/else, master Swift's powerful switch statement with pattern matching, and repeat actions with for-in, while, and repeat-while loops.
What You'll Learn in This Lesson
- โข Conditionals:
if,else if,else, and the ternary operator - โข Swift's
switchwith ranges, tuples, and pattern matching - โข Loops:
for-in,while,repeat-while - โข Loop control with
breakandcontinue - โข Ranges: closed (
1...5) and half-open (1..<5)
1๏ธโฃ Conditionals
Conditionals let your program make decisions. Swift's if statements don't require parentheses around the condition (unlike C/Java), but the braces are always required โ even for single-line bodies.
Try It: Conditionals
Use if/else, comparison operators, logical operators, and ternary
// Swift Conditionals โ if, else if, else, guard
console.log("=== if / else if / else ===");
let temperature = 28;
console.log("let temperature = " + temperature);
console.log();
if (temperature > 35) {
console.log("๐ฅ It's extremely hot! Stay hydrated.");
} else if (temperature > 25) {
console.log("โ๏ธ It's a warm day. Perfect for the beach!");
} else if (temperature > 15) {
console.log("๐ค๏ธ It's mild. Grab a light jacket.");
} else if (temperature > 5) {
console.log("๐งฅ It's cold. Wear
...2๏ธโฃ Switch Statements
Swift's switch is far more powerful than in most languages. It supports ranges (0...100), tuples, pattern matching, and where clauses. Unlike C, there's no implicit fallthrough โ each case automatically breaks.
Try It: Switch Statements
Pattern matching with ranges, tuples, and multiple values
// Swift's Powerful Switch Statement
console.log("=== Switch Statement ===");
console.log("Swift's switch is MUCH more powerful than other languages!");
console.log();
// Basic switch
let day = "Wednesday";
console.log("let day = \"" + day + "\"");
switch (day) {
case "Monday": case "Tuesday": case "Wednesday": case "Thursday": case "Friday":
console.log("๐
It's a weekday โ time to work!");
break;
case "Saturday": case "Sunday":
console.log("๐ It's the weekend โ time to relax!
...3๏ธโฃ Loops
Swift provides for-in for iterating over sequences, while for condition-based loops, and repeat-while (similar to do-while) which always executes at least once. Use stride(from:to:by:) for custom step values.
Try It: Loops
for-in, while, repeat-while, stride, break, and continue
// Swift Loops โ for-in, while, repeat-while
console.log("=== for-in Loop ===");
// Looping over a range
console.log("for i in 1...5 {");
for (let i = 1; i <= 5; i++) {
console.log(" " + i + " ร " + i + " = " + (i * i));
}
console.log();
// Looping over an array
console.log("for fruit in fruits {");
let fruits = ["Apple", "Banana", "Cherry"];
for (let fruit of fruits) {
console.log(" ๐ " + fruit);
}
console.log();
// Enumerated (index + value)
console.log("for (index, fruit) in fruits.
...โ ๏ธ Common Mistakes
default case or cover all enum values.== with optionals โ Comparing an optional directly works in Swift, but be careful: nil == nil is true.where clauses in switch/for to add conditions: for i in 1...100 where i % 2 == 0.๐ Quick Reference โ Control Flow
| Construct | Swift Syntax |
|---|---|
| if/else | if x > 5 { ... } else { ... } |
| switch | switch val { case 1: ... } |
| for-in range | for i in 1...10 { ... } |
| for-in array | for item in array { ... } |
| while | while x > 0 { ... } |
| repeat-while | repeat { ... } while x > 0 |
| stride | stride(from: 0, to: 10, by: 2) |
๐ Lesson Complete!
You've mastered conditionals, switch statements, and loops! Next, learn about functions and closures.
Sign up for free to track which lessons you've completed and get learning reminders.