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<=.
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 गलतियों का ध्यान रखें।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में for loop क्या है?
for loop एक code block को ज्ञात संख्या में दोहराता है। यह तीन हिस्सों को एक line में रखता है — initialisation, condition, और update — जैसे for (int i = 0; i < 5; i++), जो i के 0 से 4 तक जाते हुए body को पाँच बार चलाता है।for loop के तीन हिस्से क्या हैं?
int i = 0), condition (हर pass से पहले जाँचा जाता है, जैसे i < n), और update (हर pass के बाद चलता है, जैसे i++)। मिलकर ये तय करते हैं कि loop कितनी बार चले।C++ में range-based for loop क्या है?
for (int x : container) लिखा जाता है, किसी array, vector या अन्य container के हर element को बारी-बारी देखता है बिना index की ज़रूरत के। C++11 में आया, यह गिनने वाले loop से छोटा और सुरक्षित है जब आप बस हर element चाहते हैं।while loop के बजाय for loop कब इस्तेमाल करूँ?
for loop तब इस्तेमाल करें जब पता हो कितनी बार दोहराना है या किसी range में गिन रहे हों, क्योंकि counter, condition और step साफ़-साफ़ साथ बैठते हैं। while loop उन मामलों के लिए है जहाँ किसी condition के बदलने तक दोहराते हैं और गिनती पहले से पता नहीं।for और range-based for में क्या अंतर है?
for loop index इस्तेमाल करता है और हर element की स्थिति देता है, जो तब उपयोगी है जब आपको index चाहिए या skip/step करना हो। Range-based for loop index छिपाता है और बस हर element देता है, जो तब साफ़ है जब index की ज़रूरत न हो।