Sequence
Without anything special, a program runs its lines in order, top to bottom. Control flow is everything that changes that order.
Branches
A conditional runs a block only if a condition is true:
if temperature > 30:
print("hot")
elif temperature > 15:
print("mild")
else:
print("cold")
The conditions are checked in order, and only the first one that’s true runs. At 35 degrees, both > 30 and > 15 are true, but only “hot” prints.
Many languages also have switch or match for choosing between many cases. Rust’s match and Python’s match (3.10+) can also check the shape of a value, not just whether it’s equal.
Loops
A loop repeats a block.
- A
forloop repeats once for each item, or each number in a range. Use it when you know what you’re looping over. - A
whileloop repeats as long as a condition stays true. Use it when you don’t know in advance how many times, like “until the user types quit”.
Two keywords change a loop from the inside: break leaves the loop at once, and continue skips to the next round.
Example: FizzBuzz
Count from 1 to n, but say “Fizz” for multiples of 3, “Buzz” for multiples of 5, and “FizzBuzz” for multiples of both. It’s a classic first exercise because it needs exactly a loop and a chain of branches.
def fizzbuzz(n):
out = []
for i in range(1, n + 1): # loop: repeat for 1, 2, …, n
if i % 15 == 0: # branch: the first true condition wins
out.append("FizzBuzz")
elif i % 3 == 0:
out.append("Fizz")
elif i % 5 == 0:
out.append("Buzz")
else:
out.append(str(i))
return outfunction fizzbuzz(n) {
const out = [];
for (let i = 1; i <= n; i++) { // loop: repeat for 1, 2, …, n
if (i % 15 === 0) { // branch: the first true condition wins
out.push('FizzBuzz');
} else if (i % 3 === 0) {
out.push('Fizz');
} else if (i % 5 === 0) {
out.push('Buzz');
} else {
out.push(String(i));
}
}
return out;
}import "strconv"
func fizzbuzz(n int) []string {
out := []string{}
for i := 1; i <= n; i++ { // loop: repeat for 1, 2, …, n
switch { // branch: the first true case wins
case i%15 == 0:
out = append(out, "FizzBuzz")
case i%3 == 0:
out = append(out, "Fizz")
case i%5 == 0:
out = append(out, "Buzz")
default:
out = append(out, strconv.Itoa(i))
}
}
return out
}fn fizzbuzz(n: u32) -> Vec<String> {
let mut out = Vec::new();
for i in 1..=n { // loop: repeat for 1, 2, …, n
let word = match (i % 3, i % 5) { // branch: the first matching pattern wins
(0, 0) => "FizzBuzz".to_string(),
(0, _) => "Fizz".to_string(),
(_, 0) => "Buzz".to_string(),
_ => i.to_string(),
};
out.push(word);
}
out
}The check for 15 has to come first. Every multiple of 15 is also a multiple of 3, so if the 3 check came first, 15 would print “Fizz” and never reach “FizzBuzz”. Rust’s version avoids that ordering trap by matching on both remainders at once.
Common mistakes
- Off-by-one loops.
range(1, n)stops at n − 1. Decide whether the end is included, and check the first and last round. - Loops that never end. A
whileloop whose condition never becomes false runs forever. Make sure something in the loop moves toward the exit. - Wrong branch order. Put the most specific condition first, as with 15 before 3.
- Deep nesting. Five levels of
ifinsideifis hard to follow. Return early, or move a block into a function.
Check yourself
Check yourself
With the checks in the order shown, what does FizzBuzz print for 30?
30 is a multiple of 15, and that check comes first, so it prints FizzBuzz and skips the rest.
Check yourself
Inside a loop, what does continue do?
continue skips the rest of this round and moves on to the next one. break leaves the loop completely.
Sources
- Corrado Böhm and Giuseppe Jacopini, “Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules,” Communications of the ACM 9, no. 5 (1966): 366–371.
- Edsger W. Dijkstra, “Go To Statement Considered Harmful,” Communications of the ACM 11, no. 3 (1968): 147–148.
Finished this page?
Your list