What does a loop do?

The key idea

A loop repeats a block of work while a condition permits it. In C, a for loop groups the initialisation, continuation condition and update in one place. A while loop checks its condition before each repetition. A do-while loop checks after the body, so the body runs at least once.

Choose the form that makes the stopping rule easiest to explain. The most important questions are what changes during the loop and why it eventually stops.

Trace a complete example

#include <stdio.h>

int main(void) {
    int values[] = {2, 4, 6};
    size_t count = sizeof values / sizeof values[0];
    int total = 0;
    for (size_t i = 0; i < count; ++i) {
        total += values[i];
    }
    printf("%d\n", total);
    return 0;
}

The output is 12. The three array positions are 0, 1 and 2, so the continuation condition is i < count. When i becomes 3, the condition fails before another element is read.

Index usedValue addedTotal after the addition
022
146
2612

count is calculated from the actual local array. That sizeof expression must not be copied into a function expecting an array parameter and assumed to produce the same result; such a parameter is adjusted to a pointer.

Why do off-by-one errors happen?

An off-by-one error performs one repetition too many or too few. Changing the condition to i <= count would attempt to access past the final element. Stopping at i < count - 1 would omit the last element in this example.

Think in terms of a valid range: start at zero and stop before count. Do not rely on an out-of-bounds read appearing to work during one run; the program no longer has defined behaviour for that access.

Distinguish a total from a counter

To count how many values exceed 3, start a separate count at zero and add one when the condition is true. Do not add the value itself. For 2, 4 and 6, the number of qualifying elements is 2, while their sum is 10.

This change is a useful exercise because the loop can remain similar while the meaning of the accumulated result changes. Name the result to match its purpose.

Check the stopping rule

Before running your loop, answer: what is the first index, what is the last valid index, and what changes after each pass? For a while loop, make sure the body can move the state toward termination.

Practise with a single-element array and with a logical input count of zero in a suitably designed function. The fixed local example above contains three elements. Avoid inventing a zero-length C array as a shortcut to test emptiness.

When you can trace these cases, you are ready to move repeated work into a function and state its length requirements explicitly.

Choose your next step with Gammal Tech

Choose one skill you want to demonstrate, then review the relevant course and its prerequisites. Ask Gammal Tech about the content, study arrangements and current fees before enrolling.

References and course details

Use these references for further study. The examples and exercises in this article are educational; consult the catalogue for course details.

Articles & Guides