A for loop is a count-controlled loop in C++ (and many other programming languages) that repeats a block of code a specific number of times.

  • Why use a for loop?

    • When you know exactly how many times the code should repeat.

    • It keeps the code organized, clean, and easy to read.

    • Reduces errors compared to writing the same code multiple times.

  • Difference from while loop:

    • While loop is condition-controlled → may run 0 or many times.

    • For loop is count-controlled → usually runs a fixed number of times.

2. For Loop Syntax 

 
for(initialization; condition; update) {
// statements

}

Step-by-Step Explanation:

  1. Initialization

    • Sets the starting value of the loop control variable (LCV).

    • Executed only once at the beginning.

    • Example: int i = 0; → starts counting from 0.

  2. Condition

    • Checked before each iteration.

    • If true → loop executes.

    • If false → loop stops.

    • Example: i < 10 → runs as long as i is less than 10.

  3. Update

    • Changes the LCV after each iteration.

    • Usually i++ (increment by 1) or i-- (decrement by 1).

    • Can use custom updates like i += 2.

  4. Statements inside the loop

    • Executes for each iteration.

    • Can be one or more statements.

3. Loop Control Variable (LCV)

  • Definition: A variable that controls how many times the loop executes.

  • Usually declared in the initialization part of the for loop.

  • Characteristics:

    1. Sets the starting value.

    2. Checked against condition.

    3. Updated in each iteration.

    4. Proper update is necessary to avoid infinite loops.

Example:

 
for(int i = 1; i <= 5; i++) {
cout << i << " ";
}
  • Here, i is the LCV.

  • It starts at 1, runs until i <= 5, and increments by 1 each iteration.

4. Step-by-Step Execution Table

Example Code:

 
for(int i = 1; i <= 5; i++) {
cout << i << " ";
}
Iterationi valueCondition (i <= 5)ActionUpdate i
11TruePrint 1i=2
22TruePrint 2i=3
33TruePrint 3i=4
44TruePrint 4i=5
55TruePrint 5i=6
66FalseStop

Observation: Loop executes 5 times, exactly as controlled by the LCV.

5. Variations of For Loop

a) Incrementing Loop

 
for(int i = 0; i < 10; i++) {
cout << i << " ";
}
  • Starts at 0, increments by 1 each time.

  • Prints 0 1 2 3 4 5 6 7 8 9.

b) Decrementing Loop

 
for(int i = 5; i > 0; i--) {
cout << i << " ";
}
  • Starts at 5, decrements by 1 each time.

  • Prints 5 4 3 2 1.

c) Custom Update

 
for(int i = 0; i <= 10; i += 2) {
cout << i << " ";
}
  • Increments by 2 each iteration.

  • Prints 0 2 4 6 8 10.

d) Multiple Loop Control Variables

 
for(int i = 0, j = 10; i <= 5; i++, j--) {
cout << "(" << i << "," << j << ") ";
}
  • Prints (0,10) (1,9) (2,8) (3,7) (4,6) (5,5)

  • Shows two LCVs updating together.

6. Nested For Loops

  • A for loop inside another for loop is called a nested for loop.

  • Useful for patterns, tables, and 2D structures.

Example: Multiplication Table

 
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
cout << i*j << " ";
}
cout << endl;
}

Output:

 
1 2 3
2 4 6
3 6 9

Explanation:

  • Outer loop controls rows (i).

  • Inner loop controls columns (j).

  • LCVs are independent.

7. Common Mistakes

  1. Infinite loop

 
for(int i = 1; i <= 5;) {
cout << i << " ";
}
// ❌ i not updated → infinite loop
  1. Loop never executes

 
for(int i = 10; i <= 5; i++) {
cout << i << " ";
}
// ❌ Condition false initially
  1. Wrong type for LCV

 
for(char i = 1; i <= 5; i++) {
cout << i << " ";
}
// ✅ Works but may not be ideal

8. Practical Applications

  1. Printing numbers 1 to 100

  2. Summation of numbers

 
int sum = 0;
for(int i = 1; i <= 10; i++) {
sum += i;
}
cout << sum;
  • Prints 55 → sum of numbers 1 to 10

  1. Array iteration

 
int arr[] = {10,20,30,40};
for(int i = 0; i < 4; i++) {
cout << arr[i] << " ";
}
  • Prints 10  20  30  40

  1. Patterns

 
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
cout << "*";
}
cout << endl;
}

Output:

 
*
**
***
****
*****