Loops in C++
A loop is a control structure that repeats a block of code multiple times. Instead of writing the same line 100 times, a loop does it automatically. C++ provides three main types of loops: for, while, and do-while.
The for Loop
The for loop is the most commonly used loop when the number of iterations is known in advance. It combines initialization, condition checking, and updating in one line.
for (initialization; condition; update) {
// code to repeat
}
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Count: " << i << endl;
}
return 0;
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5How it works:
int i = 1— Start with i = 1i <= 5— Keep looping while i is 5 or lessi++— After each loop, add 1 to i
Counting Downward with for
for (int i = 5; i >= 1; i--) {
cout << i << " ";
}
cout << endl;
Output:
5 4 3 2 1The while Loop
The while loop repeats as long as the condition is true. It is best used when the number of repetitions is not known in advance.
while (condition) {
// code to repeat
}
int num = 1;
while (num <= 5) {
cout << num << " ";
num++;
}
Output:
1 2 3 4 5Practical Example — Sum of digits:
int n = 12345;
int sum = 0;
while (n > 0) {
sum += n % 10; // extract last digit
n /= 10; // remove last digit
}
cout << "Sum of digits: " << sum << endl;
Output:
Sum of digits: 15The do-while Loop
The do-while loop is like the while loop, but with one key difference — it always runs at least once, because the condition is checked after the loop body executes.
do {
// code to repeat
} while (condition);
int x = 1;
do {
cout << "x = " << x << endl;
x++;
} while (x <= 3);
Output:
x = 1
x = 2
x = 3When do-while runs even with false condition:
int val = 10;
do {
cout << "This runs once." << endl;
} while (val < 5); // false, but body already ran
Output:
This runs once.Nested Loops
A loop inside another loop is called a nested loop. Each iteration of the outer loop runs the inner loop completely.
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
cout << row << "x" << col << "=" << row*col << " ";
}
cout << endl;
}
Output:
1x1=1 1x2=2 1x3=3
2x1=2 2x2=4 2x3=6
3x1=3 3x2=6 3x3=9Loop Control Statements
break — Exit the loop immediately
for (int i = 1; i <= 10; i++) {
if (i == 5) break;
cout << i << " ";
}
// Output: 1 2 3 4
continue — Skip current iteration and move to next
for (int i = 1; i <= 6; i++) {
if (i == 3) continue;
cout << i << " ";
}
// Output: 1 2 4 5 6
Range-Based for Loop (C++11)
A cleaner way to iterate over arrays or collections:
int numbers[] = {10, 20, 30, 40, 50};
for (int n : numbers) {
cout << n << " ";
}
Output:
10 20 30 40 50Infinite Loops
When the condition of a loop never becomes false, it runs forever. This is sometimes intentional (like a server waiting for requests), but usually a bug.
// Intentional infinite loop example — always provide a break condition!
while (true) {
cout << "Running..." << endl;
break; // without this, it would never stop
}
Comparison of Loops
| Loop | When to Use |
|---|---|
for | Known number of iterations |
while | Unknown iterations, check before entering |
do-while | Must run at least once, then check condition |
Key Takeaways
- Loops repeat code automatically based on a condition.
foris best when the count is known.whilechecks the condition before entering the loop.do-whilealways runs the loop body at least once.breakexits a loop;continueskips to the next iteration.
