Lesson 6 of 6

Control Flow & Loops

So far, every program you've written has run top to bottom, once, every line every time. Real programs need to make decisions ("only send the email if the order succeeded") and repeat work ("do this for every item in the cart"). That's control flow — the tools that let a program branch and repeat instead of just marching straight through.

Making decisions: if / elif / else

age = 20

if age < 13:
    print("child")
elif age < 20:
    print("teenager")
else:
    print("adult")

Python checks each condition top to bottom and runs the first block whose condition is true, skipping the rest. elif is short for "else if" — you can chain as many as you need, and the final else is optional, catching anything not matched above.

💡 The Bottom Line

if/elif/else lets your program choose a path. for loops let it repeat an action once per item in a collection. while loops let it repeat until a condition changes. Together with variables, functions, and data structures, this is the last piece of the foundation — from here, every program is just these pieces combined.

Repeating over a collection: for loops

A for loop in Python reads almost like English: "for each item in this collection, do the following":

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit.upper())

This works on any iterable — lists, strings, dictionaries, and a handy built-in called range() for counting:

for i in range(5):
    print(i)         # prints 0, 1, 2, 3, 4

🙋 There Are No Dumb Questions

Q: Why does range(5) print 0 through 4, and not 1 through 5?
A: Python counts from zero everywhere — the same reason list[0] is the first item in a list. range(5) means "five numbers starting at zero," which lands on 0, 1, 2, 3, 4. If you specifically want 1 through 5, write range(1, 6) — the second number is always the stopping point, not included.

Repeating until something changes: while loops

Use while when you don't know in advance how many times you'll repeat — you just know the condition that should keep it going:

count = 0
while count < 3:
    print("counting:", count)
    count += 1

Every while loop needs something inside it that eventually makes the condition false — here, count += 1. Forget that step and you've written an infinite loop, one of the most common early bugs.

⚠️ Watch Out

Before running a while loop, double-check that something inside it actually moves the condition toward becoming false. A loop like while count < 3: with no count += 1 anywhere inside will run forever and freeze your program — you'll need to manually interrupt it (Ctrl+C in a terminal).

Skipping and stopping early: break and continue

Two keywords let you interrupt a loop's normal rhythm:

for number in range(10):
    if number == 5:
        break          # stop the loop entirely
    print(number)

for number in range(6):
    if number % 2 == 0:
        continue       # skip just this iteration, keep looping
    print(number)       # prints only odd numbers: 1, 3, 5

break exits the loop immediately, no matter how many iterations were left. continue skips only the rest of the current iteration and moves on to the next one.

🔧 Try It Yourself

Write a loop that goes through the numbers 1 to 20 and prints "Fizz" for multiples of 3, "Buzz" for multiples of 5, "FizzBuzz" for multiples of both, and the number itself otherwise — a classic first exercise that exercises everything from this lesson:

for n in range(1, 21):
    if n % 15 == 0:
        print("FizzBuzz")
    elif n % 3 == 0:
        print("Fizz")
    elif n % 5 == 0:
        print("Buzz")
    else:
        print(n)

Run it and check the output against 15, 30 — both should print "FizzBuzz," not just "Fizz" or "Buzz," since they're multiples of both 3 and 5.

✅ Quick Check

Inside a loop, you want to skip the current item and move straight to the next one, without ending the loop entirely. Which keyword do you use?


That's the foundation — variables, types, data structures, functions, and control flow, the same handful of building blocks behind every Python program you'll ever read or write. Time for the recap.

← Back to
Next →