0Pricing
C++ Academy · Lesson

Loop Patterns: for, while, do-while

Iterate with the three classic loop forms and pick the right one for the situation.

Loop Patterns: for, while, do-while is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The for Loop

The classic indexed loop. Use it when you know how many iterations you need.

for (int i = 0; i < 10; ++i) {
    std::cout << i << " ";
}

Anatomy of a for Loop

Three parts separated by semicolons:

  • Init — runs once before the loop
  • Condition — checked before each iteration
  • Update — runs after each iteration

Any part can be empty.

Counting Down

Decrement to iterate in reverse. Watch the boundary carefully.

for (int i = 9; i >= 0; --i) {
    std::cout << i << " ";
}

The while Loop

Loop while a condition is true. Use it when iteration count depends on a runtime state.

int n = 100;
while (n > 1) {
    std::cout << n << " ";
    n /= 2;
}

The do-while Loop

Like while but the body runs at least once — the condition is checked at the end. Useful for input validation.

int n;
do {
    std::cout << "Enter 1-9: ";
    std::cin >> n;
} while (n < 1 || n > 9);

Infinite Loops

Sometimes you want a loop that runs until break. The idiomatic forms:

for (;;) { /* ... */ }      // C-style
while (true) { /* ... */ }   // arguably clearer

break: Exit Early

break stops the innermost loop or switch immediately.

for (int i = 0; i < n; ++i) {
    if (a[i] == target) {
        index = i;
        break;
    }
}

continue: Skip to Next Iteration

continue jumps to the next iteration without running the rest of the body.

for (int i = 1; i <= 100; ++i) {
    if (i % 2 == 0) continue;
    std::cout << i << " ";  // print odd numbers
}

Nested Loops

Loops can nest. Inner-loop break only exits the inner loop — use a flag or refactor into a function for clarity.

Choosing the Right Loop

Quick guide:

  • Known count → for
  • Until a condition flips → while
  • Run at least once → do-while
  • Container iteration → range-based for (next lesson)

Off-by-One Errors

The biggest loop bug is going one too many or too few iterations. Prefer < with size() when indexing arrays — and verify boundaries with paper or tests.

Quick Check

Which loop is guaranteed to execute its body at least once?

Recap

Pick for for known counts, while for condition-driven loops, and do-while when the body must run at least once. Use break and continue sparingly for clarity.

Frequently asked questions

Is the “Loop Patterns: for, while, do-while” lesson free?

Yes — the full text of “Loop Patterns: for, while, do-while” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Loop Patterns: for, while, do-while”?

Iterate with the three classic loop forms and pick the right one for the situation. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Loop Patterns: for, while, do-while” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Branching: if-else, ternary, switch-case
  2. Loop Patterns: for, while, do-while
  3. Range-Based for Loop with Containers
  4. Early Returns break continue and goto
← Back to C++ Academy