Definite and indefinite iteration
Two Kinds of Iteration
Iteration means repeating a block of statements. Every loop falls into one of two categories depending on how the repetition is controlled.
| Type | Also called | How many times does it run? | Typical keyword |
|---|---|---|---|
| Definite | Count-controlled | A fixed number of times, known before the loop starts | FOR |
| Indefinite | Condition-controlled | An unknown number of times, determined by a condition | WHILE / REPEAT |
Indefinite loops come in two forms depending on where the condition is checked: at the start of each iteration, or at the end. This is an important distinction that AQA requires you to understand, even if your chosen language does not support all forms directly.
Loop Types in Code
A FOR loop repeats a fixed number of times. A loop variable (sometimes called a counter) steps through a range of values automatically. You use a FOR loop when you know exactly how many repetitions are needed before the loop begins.
AQA pseudocode:
FOR i ← 1 TO 5
OUTPUT i
ENDFOR
# Python — FOR loop (definite / count-controlled)
# range(1, 6) produces 1, 2, 3, 4, 5 (upper bound is exclusive in Python)
for i in range(1, 6):
print(i)
# Output: 1 2 3 4 5 (one per line)
' VB.NET — FOR loop (definite / count-controlled)
' Both bounds are inclusive in VB.NET (runs for i = 1, 2, 3, 4, 5)
For i As Integer = 1 To 5
Console.WriteLine(i)
Next i
' Output: 1 2 3 4 5 (one per line)
// C# — FOR loop (definite / count-controlled)
// Initialise; condition; increment — all in one line
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
// Output: 1 2 3 4 5 (one per line)
A WHILE loop checks its condition before each iteration. If the condition is false when the loop is first reached, the body never executes at all. Use a WHILE loop when the number of repetitions is unknown and the loop might legitimately run zero times.
AQA pseudocode:
count ← 5
WHILE count > 0
OUTPUT count
count ← count - 1
ENDWHILE
# Python — WHILE loop (condition checked at START)
count = 5
while count > 0: # condition checked BEFORE each iteration
print(count)
count = count - 1
# Output: 5 4 3 2 1
' VB.NET — WHILE loop (condition checked at START)
Dim count As Integer = 5
Do While count > 0 ' condition checked BEFORE each iteration
Console.WriteLine(count)
count = count - 1
Loop
' Output: 5 4 3 2 1
// C# — WHILE loop (condition checked at START)
int count = 5;
while (count > 0) // condition checked BEFORE each iteration
{
Console.WriteLine(count);
count = count - 1;
}
// Output: 5 4 3 2 1
A REPEAT...UNTIL loop checks its condition after each iteration. This guarantees the body executes at least once, regardless of the condition. It is ideal when you always need to perform the action before you can check whether to stop - for example, asking for user input before you can validate it.
while True: with a break statement at the end of the loop body. You must know the pseudocode pattern for AQA exams.
AQA pseudocode (REPEAT...UNTIL):
REPEAT
answer ← USERINPUT
UNTIL answer = "yes"
# Python — REPEAT...UNTIL workaround (condition checked at END)
# Python has no REPEAT...UNTIL; use while True: with break at the end
while True:
answer = input("Type yes to continue: ")
if answer == "yes": # condition checked AFTER the body runs
break # exits the loop — equivalent to UNTIL answer = "yes"
print("Loop ended.")
' VB.NET — REPEAT...UNTIL (condition checked at END)
' VB.NET supports this directly with Do...Loop Until
Dim answer As String
Do
Console.Write("Type yes to continue: ")
answer = Console.ReadLine() ' body always runs at least once
Loop Until answer = "yes" ' condition checked AFTER body
Console.WriteLine("Loop ended.")
// C# — do...while (condition checked at END)
// C# uses do...while; condition is true to CONTINUE (opposite phrasing to UNTIL)
string answer;
do
{
Console.Write("Type yes to continue: ");
answer = Console.ReadLine(); // body always runs at least once
} while (answer != "yes"); // condition checked AFTER body
Console.WriteLine("Loop ended.");
Key Takeaways
- A definite (count-controlled) loop repeats a fixed, known number of times - use a FOR loop.
- An indefinite (condition-controlled) loop repeats an unknown number of times based on a condition - use WHILE or REPEAT...UNTIL.
- A WHILE loop checks its condition at the start - the body may never execute if the condition is false from the beginning.
- A REPEAT...UNTIL loop checks its condition at the end - the body always executes at least once.
- Python has no REPEAT...UNTIL. Use
while True:withbreakat the end of the body to simulate it. You must still understand the pseudocode pattern for AQA exams. - In Python,
range(1, 6)produces values 1 to 5 - the upper bound is exclusive. In VB.NET and AQA pseudocode, the upper bound is inclusive.