Definite vs Indefinite Iteration: Pre and Post-Condition Loops in Pseudocode
When you're learning algorithms for Cambridge IGCSE (0478) or A-Level (9618) Computer Science, mastering iteration (loops) is critical. But it’s not just about knowing the keywords—you need to understand when and why to use each type of loop.
The two main classifications you need to know for your exam are Definite vs Indefinite iteration, and for indefinite loops, the difference between Pre-condition and Post-condition loops.
Let’s break them down clearly.
1. Definite Iteration (The FOR Loop)
Definite iteration is used when you know exactly how many times a loop needs to run before it even starts. It is also known as a count-controlled loop.
In pseudocode, definite iteration is almost always implemented using a FOR...NEXT loop.
DECLARE Count : INTEGER // We know this will run exactly 10 times FOR Count ← 1 TO 10 OUTPUT "This is loop number: ", Count NEXT Count
When to use Definite Iteration:
- Iterating through every element of an array of a known size (e.g.,
ARRAY[1:50]). - Running a process a specific number of times (e.g., "Give the user 3 attempts").
- Printing a multiplication table.
2. Indefinite Iteration (Condition-Controlled)
Indefinite iteration is used when you do not know how many times the loop needs to run. Instead of a counter, the loop relies on a condition being met to stop.
Indefinite iteration is split into two distinct types based on when the condition is checked: Pre-condition and Post-condition.
Pre-Condition Loop
Checks the condition before entering the loop body.
- Keyword:
WHILE...ENDWHILE - May run zero times if the condition is false initially.
- Best for: Reading files, searching where an item might not exist.
Post-Condition Loop
Checks the condition after executing the loop body.
- Keyword:
REPEAT...UNTIL - Will always run at least once.
- Best for: Input validation menus, password prompts.
3. Pre-Condition Loops (WHILE)
A pre-condition loop evaluates its condition at the very beginning. If the condition evaluates to FALSE on the very first check, the code inside the loop is completely skipped.
DECLARE Password : STRING Password ← "" // Condition checked BEFORE the loop runs WHILE Password <> "secret" DO OUTPUT "Enter password:" INPUT Password ENDWHILE OUTPUT "Access Granted!"
4. Post-Condition Loops (REPEAT UNTIL)
A post-condition loop executes its block of code first, and then evaluates the condition at the end. This guarantees that the code inside the loop will execute at least one time.
Notice how we don't need to initialize the variable before the loop like we did with the WHILE loop above.
DECLARE Password : STRING // Loop runs first, then condition is checked at the END REPEAT OUTPUT "Enter password:" INPUT Password UNTIL Password = "secret" OUTPUT "Access Granted!"
⚠️ Crucial Exam Tip: Condition Logic
Notice the difference in the logic between WHILE and REPEAT UNTIL:
• WHILE continues running as long as the condition is TRUE (e.g., WHILE Password <> "secret").
• REPEAT UNTIL continues running as long as the condition is FALSE, and stops when it becomes TRUE (e.g., UNTIL Password = "secret").
Summary Comparison Table
| Loop Type | Iteration Type | Keyword | Minimum Executions |
|---|---|---|---|
| FOR Loop | Definite | FOR...NEXT |
Known in advance (e.g., 10) |
| Pre-Condition Loop | Indefinite | WHILE...ENDWHILE |
0 (Zero) |
| Post-Condition Loop | Indefinite | REPEAT...UNTIL |
1 (One) |
Want to test these loops out for yourself? Try them in our free online pseudocode editor!