Justify why one loop structure may be better suited to solve a problem than the others

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Topic 11.2 Constructs

Objective

Justify why one loop structure may be better suited to solve a problem than the others.

Common Loop Structures

  • for loop – initialisation; test; update in a single line.
  • while loop – test performed before each iteration.
  • do…while loop – test performed after each iteration (guarantees at least one execution).

When to Prefer Each Loop

  1. Known number of repetitions

    If the exact number of iterations is known before the loop starts, a for loop is usually the clearest choice because the control variables are visible in one place.

    Example: printing the first \$n\$ Fibonacci numbers.

    for (int i = 1; i <= n; i++) {

    // body

    }

  2. Indeterminate repetitions, condition‑driven

    When the loop should continue until a condition that depends on data becomes false, a while loop is appropriate because the test is evaluated before each iteration.

    Example: reading input until the sentinel value -1 is entered.

    while (value != -1) {

    // process value

    value = readNext();

    }

  3. At least one execution required

    If the loop body must run at least once regardless of the initial condition, a do…while loop is the natural fit.

    Example: presenting a menu and repeating until the user chooses “Exit”.

    do {

    displayMenu();

    choice = getChoice();

    // handle choice

    } while (choice != EXIT);

Comparison Table

Loop TypeWhen to UseKey CharacteristicsTypical Use‑Case
forKnown, fixed number of iterationsInitialisation, test, and update are in the header; loop variable scoped to loopIterating over arrays, counting loops
whileIndeterminate repetitions; condition may become false before first iterationTest evaluated before each iteration; body may never executeReading input until EOF, searching until a match is found
do…whileAt least one execution required; condition checked after bodyBody always runs once; test after each iterationUser‑driven menus, repeat‑until validation loops

Justification Example: Searching a List

Suppose we need to search a list of unknown length for a target value. The loop must stop as soon as the value is found, but it may also reach the end of the list without finding it.

Using a while loop:

int i = 0;

while (i < list.length && list[i] != target) {

i++;

}

if (i < list.length) {

// target found at index i

}

Why while is better here:

  • The number of iterations is not known in advance; it depends on where (or if) the target appears.
  • The loop may terminate before any iteration if the list is empty (list.length == 0).
  • All termination conditions (i < list.length and list[i] != target) are naturally expressed in the test part of the while header.

Using a for loop would require embedding the same complex condition in the header, which reduces readability, while a do…while loop would guarantee at least one iteration even when the list is empty, leading to an out‑of‑bounds error.

Key Points to Remember

  • Choose the loop that makes the control flow most obvious to a reader.
  • Consider whether the loop must execute at least once.
  • Match the loop structure to the nature of the problem: fixed count, condition‑driven, or post‑condition.
  • A clear loop reduces bugs and simplifies maintenance.

Suggested diagram: Flowchart comparing the entry and exit points of for, while, and do…while loops.