Loops and Iteration in C++: while, for, do-while
Learn how to make your programs repeat tasks efficiently using while, for, and do-while loops. Master break, continue, and common loop patterns.
- What loops are and why they are essential
- How to use while, for, and do-while loops
- How to control loops with break and continue
- Common patterns: counting, summing, and nested loops
- How to avoid infinite loops and off-by-one errors
1Why use loops?
Loops allow you to repeat a block of code multiple times. Instead of writing the same code again and again, you let the loop do the repetition for you.
Without a loop:
cout << "Hello" << endl; cout << "Hello" << endl; cout << "Hello" << endl;
With a loop:
for (int i = 0; i < 3; i++) { cout << "Hello" << endl; }
Loops are one of the most powerful tools in programming. They let you:
- Process lists of data (arrays, vectors, files)
- Repeat an action a specific number of times
- Keep running until a condition changes (like user input)
- Build complex patterns and structures
2The while loop
The while loop checks the condition before each iteration. If the condition is false initially, the loop body never runs.
Syntax:
while (condition) { // code block }
Example – printing numbers 1 to 5:
int i = 1; while (i <= 5) { cout << i << endl; i++; }
i++), the condition will always be true and the loop will run forever. Always ensure the condition eventually becomes false.
3The for loop
A for loop is ideal when the number of iterations is known in advance. It combines initialization, condition, and update in one line.
Syntax:
for (initialization; condition; update) { // code block }
Example – printing numbers 1 to 5:
for (int i = 1; i <= 5; i++) { cout << "Number: " << i << endl; }
The for loop does exactly the same thing as the while example but is more compact and is the preferred style when you know exactly how many times to iterate.
4The do-while loop
The do-while loop checks the condition after each iteration. This guarantees that the loop body runs at least once.
Syntax:
do { // code block } while (condition);
Example – printing numbers 1 to 5:
int i = 1; do { cout << i << endl; i++; } while (i <= 5);
do-while when you need the loop to run at least once regardless of the condition. A classic example is a menu that must display at least once before asking the user to exit.
5Example: Sum of first N numbers
Here is a practical example that calculates the sum of numbers from 1 to N using a for loop.
int n; cout << "Enter a number: "; cin >> n; int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } cout << "Sum: " << sum << endl;
This code adds 1 + 2 + 3 + 4 + 5 = 15. The sum += i shorthand is equivalent to sum = sum + i.
6break and continue
break exits the loop immediately, even if the condition is still true. continue skips the current iteration and jumps to the next one.
break example
for (int i = 1; i <= 10; i++) { if (i == 5) break; cout << i << " "; }
continue example
for (int i = 1; i <= 5; i++) { if (i == 3) continue; cout << i << " "; }
7Nested loops
You can put one loop inside another. This is useful for working with grids, tables, or any problem that requires two dimensions.
Example – multiplication table (5x5):
for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { cout << i * j << "\t"; } cout << endl; }
8Common loop patterns
1. Counting from 0 to N-1
for (int i = 0; i < 10; i++) { cout << i << " "; }
2. Sum of even numbers (2 to 100)
int sum = 0; for (int i = 2; i <= 100; i += 2) { sum += i; } cout << sum;
3. Reverse loop (10 down to 1)
for (int i = 10; i >= 1; i--) { cout << i << " "; }
9Common mistakes to avoid
<= when you should use < (or vice versa). For example, i < 10 runs 10 times (0–9), while i <= 10 runs 11 times (0–10).
10Mini exercises
- Print numbers from 1 to 20 using a
whileloop. - Find the factorial of a number using a
forloop. (Factorial of 5 = 5 × 4 × 3 × 2 × 1 = 120) - Use a
do-whileloop to ask the user for a number until they enter a positive number. - Create a pattern like this using nested loops:
* ** *** ****
Hint: For the pattern, the outer loop controls rows, and the inner loop controls stars in each row.
