for Loop in C++
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 << " ";
}| 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.for loop
for loop तब पसंदीदा है जब आपको पता हो कितनी बार दोहराना है, या किसी range में गिनना हो। यह loop के counter के बारे में सब कुछ एक साफ़ line में इकट्ठा करता है।
तीन हिस्से
for (int i = 0; i < 5; i++) {
// ↑ init ↑ cond ↑ update
cout << i << " ";
}| हिस्सा | कब चलता है |
|---|---|
int i = 0 (init) | एक बार, बिल्कुल शुरू में |
i < 5 (condition) | हर pass से पहले; false होने पर loop रुकता है |
i++ (update) | हर pass के बाद |
एक चलता उदाहरण
#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
Array पर loop
Classic for loop array में चलने को index इस्तेमाल करता है:
int a[] = {10, 20, 30};
for (int i = 0; i < 3; i++) {
cout << a[i] << " ";
}10 20 30
Range-based for loop
C++11 से, अगर आप बस हर element चाहते हैं उसका index नहीं, range-based for loop ज़्यादा साफ़ है:
int a[] = {10, 20, 30};
for (int x : a) {
cout << x << " ";
}10 20 30
पढ़ने को for (int x : a) इस्तेमाल करें; elements बदलना हो तो reference के साथ for (int &x : a) इस्तेमाल करें।
for बनाम while
| for तब जब | while तब जब |
|---|---|
| आपको count या range पता हो | आप किसी condition के बदलने तक दोहराएँ |
| Counter, condition, step एक line में बैठें | Passes की संख्या अज्ञात हो |
अन्य loops के लिए while और do-while lesson देखें।
आम गलतियाँ
<की जगह<=इस्तेमाल करना, एक अतिरिक्त pass चलाना (off-by-one)।- Counter को update के साथ-साथ body में भी बदलना, count को गड़बड़ करना।
- भटका semicolon:
for (...);खाली body देता है। - बहुत आगे loop करके array के अंत के पार पढ़ना।
for loop से किसी number की multiplication table print करें। फिर पाँच numbers array में रखें और उन्हें एक बार index loop से और एक बार range-based for से print करें।
सारांश
forloop तब सबसे अच्छा है जब दोहराव की संख्या पता हो।- इसके तीन हिस्से — init, condition और update — सब एक line में।
- Classic for loop index इस्तेमाल करता है; arrays और ranges के लिए बढ़िया।
- Range-based for (
for (int x : a)) हर element को साफ़ तरीके से देखता है। <बनाम<=के साथ off-by-one गलतियों का ध्यान रखें।