🟡 Control Flow  ·  Lesson 17

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

C++
for (int i = 0; i < 5; i++) {
     //  ↑ init    ↑ cond ↑ update
    cout << i << " ";
}
PartWhen 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

C++
#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;
}
Output:
Sum 1 to 5 = 15

Looping over an array

A classic for loop uses the index to walk an array:

C++
int a[] = {10, 20, 30};
for (int i = 0; i < 3; i++) {
    cout << a[i] << " ";
}
Output:
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:

C++
int a[] = {10, 20, 30};
for (int x : a) {
    cout << x << " ";
}
Output:
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 whenUse while when
You know the count or rangeYou repeat until a condition changes
Counter, condition, step fit one lineThe 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.
🏋️ Practice

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 for loop 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++?
A 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?
The three parts are the initialisation (runs once at the start, e.g. 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++?
A range-based for loop, written 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?
Use a 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?
A classic 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 में इकट्ठा करता है।

तीन हिस्से

C++
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 के बाद

एक चलता उदाहरण

C++
#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;
}
Output:
Sum 1 to 5 = 15

Array पर loop

Classic for loop array में चलने को index इस्तेमाल करता है:

C++
int a[] = {10, 20, 30};
for (int i = 0; i < 3; i++) {
    cout << a[i] << " ";
}
Output:
10 20 30

Range-based for loop

C++11 से, अगर आप बस हर element चाहते हैं उसका index नहीं, range-based for loop ज़्यादा साफ़ है:

C++
int a[] = {10, 20, 30};
for (int x : a) {
    cout << x << " ";
}
Output:
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 करें।

सारांश

  • for loop तब सबसे अच्छा है जब दोहराव की संख्या पता हो।
  • इसके तीन हिस्से — init, condition और update — सब एक line में।
  • Classic for loop index इस्तेमाल करता है; arrays और ranges के लिए बढ़िया।
  • Range-based for (for (int x : a)) हर element को साफ़ तरीके से देखता है।
  • < बनाम <= के साथ off-by-one गलतियों का ध्यान रखें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.