This post solves 2026 P-I Q40, which asks which Python program matches a REPEAT UNTIL pseudocode loop. Python has no REPEAT and no UNTIL, and that small gap makes many students stop in the exam hall even when they know while and print. The topic needs one picture and two rules, not five patterns to memorise.
The post has four parts:
- The answer: the question, its answer and the output check (section 1).
- The idea: where an algorithm runs, and the two rules that turn REPEAT UNTIL into Python (sections 2 and 3).
- Where it comes from: the past papers that asked this since 2013, and the syllabus, Teachers' Guide and Resource Book pages behind it, with their gaps (sections 4 and 5).
- How to use it: the trap in each option, a 30-second exam check, the next forms of the question and past paper questions to try (sections 6 to 8).
1. The question and its answer
The question is on page 9 of the Sinhala paper. Here is our English translation, with the code exactly as printed.
2026 P-I Q40. Consider the pseudocode shown below. Which of the following Python code correctly represents the above pseudocode?
The Sinhala paper reads "පහත පෙන්වා ඇති ව්යාජ කේතය (pseudocode) සලකා බලන්න." and "ඉහත ව්යාජ කේතය නිවැරදිව නිරූපණය කරන්නේ පහත කවර පයිතන් කේතය මගින් ද?". Sinhala-medium students meet pseudocode as "ව්යාජ කේතය".
START
n:= 5
REPEAT
PRINT n
n:= n - 2
UNTIL n < 1
STOP
The five options, as printed:
(1) n = 5 (2) n = 5
while n < 1: while True:
print(n) print(n)
n -= 2 n -= 2
if n > 1:
break
(3) n = 5 (4) n = 5
while n >= 1: for i in range(5):
n -= 2 print(i)
print(n)
(5) n = 5
while n >= 1:
print(n)
n -= 2
Our worked answer is (5).
Check it by the output
The output check starts with a hand trace of the pseudocode. Write each value of n.
- n is 5. PRINT 5. n becomes 3. Is 3 < 1? No, so repeat.
- PRINT 3. n becomes 1. Is 1 < 1? No, so repeat.
- PRINT 1. n becomes -1. Is -1 < 1? Yes, so stop.
The pseudocode prints 5 3 1. Call this the output fingerprint. A correct Python program must print the same fingerprint. Only option (5) prints 5 3 1.
Why the fingerprint is not enough
Option (5) matches here only because n starts at 5. With the start n:= 0, REPEAT prints 0 once, because it tests at the end. Option (5) prints nothing, because it tests first. So option (5) is the best match for this start value, not a copy of every REPEAT loop. Section 3 shows why.
2. Where an algorithm runs
An algorithm runs on one road from the problem down to the machine. Many students learn Python and flowcharts as two separate lessons. They are the same idea at two levels.
Five stops on one road
- The problem. "Print 5, 3, 1." Students understand it before they write anything (9.1).
- The algorithm. The steps, with no language yet (9.3). The Resource Book says algorithms can be shown "Graphically" as a flow chart or "Textually" as pseudocode (Resource Book 9.3, page 33) (Resource Book 9.3, පිටුව 38). The Sinhala Resource Book calls them "රූපමය නිරූපණය" and "ලිඛිතමය නිරූපණය", and misprints the English term as "Graphycal Representation". The correct word is Graphical.
- The source code. The same steps in Python. The Resource Book says "Flow chart / Pseudocode are converted into source code" (Resource Book 9.3, page 33) (Resource Book 9.3, පිටුව 38). The Sinhala Resource Book says "ගැලීම් සටහන/ ව්යාජ කේතය පසුව ප්රභව කේත බවට පරිවර්තනය කරනු ලැබේ".
- The translator. It turns Python into instructions the processor can run (9.5).
- The machine. The processor has no REPEAT and no
while. It only compares two values and jumps to another instruction.
Why this picture helps in the exam
Pseudocode is "independent of any particular language" (Resource Book 9.3, page 35) (Resource Book 9.3, පිටුව 39). The Sinhala Resource Book says "කිසියම් විශේෂිත භාෂාවකින් ස්වාධීන වේ". So REPEAT UNTIL is not a Python word. It is a word for a shape of steps. The task in Q40 is to keep the shape and change only the words.
At the bottom of the road, every loop is the same thing: do some steps, compare, jump back. The only question is where the compare sits. That is the divide point that frightens students. Section 3 gives the two rules for it.
3. The two rules for REPEAT UNTIL
Two rules decide every option in Q40: where the test sits, and what the condition means. The syllabus lists three control structures: sequence, selection and repetition (Syllabus 9.8, page 39) (විෂය නිර්දේශය 9.8, පිටුව 36). The Sinhala syllabus calls them "පාලන ව්යුහ" and names repetition "පුනරුක්තිය", with iteration ("පුනර්කරණය") and looping ("ලූපනය") under it. Repetition has two shapes. With the shapes learnt, every loop question becomes the same question.
Rule 1 is where the test sits
- Test at the top (pre-test). Check first, then run the body. If the check fails at once, the body runs zero times. Python's
whileworks this way (Resource Book 9.8, page 50) (Resource Book 9.8, පිටුව 56). - Test at the bottom (post-test). Run the body first, then check. The body always runs at least once. REPEAT ... UNTIL works this way.
Python has only while and for for repetition (Resource Book 9.8, page 49) (Resource Book 9.8, පිටුව 55). It has no post-test loop. So every REPEAT UNTIL question really asks one thing: how to build a test-at-the-bottom loop with a test-at-the-top tool.
Rule 2 is what the condition means
- UNTIL gives the stop condition. "Repeat until n < 1" means "stop when n < 1 is true".
- while gives the keep-going condition. "while n >= 1" means "go on while n >= 1 is true".
So the condition must flip in the move from UNTIL to while. The flip of n < 1 is n >= 1, not n > 1. These are the flips to know:
<flips to>=>flips to<===flips to!=<=flips to>
Two correct ways to write it
Way A, the faithful form. Copy the shape exactly. Run the body, then test the UNTIL condition, and leave the loop with break.
n = 5
while True:
print(n)
n -= 2
if n < 1:
break
The Resource Book says "With the break statement we can stop the loop even if the while condition is true" (Resource Book 9.8, page 51) (Resource Book 9.8, පිටුව 57). The Sinhala Resource Book says "break ප්රකාශයේ දී කොන්දේසිය සත්ය වුව ද, ලූපය නතර කළ හැකි ය". Here the while True never stops by itself, so break is the only exit. Note: the if keeps the UNTIL condition as it is. No flip.
Way B, the short form. Put the flipped condition at the top: while n >= 1:. This is safe only when the condition is true at the start, so the first pass happens anyway. In Q40, n starts at 5 and 5 >= 1 is true. So Way B gives the same output.
4. The same question since 2013
Short answer: the skill is old, the notation is new. The paper has asked "which Python program matches this algorithm" for more than ten years. Until 2026, the algorithm was always a flowchart.
The flowchart years
- 2013 P-I Q19 gives a flowchart with the test at the top. The options mix
while (a>5)withwhile not (a>5), and move theprintin and out of the loop (old syllabus). - 2015 P-I Q31 gives a flowchart with the test at the bottom. One option uses a flag variable,
iterate = True, to build the loop (old syllabus). - 2018 P-I Q25 gives a test-at-the-bottom flowchart and three programs. One uses
while, one usesfor i in range(10), and one useswhile Truewithif (i > 10): break(old syllabus). - 2019 (New Syllabus) P-I Q42 follows the same plan with three programs, A, B and C (9.8).
- 2020 P-I Q34 and 2021 (held 2022) P-I Q40 give a counting flowchart and ask which of three programs work (9.8).
In those flowcharts, from 2015 on, the diamond sits at the bottom, after the body. That is a REPEAT UNTIL loop drawn as a picture. Students have met this shape many times. 2026 only wrote it in words.
Pseudocode was not new either
- 2012 P-I Q20 asks for the output of a pseudocode loop,
For count = 1 To 10 ... Next count(old syllabus). - 2022 (held 2023) P-II B Q7(b) shows a
While (true) { ... }pseudocode loop for a greenhouse sensor (9.3).
What is new in 2026
- Pseudocode in place of a flowchart. Students must see the loop shape in words.
- The word UNTIL. A flowchart shows the exit arrow. UNTIL states the stop condition, and students must flip it themselves.
- One program in each option. The old questions used I, II and III with "A and B only" options. Q40 gives five single programs. Each wrong option has one planted mistake, and the correct option has none.
- Symbols from outside the books. The pseudocode uses
:=for assignment and START and STOP. No book example uses these (see Section 5).
Read the exit arrow on every flowchart
The bottom diamonds do not all exit the same way.
- In 2018 P-I Q25, the loop goes back on Yes. The diamond holds a keep-going condition, like
while. - In 2019 (New Syllabus) P-I Q42, the loop goes back on No. The diamond holds a stop condition, like UNTIL.
So always ask which arrow goes back. That arrow says whether to copy the condition or flip it.
Across the eight new-syllabus papers, algorithms (9.3) hold 7 MCQs and 56.5 Paper II marks. Control structures (9.8) hold 17 MCQs and 59 Paper II marks. Both appear in all eight papers.
5. Where it sits in the books
Q40 joins two levels of Unit 9: algorithms (9.3) and control structures (9.8).
The pieces in the books
- The outcome. Level 9.3 lists "Flow charts", "Pseudo codes" and "Hand traces" as its contents (Syllabus 9.3, page 36) (විෂය නිර්දේශය 9.3, පිටුව 34). The Sinhala syllabus lists them as "ගැලීම් සටහන් (flow charts)", "ව්යාජ කේත(pseudo code)" and "හස්තානුරේඛන (hand traces)".
- What pseudocode is. The Teachers' Guide says "Pseudo code is a high level description used to represent an algorithm" (Teachers' Guide 9.3, page 68) (ගුරු අත්පොත 9.3, පිටුව 68). The Sinhala Teachers' Guide says "ව්යාජ කේත යනු ඇල්ගොරිතමයක් නිරූපණය කරන ලේඛනාත්මක ආකාරයකි".
- From pseudocode to code. "Flow chart / Pseudocode are converted into source code" (Resource Book 9.3, page 33) (Resource Book 9.3, පිටුව 38). A hand trace is a "manual check" done "line by line" before the conversion (Resource Book 9.3, page 35) (Resource Book 9.3, පිටුව 40). The Sinhala Resource Book calls it "අත්යුරු පරීක්ෂාවක් (manual check)" done "පේළියෙන් පේළිය".
- Repetition. The Teachers' Guide asks students to appreciate "that some sequences need to be repeated (pre-determined or post- determined number of times – Iteration and Looping)" (Teachers' Guide 9.8, page 77) (ගුරු අත්පොත 9.8, පිටුව 76). The Sinhala Teachers' Guide names it "පුනර්කරණය සහ ලූපනය".
- Python loops. Python gives
whileandforfor repetition, andbreakto leave a loop early (Resource Book 9.8, pages 49 to 51) (Resource Book 9.8, පිටු 55 සිට 57).
Book gaps to know
- No REPEAT UNTIL anywhere. The only pseudocode example in the Resource Book is an If, Else, Endif for voting age (Resource Book 9.3, page 35) (Resource Book 9.3, පිටුව 40). No book shows a loop in pseudocode.
- No names for the two shapes. No book uses the words pre-test and post-test. The Teachers' Guide line on "pre-determined or post- determined" is about how many times a loop runs, not where its test sits (Teachers' Guide 9.8, page 77) (ගුරු අත්පොත 9.8, පිටුව 76). The Sinhala line says the same: a count "පෙර සඳහන් කළ හෝ ක්රමලේඛය හෝ සිදුවන අවස්ථාවේ දී දැන ගන්නා", stated before or known while the program runs.
- A mixed-up "until" in the English book. The English Teachers' Guide asks for a program "to add numbers 1, 2, 3, 4 an so on until the total is less than 50" (Teachers' Guide 9.8, page 77), with "an" for "and". Read literally, the total starts below 50, so the loop would stop at once. The meaning is "while the total is less than 50". The Sinhala Teachers' Guide gets it right: "එකතුව 50ට අඩු වන ලෙස", so that the total stays below 50 (ගුරු අත්පොත 9.8, පිටුව 77). This is the same until and while mix-up that Q40 tests.
- Different symbols. The Resource Book writes Begin and End, Read and Display (Resource Book 9.3, page 35) (Resource Book 9.3, පිටුව 40). Q40 writes START and STOP, PRINT and
:=. Treat any pseudocode words as plain English. The shape is what counts.
6. Traps and a calm exam check
Each wrong option hides one planted mistake. Name the mistake, and the option falls away.
The trap in each option
- (1) The copy trap.
while n < 1copies the UNTIL condition with no flip. 5 < 1 is false at the start, so the body never runs. - (2) The shape trap.
while Truewithbreakis the faithful form from Section 3. But the test isn > 1, notn < 1. After the first print, n is 3, 3 > 1 is true, and the loop breaks. A student who knows the shape but skips the condition picks this one. - (3) The order trap. The condition is flipped correctly. But
n -= 2comes beforeprint(n), so the first value printed is 3. The loop body must keep the pseudocode's order. - (4) The counter trap.
for i in range(5)repeats five times and printsi, the counter, from 0 to 4. It never uses n or the step of 2. - (5) The match. Flipped condition, same order, same output.
The 30-second check for a nervous moment
When the mind goes blank, do these four steps. They need no theory.
- Trace. Write the pseudocode's output: 5 3 1.
- First value. Cross out any option whose first output is not 5. That removes (1), (3) and (4).
- Last value. Run the rest to the end. (2) stops after 5. (5) gives 5 3 1.
- Confirm with the rule. UNTIL n < 1 flips to while n >= 1, and print comes before the change. Option (5) agrees.
The trace is the "hand trace" the books ask for (9.3). A hand trace is a "manual check" done "line by line" (Resource Book 9.3, page 35) (Resource Book 9.3, පිටුව 40). It works on every "which code matches" question, even when the rules are forgotten.
7. Thought experiment for the next question
The next paper can move this pattern in six ways. These are our own examples from the syllabus, not a promise of what will come.
Six ways the pattern can move
- Change the start value. With
n:= 0, REPEAT prints 0 once.while n >= 1prints nothing. Now only the faithful form matches. This tests Rule 1 directly. - Ask for the faithful form. Five options, all with
while Trueandbreak. Only the condition and the order differ. - Give WHILE pseudocode.
WHILE n >= 1 DO ... ENDWHILEcopies straight towhile n >= 1:. A student who flips by habit falls into the opposite trap. - Give FOR pseudocode.
For count = 1 To 10includes 10. Python'srange(1, 11)stops before 11. This links to 2012 P-I Q20 (old syllabus). - Move it to Paper II. Ask for the Python of a short REPEAT UNTIL algorithm. Recent Paper II questions already ask students to turn a flowchart into a
whileloop, as in 2026 P-II A Q3(c)(ii) (9.8). - Draw it again. Bring back a flowchart and ask whether the loop goes back on Yes or on No.
8. Past paper questions to try
Use the two rules and the 30-second check on each question. The answers are not in this post.
Which code matches the algorithm
- 2019 (New Syllabus) P-I Q42: three programs for a test-at-the-bottom flowchart.
- 2020 P-I Q34: three programs that count items in a list.
- 2021 (held 2022) P-I Q40: three programs that count values below 100.
- 2018 P-I Q25:
while,forandwhile Truewithbreak(old syllabus).
Trace a loop
- 2012 P-I Q20: the output of a pseudocode For loop (old syllabus).
- 2023 (held 2024) P-I Q43: a
whileloop withbreak, count the output. - 2026 P-I Q36: a nested
forandwhiletrace, in the same paper as Q40.
Write the loop
- 2026 P-II A Q3(c)(ii): code a reverse-integer flowchart as a
whileloop. - 2025 P-II A Q3(b)(iii): a
whileloop with selection and a sentinel. - 2022 (held 2023) P-II B Q8(a)(i): trace a
whileloop that sums digits.
The 2023 (held 2024) and 2026 keys are not official. The 2022 (held 2023) key comes from a third-party scheme.
Sources
- 2026 P-I Q40: G.C.E. (A/L) 2026 ICT Paper I, Sinhala medium, page 9 (AL/2026/20/S-I). The key is our own worked answer, not official.
- Past papers 2011 to 2026: G.C.E. (A/L) ICT question papers, Department of Examinations, Sri Lanka.
- NIE A/L ICT Syllabus (2017), English pages 36 (9.3) and 39 (9.8); Sinhala pages 34 (9.3) and 36 (9.8).
- G13 Teachers' Guide (English), pages 68 (9.3) and 77 (9.8); G13 Teachers' Guide (Sinhala), pages 68 (9.3) and 76 to 77 (9.8).
- G13 Resource Book (English), pages 33 and 35 (9.3), and 49 to 51 (9.8); G13 Resource Book (Sinhala), pages 38 to 40 (9.3), and 55 to 57 (9.8).
