What You’ll Learn
By the end of this post, you’ll understand:
- What loops are and why they are useful
- The differences between
while
,for
, anddo-while
loops - How to control loop behavior with
break
andcontinue
- Common patterns like counting, summing, and nested loops
Why 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.
Example (without loop):
cout << "Hello" << endl;
cout << "Hello" << endl;
cout << "Hello" << endl;
With a loop:
for (int i = 0; i < 3; i++) {
cout << "Hello" << endl;
}
The while
Loop
The while
loop checks the condition before each iteration.
Syntax:
while (condition) {
// code block
}
Example:
int i = 1;
while (i <= 5) {
cout << i << endl;
i++;
}
Output:
1
2
3
4
5
The for
Loop
A for
loop is often used when the number of iterations is known.
Syntax:
for (initialization; condition; update) {
// code block
}
Example:
for (int i = 1; i <= 5; i++) {
cout << "Number: " << i << endl;
}
This does exactly the same thing as the while
example above but is more compact.
The do-while
Loop
This loop runs the code block at least once, then checks the condition.
Syntax:
do {
// code block
} while (condition);
Example:
int i = 1;
do {
cout << i << endl;
i++;
} while (i <= 5);
Example: Sum of First N Numbers
int n;
cout << "Enter a number: ";
cin >> n;
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
cout << "Sum: " << sum << endl;
Try this with n = 5
. You’ll get 1 + 2 + 3 + 4 + 5 = 15
.
break
and continue
break
: Exits the loop immediately.
for (int i = 1; i <= 10; i++) {
if (i == 5) break;
cout << i << " ";
}
// Output: 1 2 3 4
continue
: Skips the current iteration.
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
cout << i << " ";
}
// Output: 1 2 4 5
Nested Loops
You can put one loop inside another. This is useful for 2D problems like grids or tables.
Example: Multiplication Table
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
cout << i * j << "\t";
}
cout << endl;
}
Common Loop Patterns
1. Counting:
for (int i = 0; i < 10; i++) {
cout << i << " ";
}
2. Sum of Even Numbers:
int sum = 0;
for (int i = 2; i <= 100; i += 2) {
sum += i;
}
cout << sum;
3. Reverse Loop:
for (int i = 10; i >= 1; i--) {
cout << i << " ";
}
Quiz: Loops
Question 1/7
Mini Exercises
- Print numbers from 1 to 20 using a
while
loop. - Find the factorial of a number using a
for
loop. - Use a
do-while
loop to ask the user for a number until they enter a positive number. - Create a pattern like this using nested loops:
*
**
***
****
Common Mistakes to Avoid
- Forgetting to update the loop variable (can lead to infinite loops)
- Off-by-one errors (using
<=
when it should be<
, or vice versa) - Misplacing braces — always use
{}
even for one-line loops if you’re new
Summary
In this post, you learned:
- How to use
while
,for
, anddo-while
loops in C++ - The purpose of
break
andcontinue
- Loop patterns like counting, summing, and nested loops
- How to write clean, structured loops that avoid common pitfalls
What’s Next?
In the next post, we’ll explore Arrays and Vectors — how to store and work with lists of data.