Technologies K–10 · Years 7–8
Tracing nested loops by hand before running them
Digital and Communication Technologies focus area (NSW Technology 7–8, 2023); Digital Technologies: Processes and production skills, Generating and designing (ACARA v9)
This site has no interactive model of its own. Where a step or a material names a Concept Studio model, simulation or tool, it has not been built; an external simulation a step names (for example PhET) is not part of this site.
The idea
A trace table follows every variable through each pass of a loop, so the output of nested loops can be predicted, and errors such as an off-by-one range found, before the program runs.
What you need
- Printed program A: for i in range(1, 4): make an empty list parts; for j in range(1, i + 1): append str(i * j) to parts; print(' '.join(parts))
- Printed program B: the same, but with range(1, i) in the inner loop
- Trace table template with columns i, j, i * j, parts, output
- Computer with Python 3
How to do it
- Draw a flowchart of program A, showing the inner loop inside the outer loop.
- Trace program A by hand, one row per pass of the inner loop, and write the predicted output.
- Count how many times the inner loop body runs.
- Trace program B the same way and write its predicted output.
- Run both programs and compare with the predictions.
- Rewrite program A in pseudocode and change it so it prints the 5-row version; predict before running.
What you should see
Program A prints three lines: '1', '2 4' and '3 6 9', and its inner loop body runs 6 times (1 + 2 + 3). Program B's inner range stops one early, so it prints an empty line, then '2', then '3 6', and its inner body runs 3 times. The learner knows it worked when the traced output matches the program output line for line, and they can point to the exact range that causes program B's error.
What changes
This activity lists no variables to change, measure and keep the same.
Common misconceptions
Each of these ideas is wrong, and the activity is a chance to test it.
- range(1, 4) includes 4 (it stops before 4).
- The inner loop runs once in total (it runs completely on every pass of the outer loop).
- If the program runs without an error message it is correct (program B runs but prints the wrong output).
Safety card
Hazards
- None beyond normal computer use
Controls
- Not applicable
Note
No chemicals or heat.
Curriculum references
The NSW syllabus outcomes and Australian Curriculum v9 codes this activity supports. They are references, not a verified or complete curriculum alignment.
- Technology 7–8 Syllabus (2023), NESA. Current: taught from 2026. Code read from the outcomes page on 2026-09-22.TE4-DIG-02
- Technology Mandatory 7–8 Syllabus (2017), NESA. Outgoing: replaced by the Technology 7–8 Syllabus (2023) from 2026 and not available after December 2027. Code read from the official syllabus document (DOCX) on 2026-09-22.TE4-4DP
- Australian Curriculum v9AC9TDI8P05AC9TDI8P06
Sources
The pages the author read to write this activity.
- curriculum.nsw.edu.au/learning-areas/tas/technology-7-8-2023/outcomes
- www.nsw.gov.au/education-and-training/nesa/curriculum/tas/technology-mandatory-7-8-2017
- docs.python.org/3/tutorial/controlflow.html
- www.digitaltechnologieshub.edu.au/plan-and-prepare/scope-and-sequence-f-10/years-7-8
- www.digitaltechnologieshub.edu.au/media/ywvisgk0/dt-challenge-7-8-python-intro-to-microbit-lesson-plan.pdf