Do While Loops

Iteration in C: For, While and Do-While Loops

Iteration in C means repeating code. The for, while and do-while loops are all iteration statements. Learn each one's syntax with a shared example.

On this page
  1. What iteration means
  2. The for loop
  3. The while loop
  4. The do-while loop
  5. The same task with each loop
  6. Which loop should you use?
  7. Common mistakes
  8. Summary

An example of iteration in C is any loop that repeats a block of code. The for, while and do-while loops are all iteration statements in C, so if a question lists those three as choices, the answer is all of the mentioned. The loop syntax is the same in C and C++, and the examples on this page use C++ with std::cout for output.

What iteration means

Iteration means running the same block of code more than once. Every loop needs a condition that decides whether another pass happens, and something inside the loop that eventually makes that condition false. If nothing does, the loop never ends.

The for loop

for (initialization; condition; update) {
    // statements to repeat
}

The initialization runs once, before the first pass. The condition is checked before each pass, and the update runs after each pass. A for loop is often the clearest choice when you know how many passes you need, because the start, the stop and the step all sit on one line.

The while loop

while (condition) {
    // statements to repeat
}

The condition is checked before each pass. If it is false the first time, the body never runs. A while loop suits cases where the number of passes depends on something that changes inside the loop. The c while loop syntax covers the rules for this form in more depth.

The do-while loop

do {
    // statements to repeat
} while (condition);

The body runs first, and the condition is checked after it. The body always runs at least once. Remember the semicolon after the condition. For more complete do-while programs, the do while loop in c example program includes several worked examples.

The same task with each loop

This example adds the numbers 1 through 5 with each loop type. All three versions print the same result.

Using a for loop

#include <iostream>

int main() {
    int sum = 0;

    for (int i = 1; i <= 5; i++) {
        sum += i;
    }

    std::cout << "Sum: " << sum << std::endl;
    return 0;
}

Using a while loop

#include <iostream>

int main() {
    int sum = 0;
    int i = 1;

    while (i <= 5) {
        sum += i;
        i++;
    }

    std::cout << "Sum: " << sum << std::endl;
    return 0;
}

Using a do-while loop

#include <iostream>

int main() {
    int sum = 0;
    int i = 1;

    do {
        sum += i;
        i++;
    } while (i <= 5);

    std::cout << "Sum: " << sum << std::endl;
    return 0;
}

Output for all three:

Sum: 15

Each version starts the counter at 1 and stops after 5. The for loop keeps the counter setup and update in its header. The while and do-while versions set the counter up before the loop and update it inside the body.

Which loop should you use?

  • Use a for loop when you know how many passes you need.
  • Use a while loop when you want the condition checked before any pass, including the possibility of zero passes.
  • Use a do-while loop when the body must run at least once, such as showing a menu or asking for input.

Any of the three can be written with the others, so the choice is mostly about clarity.

Common mistakes

  • Infinite loops. If the body never changes what the condition checks, the loop never ends. Leaving out i++ in the examples above would cause this.
  • Off-by-one errors. Writing i < 5 instead of i <= 5 in the sum example adds only 1 + 2 + 3 + 4, which gives 10.
  • Missing semicolon. A do-while loop needs a semicolon after while (condition). Without it, the code will not compile.

Summary

Iteration in C means repeating code, and the for, while and do-while loops are the three iteration statements. The for loop puts its setup and update on one line, and the while loop keeps its condition on its own. The do-while loop is the only one of the three that checks its condition after each pass, so it is the one to choose when the body must run at least once.

The complete guide to do while loops

C++ Do While Loop Example Program
Read the guide

Keep exploring

Guides from other topics

All topics →

The sizeof Operator in C++

Learn how the sizeof operator in C++ works: what it returns in bytes, how it handles arrays, pointers and structs, and common mistakes to avoid.

sizeof 6 min read