Control Flow: Conditionals and Loops
Byte the friendly robot stands at a glowing fork in a circuit-board path, one arm pointing left and one right, while spinning gears loop above its head in a bright workshop
- Explain how an if/else statement chooses between two different paths based on a condition
- Predict which branch of an if/else block runs for a given input
- Describe how a loop repeats a set of steps without rewriting them
- Identify whether a real-world task needs a conditional, a loop, or both
Key terms
- Control flow
- The order in which a program runs its steps
- Conditional
- An if/else statement that chooses a path based on a condition
- Loop
- A structure that repeats steps until a condition stops it
- Branch
- One of the alternative paths an if/else can take
Only One Branch Runs
An if/else is a fork in the road, not a place where the program splits into two copies of itself. The computer checks the condition once, then runs exactly one branch: the if branch when the condition is true, otherwise the else branch. The other branch is skipped entirely and its steps never execute. Remembering that only one branch ever runs prevents a very common tracing error, where learners imagine both the if and the else lines printing one after the other.
Loops Replace Copy and Paste
Without loops you would rewrite the same steps once for each repetition, which is tiring and easy to get wrong. A loop states the steps a single time and tells the computer to repeat them, either a fixed number of times or while a condition stays true. When the count finishes or the condition turns false, the loop stops and the program moves on. Loops and conditionals are partners, not rivals: an if/else placed inside a loop lets the program decide something fresh on every single repetition.
Worked examples
Trace IF score >= 60 print Pass ELSE print Fail with score 45
- Evaluate the condition: is 45 greater than or equal to 60? No.
- The condition is false, so the if branch is skipped.
- Because the condition is false, the else branch runs.
- The program prints 'Fail' and nothing else.
Answer: It prints Fail
Count how many times a loop runs for 5 plants
- The loop says repeat the watering step for each of the 5 plants.
- It waters plant 1, then 2, then 3, then 4, then 5.
- After plant 5 there are no plants left, so the condition to continue is false.
- The loop stops after running the step 5 times.
Answer: The loop runs 5 times
Activity
Sort each everyday task into the category that best describes what it mainly needs: a conditional, a loop, or both
Practice
Decide whether 'greet every guest at the door' needs a loop, conditional, or both.
Predict the output of an if/else printing Win or Lose for several scores.
Common mistakes to avoid
- Both branches of an if/else runOnly one branch ever runs: the if branch if the condition is true, otherwise the else branch.
- Loops are used to store valuesLoops repeat steps, while storing values is the job of variables, not loops.
Check your understanding
A program runs: IF score >= 60, print "Pass"; ELSE print "Fail". The score is 45. What does it print?
Why would a programmer use a loop instead of writing the same steps many times?
In an if/else statement where the condition is true, which steps run?
Recap
Control flow is the order a program runs its steps. A conditional picks exactly one branch based on a true or false condition, and a loop repeats steps until its condition ends. Placing an if/else inside a loop lets a program decide and repeat at the same time.
Reflect
What repeated task in your day could you describe as a loop with a conditional inside?