Tracing loops and assignments
Follow an algorithm one statement at a time instead of guessing its final pattern.
What you will learn
- Update a variable correctly after assignment.
- Track a fixed number of loop iterations.
Assignment changes stored state
The arrow in total ← total×3 means calculate the right-hand side using the current value, then store the result in total. It is not an algebraic equation to solve for total.
A trace table records variables as the algorithm executes. Use a fresh row at a consistent point, such as after each loop iteration, and make that convention clear.
Count iterations carefully
Initialisation happens before the loop unless the algorithm places it inside. A loop that repeats twice performs its body exactly twice. Reinitialising the total on every iteration would be a different algorithm.
For conditional loops, inspect when the condition is tested. A pre-condition loop may run zero times; a post-condition loop runs its body at least once.
Worked example
Set total ← 3, then repeat total ← total×2 three times. Find the final total.
Show the worked solution
- Initial value: 3.
- After iterations 1 and 2: 6, then 12.
- After iteration 3: 24.
Answer 24
Common mistakes
- Do not count initialisation as the first loop iteration.
- Use the updated value in the next iteration.
What can you explain now?
Set x←1, then repeat x←x+4 twice. What is x?
Compare with the explanation
9
The successive values are 1,5,9.
After trying it yourself, choose your next review. This is your self-assessment.
Your review choice appears on Today. Sign in to sync it across devices.
Original StudyForA teaching content · AI-assisted checks · Human teacher review pending.
These lessons teach the topics represented in our current practice sets. They are not a complete course for every paper or option in the qualification.
Open related practice and resources