This post explains how to trace a Python loop on paper, the skill behind most of the Python marks in Unit 9. Control structures (9.8) appear in all eight new-syllabus papers, 2019 to 2026, with 17 MCQs and 59 Paper II marks.
The post has four parts:
- The ground: what the syllabus asks, how the papers ask it, and the trace table method (sections 1 and 2).
- The two loops:
forwithrange(), andwhileloops that stop on a value (sections 3 and 4). - Changing the flow:
breakandcontinue, nested loops, flags and the loopelse(sections 5 to 7). - Practice: past paper questions to try, sorted by type (section 8).
1. Loops in the syllabus and the papers
A loop is one of three control structures. The syllabus lists sequence, selection and repetition, and splits repetition into iteration and looping (Syllabus 9.8, page 39) (විෂය නිර්දේශය 9.8, පිටුව 36).
The Teachers' Guide explains the split. Some sequences repeat a "pre-determined or post-determined number of times", which is iteration and looping (Teachers' Guide 9.8, page 77) (ගුරු අත්පොත 9.8, පිටුව 76). The Sinhala books call them "පුනර්කරණය" (iteration) and "ලූපනය" (looping), and call repetition "පුනරුක්තිය". The Sinhala Resource Book uses one word for both. Its heading "පයිතන්හි පුනර්කරණ (Python Loops)" puts while and for under "පුනර්කරණ" (Resource Book 9.8, පිටුව 56). Learn both words, because Sinhala-medium papers use them.
In Python, the two kinds map to the two loops:
forloop (iteration): the count is known before the loop starts. The Resource Book says a for loop repeats a block "for a known number of times" (Resource Book 9.8, page 51) (Resource Book 9.8, පිටුව 56).whileloop (looping): the count is not known. The loop runs while its condition stays True (Resource Book 9.8, page 50) (Resource Book 9.8, පිටුව 56).
How the papers ask about loops
The papers ask loops in four forms. The learning outcomes ask students to use control structures "appropriately" and to apply "nested control structures" (Teachers' Guide 9.8, page 77) (ගුරු අත්පොත 9.8, පිටුව 76).
- Find the output (Paper I): most loop MCQs give code and ask the output, such as 2024 Paper I, Q39 and 2026 Paper I, Q36.
- Trace and state the purpose (Paper II): 2019 (New Syllabus) Paper II, A, Q2(b) and 2026 Paper II, A, Q3(a) give inputs and ask the output, then the purpose.
- Fill the blanks (Paper II): 2024 Paper II, A, Q3(c) and 2026 Paper II, A, Q3(b) give a program with gaps.
- Write the loop (Paper II): 2025 Paper II, A, Q3(b) and 2026 Paper II, A, Q3(c) ask a program from a description or a flowchart.
Since 2021 (held 2022), Paper II, Part A, Q3 has held a Python loop every year. All four forms need the same skill: run the code on paper, one line at a time.
2. The trace table method
A trace table runs the code on paper and writes down every change. The Teachers' Guide asks students to compare a program's output with its source code (Teachers' Guide 9.8, page 78) (ගුරු අත්පොත 9.8, පිටුව 77). The trace table is the way to do that in an exam.
Follow these steps:
- Make one column for each variable that changes, one for the loop condition and one for the output.
- Write the values before the loop in the first row.
- Test the condition. Write True or False. False ends the loop.
- Run the body lines in their written order. Write each new value in a new row.
- When the loop ends, run the lines after it, once.
The options of a loop MCQ are often the results of a skipped step. Do not run the loop in the head. Draw the table.
A worked example from 2026
2026 Paper II, A, Q3(a) gives this program and the inputs 10, 5, 0, 15, 25, -1.
x = 0
found = False
n = int(input())
while n >= 0:
found = True
if n > x:
x = n
n = int(input())
if found:
print(x)
else:
print("No positive numbers were entered")
The trace table has the columns n, n >= 0, found, n > x and x:
- Start: x = 0, found = False, n = 10.
- n = 10: True. found = True. 10 > 0, so x = 10.
- n = 5: True. 5 > 10 is False, so x stays 10.
- n = 0: True. x stays 10.
- n = 15: True. x = 15.
- n = 25: True. x = 25.
- n = -1: False. The loop stops. found is True, so the program prints 25.
So (i) the output is 25. (ii) The program prints the largest of the numbers entered before a negative number. (iii) found records whether the loop body ran at least once (section 7). These are our own worked answers, checked by running the code, because 2026 has no official scheme yet.
3. for loops and range()
A for loop takes its values from a structure, one at a time. The structure can be a list, a string or a range(). The Resource Book shows a for loop over a list of animals (Resource Book 9.8, page 51) (Resource Book 9.8, පිටුව 57).
range(a, b) starts at a and stops before b. So it gives b minus a values. range(b) starts at 0. A third number is the step, and a negative step counts down.
range(5)gives 0, 1, 2, 3, 4.range(1, 5)gives 1, 2, 3, 4.range(2, 3, 1)gives only 2.range(10, 0, -2)gives 10, 8, 6, 4, 2.range(5, 1)gives nothing, so the body never runs.
The stop value is left out for a reason. A list of length 5 has the indexes 0 to 4, and range(len(L)) gives exactly those.
Neither Resource Book shows range() in its control structures pages. Their for loop example uses only a list (Resource Book 9.8, page 51) (Resource Book 9.8, පිටුව 57). But almost every for loop in the papers uses range(). Learn it from the papers.
Two MCQs that test range and for
2024 Paper I, Q39 prints x, y from for x in range(1, 3) and an inner for y in qns, where qns = ["a", "b"]. x takes 1 and 2 only. So the output is 1 a 1 b 2 a 2 b, option (3). Option (4) reads range(1, 3) as 1 and 3. The key is from a third-party copy of the marking scheme.
2025 Paper I, Q37 loops over the string "01101" from the left. Each "1" adds 2 to the power p, and p goes up by 1 on each character:
- p = 1: t = 2.
- p = 2: t = 2 + 4 = 6.
- p = 4: t = 6 + 16 = 22.
So the output is 22, option (4), the official key. A student who reads "01101" as binary 13 finds no such option. Trace what the code does, not what the string looks like.
4. while loops that stop on a value
A while loop runs while its condition is True and stops when the condition becomes False. The Resource Book shows the parts as initialisation, condition, body and increment (Resource Book 9.8, page 50) (Resource Book 9.8, පිටුව 56). The body must change something that the condition tests. If it does not, the loop never stops.
Sentinel input
A sentinel is an input value that means "no more data". It cannot be real data. Marks and prices are never negative, so -1 is a common sentinel. The sentinel loop has four parts:
- Read the first value before the loop.
- Test that the value is not the sentinel.
- Do the work in the body.
- Read the next value as the last line of the body.
2026 Paper II, A, Q3(b) asks for the six blanks of an average program with this shape. Our own worked answer, checked by running the code:
- ❶
0, ❷total + n, ❸count + 1. - ❹
n = int(input()), ❺count, ❻total / count.
Blank ❺ must be count, not total. The inputs 0, 0, -1 give a total of 0, but two numbers were entered, and their average is 0. Also, total / count with count = 0 stops the program with an error. Without ❹, n never changes and the loop never stops.
2025 Paper II, A, Q3(b) asks for the sum of the even inputs before a negative number. With 1, 2, 3, 5, -1, only 2 is even, so the output is 2. In the program, the read line stays outside the if, as the last line of the while body. If it goes inside the if, an odd input is never replaced, and the loop never stops. This is our own worked answer.
Digits with % 10 and // 10
A digit loop takes a number apart from the right. n % 10 gives the last digit. n // 10 removes it. The loop stops when n becomes 0.
2022 (held 2023) Paper II, B, Q8(a) adds the digits of 1002. A trace table gives C = 2, 0, 0, 1 and B = 2, 2, 2, 3. So (i) the output is 3. (ii) To print the reverse, change B = B + C to B = B * 10 + C. The * 10 moves the digits already in B one place to the left, and C fills the units place. These are our own worked answers.
2026 Paper II, A, Q3(c) gives the same idea as a flowchart for 1234 to 4321. The blanks are reverse, n > 0, n % 10, reverse * 10 + digit and n = n // 10. With n >= 0, the loop would never stop, because 0 // 10 is still 0. The Python code needs a while loop with the same five parts. This is our own worked answer.
5. break and continue
break ends the loop at once. continue ends only the current pass, and the loop goes on with its next pass. Both act on the nearest loop that holds them.
The Resource Books show both with short examples (Resource Book 9.8, pages 51 and 52) (Resource Book 9.8, පිටු 57 සහ 58). The English Resource Book titles one example "Continue to the next iteration if i is 3". But its code uses the variable a, not i (Resource Book 9.8, page 52). The Sinhala book writes the title correctly, "a == 3 නම් ඊළඟ පුනර්කරණය ක්රියාත්මක වේ" (Resource Book 9.8, පිටුව 58).
Three MCQs worked
- 2023 (held 2024) Paper I, Q43 counts the stars. i starts at 7. Pass 1: i = 4, one star,
4 <= 2is False, so theelseprints a second star. Pass 2: i = 1, a third star,1 <= 2is True, sobreak. The answer is 3 stars, option (2). The star printed before thebreakstill counts. - 2022 (held 2023) Paper I, Q41 starts val at 9, with an inner
range(2, 3, 1)that runs once each pass. val goes 10 (even,continue), 13, 14, 17, 18. The answer is 18, option (1). Theval += 2undercontinuenever runs. Option (2), 24, counts it. - 2024 Paper I, Q41 loops over a list of tuples and skips each mark under 50 with
continue. Rajah (34) and rani (49) are skipped, so the output isamara fahim, option (3). Option (5) prints the two skipped names instead. The key is from a third-party copy of the marking scheme.
The 2022 (held 2023) and 2023 (held 2024) answers are our own worked answers, checked by running the code.
6. Nested loops
In a nested loop, the inner loop runs from start to end on each pass of the outer loop. So the inner body runs (outer passes) x (inner passes) times. The Teachers' Guide names "sub repetitions inside a repetition (Nested Loops)" (Teachers' Guide 9.8, page 77). The Sinhala Guide calls them "නිඩිත ලූපනයන්" (ගුරු අත්පොත 9.8, පිටුව 76).
The control structures pages of both Resource Books have no nested loop example, though the learning outcome asks for nested control structures. The papers ask them in most years.
- 2026 Paper I, Q36: a
whileinside afor. When i = 1, j = 1, so y = 0 + 2 = 2 and j = -1. Then x = 3. When i = 2, j = 2, so y = 2 + 3 = 5 and j = 0. Then x = 4. The output is4 5, option (3). This is our own worked answer. On the second pass, y adds the current x, 3, not 2. - 2024 Paper II, B, Q8(a):
calculate(4)adds i x j, where j runs overrange(i). The sums are 0, then 2, then 0 + 3 + 6 = 9, then 0 + 4 + 8 + 12 = 24. So the result is 0 + 2 + 9 + 24 = 35. Starting j at 1 gives the wrong total of 50. This is our own worked answer. - 2023 (held 2024) Paper I, Q45: the inner
print(..., end=' ')keeps the values on one line, and the outerprint()starts a new line. So the number of lines equals the outer passes. The output is1,2 4,3 6 9, option (3), our own worked answer.
7. Flags and the loop else
A flag is a True or False variable that records whether something happened inside a loop. The program sets it before the loop, changes it in the loop, and tests it after the loop. The found variable of 2026 Paper II, A, Q3(a) is a flag (section 2).
Prime numbers in two papers
A number is prime when no number from 2 up to one less than it divides it. The loop tries each divisor and leaves with break at the first one that divides.
2021 (held 2022) Paper II, A, Q3(c) uses a flag. For num from 2 to 5, flag starts at 1 and becomes 0 when a divisor is found. So the output is 2, 3 and 5, each on its own line. When num = 2, range(2, 2) is empty, the flag stays 1, and 2 is printed. This is our own worked answer.
2024 Paper II, A, Q3(c) does the same with the loop else. A loop else runs only when the loop ends without a break. So the else means "no divisor was found", which is the job of the flag, with no variable. The five blanks are int, upper + 1, num, num % i and break. This is our own worked answer.
8. Past paper questions to try
Try each question with the method of its section, then check the answer given there. Questions without an answer in this post are for practice.
- for and range (section 3): 2019 (New Syllabus) Paper I, Q46, 2024 Paper I, Q39, 2025 Paper I, Q37, 2023 (held 2024) Paper II, A, Q3(b).
- Sentinel while loops (section 4): 2019 (New Syllabus) Paper II, A, Q2(b), 2025 Paper II, A, Q3(b), 2025 Paper II, B, Q8(c), 2026 Paper II, A, Q3(a) and (b).
- Digit loops (section 4): 2022 (held 2023) Paper II, B, Q8(a), 2024 Paper II, B, Q8(b), 2026 Paper II, A, Q3(c).
- break and continue (section 5): 2019 (New Syllabus) Paper I, Q47, 2020 Paper I, Q40, 2021 (held 2022) Paper I, Q44, 2022 (held 2023) Paper I, Q41, 2023 (held 2024) Paper I, Q43, 2024 Paper I, Q41.
- Nested loops (section 6): 2023 (held 2024) Paper I, Q45, 2024 Paper II, B, Q8(a), 2026 Paper I, Q36.
- Flags and the loop else (section 7): 2021 (held 2022) Paper II, A, Q3(c), 2024 Paper II, A, Q3(c), 2026 Paper II, A, Q3(a)(iii).
The old-syllabus papers asked the same skills. Try these for more practice.
- 2013 Paper I, Q13: a
whileloop trace. - 2016 Paper II, A, Q4(a): a
whileloop that never stops, because of its indentation. - 2018 Paper I, Q47: a
forloop withrange()that adds a total.
