3.2.2 - Count-controlled iteration (C#)

3.2.2 - Count-controlled iteration (C#)

Repeat a known number of operations and trace how a counter and running total change. C# written tasks have no Run. C# execution is unavailable here; the runnable activity is an optional Python demonstration.

Choosing the repetition count

Count-controlled iteration repeats a body of statements a specified number of times, avoiding a separate copy of the same instructions for each repetition.

Iteration

Repetition of a group of instructions. Count-controlled iteration uses a known repetition count.

C#
for (int number = 1; number <= 5; number = number + 1)
{
    Console.WriteLine(number);
}

The for header contains three jobs separated by semicolons. Initialisation sets number to 1 once. The condition is tested before each pass. The update adds 1 after each pass. The body therefore displays 1, 2, 3, 4 and 5. After the update from 5 to 6, the condition is false and repetition ends. There are five passes; changing <= to < would give only four.

The counter declared inside this C# header is accessible in the loop, but not after it. In an AQA supplied FOR number ← 1 TO 5, the endpoint is inclusive and the counter never runs beyond that endpoint or exists afterwards. The output is the same; the specified internal counter semantics differ. Do not invent a final counter value for an AQA trace.

Starting at 0 and testing number < 5 also gives five passes, but displays 0–4. Starting value, condition and update must be chosen together. For a countdown, use a decreasing update, for example start at 5, continue while number >= 1, and subtract 1. Iteration combines with sequence inside the body and selection for decisions within each pass.

Tracing counters and accumulators

An accumulator keeps a running result while a loop's counter identifies the current repetition.

C#
int total = 0;
for (int number = 1; number <= 4; number++)
{
    total = total + number;
}
Console.WriteLine(total);

number++ increases the counter by 1. The total starts at 0 before the loop, because no values have yet been added. Each pass uses the old total on the right and stores the updated total on the left.

Counter during bodyTotal before additionTotal after addition
101
213
336
4610

The final output is 10. Initialising total inside the body would reset it on every pass and lose previous additions. Output inside the body would display four intermediate totals rather than one final total. The braces determine which instructions repeat.

Predict the four running totals in the optional Python demonstration, then run it. Change the upper range endpoint from 5 to 6 and rerun; Python excludes that endpoint, so the edit adds the value 5 to the accumulation.

Optional Python demonstration: running totals

{"code":"total = 0\nfor number in range(1, 5):\n total = total + number\n print(number, total)","instructions":"Predict the four rows and run. Change range(1, 5) to range(1, 6), predict the extra row, then run again."}