programmingconcepts

§ 1.2 · Foundations

Control flow

The order a program runs its statements in, and the branches and loops that change that order.

Control flow is the order in which a program runs its instructions. By default it goes top to bottom, one line after another. Conditionals, like if and else, choose between branches; loops, like for and while, repeat a block; and break, continue and return jump out early. Every program, however large, is built from these few pieces.

Branches
if, else if, else, switch, match
Loops
for, while
Jumps
break, continue, return
Default order
Top to bottom
Requires
Variables and types

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 for loop repeats once for each item, or each number in a range. Use it when you know what you’re looping over.
  • A while loop 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 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

  1. Off-by-one loops. range(1, n) stops at n − 1. Decide whether the end is included, and check the first and last round.
  2. Loops that never end. A while loop whose condition never becomes false runs forever. Make sure something in the loop moves toward the exit.
  3. Wrong branch order. Put the most specific condition first, as with 15 before 3.
  4. Deep nesting. Five levels of if inside if is 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?

Check yourself

Inside a loop, what does continue do?

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.

Newsletter

One new concept each week, by email.

Signup opens soon.