for Loop| Subject: Programming Fundamentals | Level: Beginner |
By the end of this lecture, students will be able to:
for loop.for loop to repeat statements.for Loop?A for loop repeats a block of code a specified number of times or while a condition remains true.
Real-life example: A teacher wants to mark attendance for 30 students. Instead of writing the same instructions 30 times, a loop can repeat the task.
A for loop is especially useful when the repetition follows a clear counting pattern.
for Loopfor (initialization; condition; update)
{
// Statements to repeat
}
There are three main parts:
| Part | Purpose | Example |
|---|---|---|
| Initialization | Sets the starting value | int i = 1 |
| Condition | Determines whether to continue | i <= 5 |
| Update | Changes the counter | i++ |
Remember the order: Initialize → Check condition → Execute body → Update → Check again.
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
cout << i << endl;
}
return 0;
}
Output:
1
2
3
4
5
int i = 1 starts the counter at 1.i <= 5 allows the loop to run while i is 5 or less.i++ increases i by 1 after each repetition.i becomes 6, the condition is false and the loop stops.#include <iostream>
using namespace std;
int main()
{
for (int i = 10; i >= 1; i--)
{
cout << i << " ";
}
return 0;
}
Output:
10 9 8 7 6 5 4 3 2 1
int i = 10 starts from 10.i >= 1 continues the loop until the counter becomes less than 1.i-- decreases the counter by 1.#include <iostream>
using namespace std;
int main()
{
for (int i = 2; i <= 20; i = i + 2)
{
cout << i << " ";
}
return 0;
}
Output:
2 4 6 8 10 12 14 16 18 20
The counter starts at 2 and increases by 2 each time. Therefore, only even numbers are displayed.
i = i + 2 means the new value of i is its previous value plus 2.
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
for (int i = 1; i <= 10; i++)
{
sum = sum + i;
}
cout << "Sum = " << sum;
return 0;
}
Output:
Sum = 55
int sum = 0 initializes the total.sum = sum + i adds each number to the running total.For example:
i |
Calculation | sum |
|---|---|---|
| 1 | 0 + 1 | 1 |
| 2 | 1 + 2 | 3 |
| 3 | 3 + 3 | 6 |
| 4 | 6 + 4 | 10 |
| … | … | … |
| 10 | 45 + 10 | 55 |
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a number: ";
cin >> n;
for (int i = 1; i <= 10; i++)
{
cout << n << " x " << i
<< " = " << n * i << endl;
}
return 0;
}
Sample output:
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
cin >> n receives a number from the user.n * i calculates each multiplication result.endl moves the output to a new line.#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter the ending number: ";
cin >> n;
for (int i = 1; i <= n; i++)
{
cout << i << " ";
}
return 0;
}
Sample output:
Enter the ending number: 7
1 2 3 4 5 6 7
The user decides how many numbers to print. If the user enters 7, the loop prints 1 through 7.
for Loops — Star PatternA nested loop is a loop placed inside another loop.
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 5; j++)
{
cout << "* ";
}
cout << endl;
}
return 0;
}
Output:
* * * * *
* * * * *
* * * * *
* * * * *
cout << endl; starts a new row.Try changing the program to produce these patterns.
Pattern A — Right-angle triangle
*
* *
* * *
* * * *
* * * * *
Pattern B — Increasing numbers
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Pattern C — Rectangle of numbers
1 2 3 4
1 2 3 4
1 2 3 4
while, do-while, and for| Feature | while |
do-while |
for |
|---|---|---|---|
| Condition checked | Before the body | After the body | Before each iteration |
| Minimum executions | Zero | One | Zero |
| Initialization and update | Usually separate | Usually separate | Written in the loop header |
| Common use | Condition-controlled repetition | Menus that run at least once | Counting and sequences |
All three loops can solve many of the same problems. Choose the loop that makes the program easiest to understand.
Mistake 1: Using the wrong condition
for (int i = 1; i >= 5; i++)
{
cout << i;
}
The condition is false at the beginning, so the body does not execute.
Mistake 2: Forgetting the update
for (int i = 1; i <= 5; )
{
cout << i;
}
The counter never changes, so the condition stays true and the loop does not end normally.
Mistake 3: Adding an unnecessary semicolon
for (int i = 1; i <= 5; i++);
{
cout << "Hello";
}
The semicolon creates an empty loop. The block after it is not the loop body.
Solve these tasks independently, without copying the examples.
Level 1 — Basic
Level 2 — Intermediate
n, where n is entered by the user.Level 3 — Problem Solving
1. Which part of a `for` loop changes the counter?
Initialization
Condition
Update
2. How many times does this loop run: `for (int i = 1; i <= 5; i++)`?
4 times
5 times
6 times
3. Which loop executes its body at least once?
while
do-while
for
4. In a nested loop, which loop controls the rows in our star-pattern example?
Outer loop
Inner loop
Both always control the same thing
Check Answers
for loop is useful for repeating statements, especially when counting.