for Loop in C++
In this lesson: Learn the for loop and range-based for in C++ with examples, dry run and output. In Hindi & English.
The for loop
A for loop is the go-to loop when you know how many times to repeat, or when counting through a range. It gathers everything about the loop's counter into one tidy line.
The three parts
for (int i = 0; i < 5; i++) {
// ↑ init ↑ cond ↑ update
cout << i << " ";
}| Step | i ki value | Condition | Kya hua | i ab |
|---|---|---|---|---|
| 1 | 0 | 0 < 5 → true | loop body chalta hai | i → 1 |
| 2 | 1 | 1 < 5 → true | loop body chalta hai | i → 2 |
| 3 | 2 | 2 < 5 → true | loop body chalta hai | i → 3 |
| 4 | 3 | 3 < 5 → true | loop body chalta hai | i → 4 |
| 5 | 4 | 4 < 5 → true | loop body chalta hai | i → 5 |
| 6 | 5 | 5 < 5 → false | Condition false — loop yahin ruk gaya (STOP) | — |
| Part | When it runs |
|---|---|
int i = 0 (init) | Once, at the very start |
i < 5 (condition) | Before each pass; loop stops when false |
i++ (update) | After each pass |
A working example
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
}
cout << "Sum 1 to 5 = " << sum << "\n";
return 0;
}Sum 1 to 5 = 15
Looping over an array
A classic for loop uses the index to walk an array:
int a[] = {10, 20, 30};
for (int i = 0; i < 3; i++) {
cout << a[i] << " ";
}10 20 30
Range-based for loop
Since C++11, if you just want each element and not its index, the range-based for loop is cleaner:
int a[] = {10, 20, 30};
for (int x : a) {
cout << x << " ";
}10 20 30
Use for (int x : a) to read; use for (int &x : a) with a reference if you want to modify the elements.
for vs while
| Use for when | Use while when |
|---|---|
| You know the count or range | You repeat until a condition changes |
| Counter, condition, step fit one line | The number of passes is unknown |
See the while and do-while lesson for the other loops.
Common mistakes
- Using
<=where you meant<, running one extra pass (off-by-one). - Changing the counter inside the body as well as in the update, confusing the count.
- A stray semicolon:
for (...);gives an empty body. - Reading past the end of an array by looping too far.
Print the multiplication table of a number using a for loop. Then store five numbers in an array and print them once with an index loop and once with a range-based for.
Summary
- A
forloop is best when the number of repeats is known. - Its three parts are init, condition and update, all on one line.
- A classic for loop uses an index; great for arrays and ranges.
- The range-based for (
for (int x : a)) visits each element cleanly. - Watch off-by-one errors with
<vs<=.
Frequently Asked Questions
What is a for loop in C++?
for loop repeats a block of code a known number of times. It packs three parts into one line — initialisation, a condition, and an update — such as for (int i = 0; i < 5; i++), which runs the body five times while i goes from 0 to 4.What are the three parts of a for loop?
int i = 0), the condition (checked before each pass, e.g. i < n), and the update (runs after each pass, e.g. i++). Together they control how many times the loop runs.What is a range-based for loop in C++?
for (int x : container), visits each element of an array, vector or other container in turn without needing an index. Introduced in C++11, it is shorter and safer than a counting loop when you just want every element.When should I use a for loop instead of a while loop?
for loop when you know how many times to repeat or are counting through a range, because the counter, condition and step sit neatly together. A while loop suits cases where you repeat until a condition changes and the count is not known in advance.What is the difference between for and range-based for?
for loop uses an index and gives you the position of each element, which is useful when you need the index or want to skip or step. A range-based for loop hides the index and simply gives each element, which is cleaner when the index is not needed.