3.2.2 - Condition-controlled iteration (C#)

3.2.2 - Condition-controlled iteration (C#)

Choose a loop when the number of repetitions depends on a condition, and explain when it will stop. Required answers use native C#; execution is unavailable here, with an optional Python demonstration for exploration.

Testing before repetition

A start-tested loop checks whether repetition is needed before executing its body, so the body may run zero times.

C#
int stock = 3;
while (stock > 0)
{
    Console.WriteLine(stock);
    stock = stock - 1;
}

C# while continues while the condition is true. It tests stock > 0, displays 3 and subtracts 1. It then tests again, producing 2 and 1 before stock reaches 0. At 0, it skips the body and leaves the loop. With a starting stock of 0, no output occurs.

Condition-controlled iteration is sometimes called indefinite iteration because the number of passes is determined by changing data rather than a fixed counter limit. For example, reading guesses until a correct answer may take one pass or many. AQA WHILE also tests before the body and continues while its condition is true.

A precondition decides whether the body may run. Test the initial condition as well as the conditions after updates.

Testing after repetition

An end-tested loop executes its body before checking the continuation or stopping condition, so it always runs at least once.

C#
string answer;
do
{
    Console.Write("Type ready: ");
    answer = Console.ReadLine();
}
while (answer != "ready");

C# do ... while continues when its final condition is true. If the first input is ready, the body still runs once, then the false condition ends the loop. If the input is wait, the true condition causes another pass. The semicolon terminates the do ... while statement.

AQA REPEAT ... UNTIL answer = "ready" expresses the same stopping rule with the opposite condition: it stops when the UNTIL condition is true. AQA DO ... WHILE answer ≠ "ready" repeats while the final condition is true. C# has no native until keyword; invert the stopping condition to use do ... while. The placement of the condition is as important as its truth meaning.

Updating the condition and terminating

A useful loop changes or rereads the data controlling its condition so that it can eventually stop.

In the stock example, subtracting 1 moves a positive stock towards 0. If that update were absent, the loop would keep displaying 3. In the input example, rereading answer allows a later input to change the condition; testing the same rejected answer forever would not help the user.

When arguing that a loop is correct, identify its starting state, which work each pass performs, how the controlling data changes, and which state exits. An input loop may rely on the user eventually supplying an acceptable value; it cannot guarantee that they will do so. Some programs therefore also impose an attempt limit.

Predict the optional Python demonstration with inputs wait, then ready. Run it, then replace the target "ready" by "go" and rerun with ready, then go. Python uses a loop and exit test to demonstrate end-tested behaviour, not C# syntax.

Optional Python demonstration: stopping after input

{"code":"while True:\n answer = input("Command: ")\n print("Read", answer)\n if answer == "ready":\n break\nprint("Finished")","instructions":"Run with wait, then ready. Change the target string ready to go and run with ready, then go."}