Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 17

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

C++
for (int i = 0; i < 5; i++) {
     //  ↑ init    ↑ cond ↑ update
    cout << i << " ";
}
Dry Run — dekhiye har step kaise chalta hai (i = 0 se shuru)
Stepi ki valueConditionKya huai ab
100 < 5 → trueloop body chalta haii → 1
211 < 5 → trueloop body chalta haii → 2
322 < 5 → trueloop body chalta haii → 3
433 < 5 → trueloop body chalta haii → 4
544 < 5 → trueloop body chalta haii → 5
655 < 5 → falseCondition false — loop yahin ruk gaya (STOP)—
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.
← 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.
Next topicwhile and do-while Loops Your progress: 17 / 81 (21%) · Lesson 18 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free