Learn in your language
Skip to content
IGCSE · Cambridge (CIE) · 0478

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.

Put the idea to work

Worked example

Set total ← 3, then repeat total ← total×2 three times. Find the final total.

Show the worked solution
  1. Initial value: 3.
  2. After iterations 1 and 2: 6, then 12.
  3. 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.
Recall without your notes

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.

Keep learning Computer Science

Course scope and assessment

165 original lessons map the listed objectives across all 10 main topics in the 2026–2028 Version 6 syllabus, with worked explanations and guided, independent and transfer practice. This is mapped teaching coverage, not proof of full exam readiness. Three earlier starter lessons remain available but are not counted as syllabus-mapped teaching. Practical programming experience in an executable browser workspace, full timed-paper practice, human subject-teacher review and the separate 2029–2031 syllabus remain outstanding. Practice is self-checked and does not award an official Cambridge grade.

StudyForA · Learn with understanding

Original teaching content and links to official sources. Check your exam year and qualification before using a resource.