Predicting what a loop outputs
About this lesson: Predicting what a loop outputs
In short
Good programmers predict the output before running code. Choose your prediction for this loop, then run it to check.

What you'll explore
- Predict, then run: What will it print?
- Good programmers predict the output before running code. Choose your prediction for this loop, then run it to check.
- Starting values: Change the start
- x doubles on each pass. Change the starting value of x and predict the last output before you run it.
- Off-by-one: One too many, one too few
- We want exactly 5 stars. Try three loops: which one gives 5? Checking the first and last pass catches most loop mistakes.
- Check yourself: Predict
- x ← 2 FOR i ← 1 TO 3 x ← x + 5 NEXT i OUTPUT x
Also in this lesson
- Common mix-ups
- Predict these
- Prediction game
Words to know
- output
- Data a program produces.
- pass
- One run through the loop body.
- off-by-one error
- A loop that runs one time too many or too few.
- initial value
- The starting value of a variable.
- predict
- Say what will happen before it does.
Sources
- BBC Bitesize KS3 Computer Science
- Raspberry Pi Foundation, Computing curriculum resources
← Following a loop · Designing an algorithm: flowchart and pseudocode →