Control flow
Programs need to make decisions and repeat actions. Control flow is how you tell the program when to do what.
if / else — Making decisions
fn main() { let age: int = 18 if age >= 18 { print("You can vote") } else { print("You cannot vote yet") } }
The condition after if must be a boolean expression. If it's true, the first block runs. If it's false, the else block runs (if present).
Multiple conditions with else if
fn main() { let score: int = 85 if score >= 90 { print("Grade: A") } else if score >= 80 { print("Grade: B") } else if score >= 70 { print("Grade: C") } else { print("Grade: F") } }
Conditions are checked from top to bottom. The first one that is true runs, and the rest are skipped.
Combining conditions
fn main() { let age: int = 25 let has_license: bool = true if age >= 16 and has_license { print("You can drive") } let is_weekend: bool = true let is_holiday: bool = false if is_weekend or is_holiday { print("Day off!") } }
Short-circuit evaluation: and and or use short-circuit evaluation. With and, if the left side is false, the right side is never evaluated. With or, if the left side is true, the right side is skipped. This is safe for guarding expressions:
if i < arr.length() and arr[i] == target { // arr[i] is only accessed if i is in bounds }
while — Repeating actions
A while loop repeats a block of code as long as a condition is true:
fn main() { var count: int = 1 while count <= 5 { print(count) count += 1 } // Prints: 1, 2, 3, 4, 5 }
Be careful: if the condition never becomes false, the loop runs forever (an "infinite loop"). Always make sure something inside the loop changes the condition.
Common pattern: counting
fn main() { // Count from 0 to 9 var i: int = 0 while i < 10 { print(i) i += 1 } }
Common pattern: accumulating
fn main() { // Sum numbers from 1 to 100 var sum: int = 0 var i: int = 1 while i <= 100 { sum += i i += 1 } print(sum) // 5050 }
break and continue
break exits the loop immediately:
fn main() { var i: int = 0 while i < 100 { if i == 5 { break // stop when i reaches 5 } print(i) i += 1 } // Prints: 0, 1, 2, 3, 4 }
continue skips the rest of the current iteration and goes to the next one:
fn main() { var i: int = 0 while i < 10 { i += 1 if i % 2 == 0 { continue // skip even numbers } print(i) } // Prints: 1, 3, 5, 7, 9 }
Nesting
You can put if inside while, while inside if, and any other combination:
fn main() { var i: int = 1 while i <= 20 { if i % 3 == 0 and i % 5 == 0 { print("FizzBuzz") } else if i % 3 == 0 { print("Fizz") } else if i % 5 == 0 { print("Buzz") } else { print(i) } i += 1 } }
This is the classic "FizzBuzz" problem — a common programming exercise.
Exercises
- Write a program that checks if a number is positive, negative, or zero.
- Write a program that prints all even numbers from 2 to 20.
- Write a FizzBuzz program that goes from 1 to 50.
- Write a program that finds the first number greater than 1000 that is divisible by 7 and 13.
- Write a program that counts down from 10 to 1, then prints "Launch!".
Summary
if/else if/elsefor decisions.whilefor repetition.breakexits a loop.continueskips to the next iteration.- Conditions must be boolean expressions.
- Always ensure while loops will eventually end.
Next chapter: Functions →