C++ PROGRAMMING · 8 MIN STUDY

Repeating work with loops

A loop repeats a body while its continuation condition permits another iteration.

Learning objectives

  • Explain the initialization, condition, body, and iteration expression of a for loop.
  • Choose while when repetition depends on a changing condition.
  • Explain why a do-while body executes at least once.
  • Identify a loop that cannot make progress toward termination.

Prerequisites: variables, comparisons, if statements, and tracing statements one at a time. Review Conditions first.

Counted repetition with for

A for loop is useful when the iteration pattern has a clear starting state, continuation condition, and update. The initialization runs once; the condition is checked before each body execution; the iteration expression runs after the body (including after a continue).

Complete C++17 program
#include <iostream>

int main() {
    int total{0};

    for (int number{1}; number <= 4; ++number) {
        total += number;
    }

    std::cout << total << '\n';
}
Expected output 10
Iterationnumbertotal after body
111
223
336
4410

Condition-controlled repetition with while

A while loop checks its condition before entering the body. It is useful when the number of repetitions is not known in advance, such as reading until a sentinel value or retrying while input is invalid.

Complete C++17 program — while
#include <iostream>

int main() {
    int remaining{3};

    while (remaining > 0) {
        std::cout << remaining << ' ';
        --remaining;
    }
    std::cout << "Go!\n";
}
Expected output 3 2 1 Go!

At-least-once repetition with do while

A do-while loop checks its condition after the body, so the body runs at least once—even if the condition is initially false. The semicolon after the condition is required.

int attempts{0};
do {
    ++attempts;
} while (attempts < 0);
// attempts is 1, not 0

Termination and common mistakes

  • Make sure some state used by the condition changes so the condition can eventually become false.
  • Check < versus <=; they often differ by one iteration.
  • Do not update an unrelated variable while leaving the loop condition unchanged.
  • A loop with an empty condition in a for statement has no condition test and needs another deliberate exit.

Practice

  1. How many times does for (int i = 2; i < 6; ++i) run?
  2. What does the while example print if remaining starts at zero?
  3. Why does the do-while example set attempts to 1?
  4. Identify the bug: int i=0; while (i<3) { std::cout << i; }.

Worked answers

Show reasoned answers
  1. Four times: i is 2, 3, 4, and 5.
  2. It skips the body and prints only Go!.
  3. The condition is checked after the body, and the body increments attempts once before the test.
  4. i is never changed, so the condition remains true and the loop does not terminate normally.

Summary

Use for for a clear counted pattern, while for a condition checked before each iteration, and do-while when the body must run at least once. Always identify the state that makes the loop stop.

References