Explain, analyse and justify why one loop structure (for, while or do…while) is the most appropriate choice for a given problem. Use clear pseudocode, state pre‑ and post‑conditions, and discuss algorithmic efficiency where relevant.
| Loop | Typical C‑like Syntax | When the Test Is Evaluated | Guarantees | Typical Use‑Case |
|---|---|---|---|---|
for | for (initialisation; test; update) { | Before each iteration (pre‑condition) | Zero or more executions; loop variable scoped to the loop | Known, fixed number of repetitions (e.g., iterating over an array) |
while | while (test) { | Before each iteration (pre‑condition) | Zero or more executions; body may be skipped entirely | Data‑driven condition where the number of repetitions is not known in advance |
do … while | do { | After each iteration (post‑condition) | At least one execution; body always runs once | Situations that require a mandatory first pass (e.g., menu‑driven programs) |
for loop.Example – Print the first n Fibonacci numbers
for (int i = 1; i <= n; i++) {
// calculate and display Fibonacci(i)
}
while loop.Example – Read values until sentinel -1 is entered
int value = readNext();
while (value != -1) {
// process value
value = readNext();
}
do…while loop.while loop plus the guaranteed first iteration.Example – Simple menu‑driven program
int choice;
do {
displayMenu();
choice = getChoice();
// handle the choice
} while (choice != EXIT);
| Loop | Pre‑condition (what must be true before the first iteration) | Post‑condition (what is true when the loop terminates) |
|---|---|---|
for | Initialisation performed; test evaluates true for the first iteration (if any). | After the final update the test is false; loop variable holds the value that caused termination. |
while | Test evaluated before the first iteration; may be false → body never runs. | Loop exits when the test becomes false. |
do…while | None – the body runs unconditionally once. | Test evaluated after each iteration; loop repeats while the test is true. |
for loops → O(n²)).Example – Multiplication table of size
for (int i = 1; i <= n; i++) { // outer loop – rows
for (int j = 1; j <= n; j++) { // inner loop – columns
print(i * j + "\t");
}
println(); // new line after each row
}
<= instead of < (or vice‑versa) in the test.do…while when the body must not run for an empty data set.array.length‑1.Problem: Find the first occurrence of target in an array list of unknown length.
int i = 0;
while (i < list.length && list[i] != target) {
i++;
}
if (i < list.length) {
// target found at index i
} else {
// target not present
}
while is optimallist.length == 0, the test fails before any body execution, preventing an out‑of‑bounds error.i < list.length AND list[i] != target) is naturally expressed in the while header, keeping the algorithm readable.for loop would obscure the early‑termination nature and require an extra break statement.do…while loop would guarantee one execution, risking an out‑of‑bounds access when the array is empty.| Loop | Ideal Situation | Key Characteristics | Typical Use‑Case | Complexity Note |
|---|---|---|---|---|
for | Known, fixed number of iterations | Initialisation, test, update all in the header; counter scoped to the loop | Iterating over arrays, counting loops, generating sequences | O(n) where n is the bound |
while | Indeterminate repetitions; loop may never run | Pre‑condition test; body may be skipped entirely | Reading input until a sentinel, searching until a match, validating data | O(k) where k ≤ n (depends on data) |
do…while | At least one execution required | Post‑condition test; body always runs once | Menu systems, repeat‑until validation, prompting for correct input | O(k + 1) – same as while plus guaranteed first pass |
forwhiledo…whilefor loops → O(n²)).for, while and do…while loops, highlighting the difference between pre‑condition and post‑condition testing.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.