Control Structures
Control structures: everything that decides between "do this" and "do that".
You can store numbers, text, and true-or-false values in variables — but so far, your programs run top-to-bottom, one line after the next. Like reading a shopping list out loud.
What if the program should greet teenagers differently from adults? What if it should process a thousand data points without you writing a thousand lines? Control structures solve both problems: conditionals let code choose a path, and loops let code repeat. By the end of this article, you will understand the three fundamental patterns — if/elif/else, for, and while — and know exactly when to use which one.
Branching — Making Decisions with If/Elif/Else
What is branching?
The analogy breaks at one point: a human bouncer uses judgment and gray areas ("looks old enough," "forgot their ID"). A program knows only True or False — the condition age >= 18 is either met or not, with no in-between.
Execution trace: age = 17
Code example: Age checker
Common misconception: "if checks all conditions"
For Loops — Repeating a Known Number of Times
What is a for loop?
The analogy breaks where a real carrier might skip a house or take a shortcut. A for loop visits every element by default — unless you explicitly use break to exit or continue to skip the current iteration.
Code examples: Lists and range()
Common misconception: "range(5) gives 1 to 5"
While Loops — Repeating Until Something Changes
What is a while loop?
In real life, you would eventually give up if someone never types the right password. A while loop never gives up on its own — it runs until the condition becomes False or you exit with break, no matter how long that takes.
Code examples: Counter and while True
Warning: Infinite Loops
Common misconception: "Infinite loops are always bugs"
For vs. While — Choosing the Right Tool
Known number of iterations. Iterates over collections or range(). Typical: traversing lists, fixed repetitions.
Unknown number of iterations. Checks condition before each pass. Typical: user input, waiting for events.
Rule of thumb: "Do I know how many times?" → for. "Do I know when to stop?" → while.
Deep Dive: Indentation Is Syntax, Not Decoration
Deep Dive: Choosing the Right Loop
Interactive: Which Control Structure Do I Need?
Answer the questions in the decision tree to find the right control structure for your problem. Notice how the questions use exactly the criteria from the previous sections: Known count? Condition? Multiple cases?
What should your code do?
Key Takeaways
Quiz: Control Structures
Comprehension Check
- In an if/elif/else chain, if multiple conditions could be True at the same time — are all matching blocks executed, or only one? Why?
- Why does range(5) produce exactly the numbers 0 through 4? Explain how start and stop values work and how this connects to zero-based indexing.
- What happens in a while loop if you never change the variable checked in the condition? What role can break play?