Loops in C++: For, While and Do-While
Imagine you’re practicing free throws in basketball. You don’t take just one shot, you repeat the motion again and again: 1, 2, 3… maybe 50 times.
Repeating an action is part of learning, and computers also repeat actions using loops.
In this lesson, you’ll teach C++ how to repeat tasks automatically, counting, adding numbers, printing patterns, without writing the same line again and again.
Simple Counting Machine
You’ll build a small program that prints a counting sequence from 1 to 10.
Here’s what it should look like:
1 2 3 4======================== COUNTING PROGRAM 1 2 3 4 5 6 7 8 9 10 ========================
Instead of writing ten print statements, you’ll use a loop to make C++ do the work for you.
Why Loops Important
Loops repeat actions automatically.
They save time, reduce code, and allow programs to handle large tasks easily.
The for Loop
Use a for loop when you know exactly how many times you want to repeat an action.
Perfect for counting, fixed repetitions, and printing sequences.
Example
1 2 3for (int i = 1; i <= 10; i++) { cout << i << " "; }
Explanation:
Starts at 1, repeats until 10, increasing by 1 each time.
The while Loop
Use while when you want to repeat something as long as a condition is true.
You don't always know how many repetitions you’ll need.
Example
1 2 3 4 5int i = 1; while (i <= 10) { cout << i << " "; i++; }
Explanation:
Keeps printing numbers while i is less than or equal to 10.
The do-while Loop
A do-while loop runs at least once, even if the condition is false.
Useful for menus and user-driven programs.
Example
1 2 3 4 5int i = 1; do { cout << i << " "; i++; } while (i <= 10);
Explanation:
Code inside do { } runs first, then the condition is checked.
break
Stops the loop immediately. Useful when you want to exit early.
1 2 3 4for (int i = 1; i <= 10; i++) { if (i == 5) break; cout << i << " "; }
Explanation:
Stops printing when i reaches 5.
continue
Skips one loop step and moves to the next.
1 2 3 4for (int i = 1; i <= 10; i++) { if (i == 5) continue; cout << i << " "; }
Explanation:
Prints all numbers except 5.
Counting
Loops can generate sequences like 1 to 10 or 10 to 1.
Summation
Add a series of numbers easily using loops.
Example
1 2 3 4int sum = 0; for (int i = 1; i <= 5; i++) { sum += i; }
Patterns
Loops can print shapes, lines, triangles, grids.
Simple star pattern
1 2 3for (int i = 1; i <= 5; i++) { cout << "* "; }
Let’s Build It: Counting Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14#include <iostream> using namespace std; int main() { cout << "========================" << endl; cout << " COUNTING PROGRAM" << endl; for (int i = 1; i <= 10; i++) { cout << i << " "; } cout << endl << "========================" << endl; return 0; }
How It Works
- The for loop starts at 1 and repeats until 10.
- Each iteration prints the current value of i.
- The output appears in a single clean line.
You can test this instantly using the DevsCall AI Code Runner.
Learn Together with AI
Try giving your AI Copilot commands like:
- “Create a loop in C++ that prints numbers from 1 to 50.”
- “Show me how to use while loops for summation.”
- “Write a pattern that prints a 5×5 square of stars.”
AI can rewrite, fix, or expand your loops instantly.
Practice Time
Write a C++ program that prints this pattern using a for loop:
1 2 3 4======================== STAR LINE * * * * * * * * * * ========================
Your program must:
- Use a loop, not ten print statements.
- Print exactly 10 stars with spaces.
- Format the output exactly as shown.
If you need help, ask AI:
“How do I print stars using a loop in C++?”
Frequently Asked Questions
You’ll learn how to repeat actions in C++ using for, while, and do-while loops, along with break, continue, and common loop patterns.
Use a for loop when you know exactly how many times the loop should run—like counting from 1 to 10 or printing stars.
A do-while loop always runs at least once before checking the condition, making it ideal for menus or repeat-until-correct scenarios.
The break statement immediately stops the loop, useful when you want to exit early—like stopping a search after finding a result.
Yes. You can run all loop-based programs in the DevsCall AI Code Runner without installing any IDE.
Still have questions?Contact our support team