Loops and Conditionals: Teaching Kids the Logic Behind “Repeat” and “If”
A program that only runs top to bottom, once, can only do so much. Loops let a child’s code repeat without retyping it. Conditionals let it choose between paths. Together they turn a short script into something that can react.
Why this feels harder than variables
A variable is a single idea: a name connected to a value. Loops and conditionals ask a child to imagine the computer skipping lines, repeating lines, or never reaching some lines at all. That is a real shift from reading code the way we read a sentence, in order, once.
Give that shift a name instead of rushing past it. Tell the learner directly: “Until now, the computer read every line once, in order. Starting today, some lines can run more than once, and some lines might not run at all.” Naming the change reduces confusion later.
Start with conditionals: teaching a program to choose
Introduce conditionals before loops. A conditional is a single decision, which is easier to trace by hand than a repeating one. Use a check a child already reasons about, such as whether a score is high enough to pass a level.
Walk through an if, then an if/else, then an if/elif/else, adding one branch at a time. At each step, ask the learner to pick a test value and predict which branch runs before you run the program.
- if: one path runs only when a condition is true
- if/else: exactly one of two paths always runs
- if/elif/else: the first true condition wins, and the rest are skipped
Move to loops: teaching a program to repeat
Once a single decision feels comfortable, introduce repetition with a for loop over a small, countable range, such as printing a countdown from five. A for loop is easier to reason about first because it has a visible, fixed number of repeats.
Save while loops for once for loops feel automatic. A while loop repeats based on a condition rather than a count, which means a learner must also understand why the loop eventually has to stop.
- for: repeat a known number of times
- while: repeat until a condition becomes false
- Ask: what has to change inside the loop for it to end?
Three small projects that combine the ideas
A guess-the-number game asks a player to keep guessing until they find a hidden value, combining a while loop with a conditional that checks each guess. A traffic-light simulator cycles through red, yellow, and green using a loop, then uses conditionals to print what a driver should do at each colour. A tally counter loops through a list of weather reports and uses a conditional to count how many days were rainy.
In each project, ask the learner to identify, out loud, which part repeats and which part decides. Separating those two questions keeps the two concepts distinct instead of blurring together.
- Guess-the-number: which line repeats, and what makes it stop?
- Traffic light: what decides which message gets printed?
- Weather tally: what is being counted, and where does that count live?
Correct the misconceptions early
Three mistakes appear consistently. First, learners often expect a for loop over a range of five to print the number five, rather than repeat five times starting from zero—walk through the actual sequence of values by hand. Second, learners often write an infinite while loop because nothing inside the loop changes the condition being checked. Third, learners often stack several if statements when they mean elif, which can cause more than one branch to run.
Each of these is best caught by tracing the program by hand, line by line, before running it. A prediction that turns out wrong is more informative than a program that happens to work.
Finish with an explanation, not just working code
Ask the learner to choose one project and explain, without reading the code aloud, what repeats, what gets decided, and what makes the repetition stop. That explanation is stronger evidence of understanding than a program that runs correctly by trial and error.
When these ideas feel solid, connect them to the guided lessons in Decision Maker and Loop Magic, or let the learner combine loops and conditionals into a larger game or quiz project.
Keep exploring
Sources and further reading
Keep the curiosity going
Learn by making something that matters to you.
Explore friendly, hands-on lessons designed to help young learners build real Python and AI skills.
