C++ Academy · Lesson

Loop Patterns: for, while, do-while

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

Lesson 2 of 413 steps

Loop Patterns: for, while, do-while is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

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

Yes — any 3 lessons of the C++ Academy learning path, including “Loop Patterns: for, while, do-while”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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