The for loop is used to repeat a block of code multiple times. It is especially useful when we know how many times we want to execute a block of statements.
A for loop repeatedly executes a block of code while a specified condition remains true.
for (initialization; condition; update) {
// statements
}
The loop continues until the condition becomes false.
for (initialization; condition; update) {
// code to repeat
}
The three parts are separated by semicolons.
for (int i = 1; i <= 5; i++) {
std::cout << i << std::endl;
}
Output:
1
2
3
4
5
The initialization part runs once before the loop starts.
for (int i = 1; i <= 5; i++) {
std::cout << i;
}
Here, int i = 1 initializes the loop variable with 1.
The condition is checked before each iteration.
for (int i = 1; i <= 5; i++) {
std::cout << i;
}
The loop continues while i <= 5 is true.
The update part changes the loop variable after each iteration.
for (int i = 1; i <= 5; i++) {
std::cout << i;
}
Here, i++ increases i by 1 after every
iteration.
For this loop:
for (int i = 1; i <= 3; i++) {
std::cout << i;
}
The execution is:
i is initialized to 1.i <= 3 is checked.i++ increases the value.#include <iostream>
int main() {
for (int i = 1; i <= 10; i++) {
std::cout << i << std::endl;
}
return 0;
}
The decrement operator can be used to count backward.
for (int i = 10; i >= 1; i--) {
std::cout << i << std::endl;
}
The output starts from 10 and ends at 1.
for (int i = 2; i <= 20; i += 2) {
std::cout << i << std::endl;
}
The update expression i += 2 increases the value by 2,
so only even numbers are printed.
for (int i = 1; i <= 19; i += 2) {
std::cout << i << std::endl;
}
The loop starts from 1 and increases by 2.
The i++ operator increases the loop variable by 1.
for (int i = 1; i <= 5; i++) {
std::cout << i;
}
This is one of the most common forms of a for loop.
The i-- operator decreases the loop variable by 1.
for (int i = 5; i >= 1; i--) {
std::cout << i << std::endl;
}
for (int i = 0; i <= 10; i += 2) {
std::cout << i << std::endl;
}
This loop prints values with a difference of 2.
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
std::cout << "Sum = " << sum;
The loop adds the numbers from 1 to 10 to the sum
variable.
int number = 5;
for (int i = 1; i <= 10; i++) {
std::cout << number << " x "
<< i << " = "
<< number * i
<< std::endl;
}
This program prints the multiplication table of 5.
#include <iostream>
int main() {
int n;
std::cout << "Enter a number: ";
std::cin >> n;
for (int i = 1; i <= n; i++) {
std::cout << i << std::endl;
}
return 0;
}
The number of iterations depends on the value entered by the user.
int number = 5;
int factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
std::cout << "Factorial = " << factorial;
The factorial of 5 is 120.
#include <iostream>
#include <string>
int main() {
std::string name = "SOOPRO";
for (int i = 0; i < name.length(); i++) {
std::cout << name[i] << std::endl;
}
return 0;
}
The loop accesses each character using its index.
int numbers[] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << std::endl;
}
The loop uses the index to access each element of the array.
A for loop can contain an if statement.
for (int i = 1; i <= 20; i++) {
if (i % 2 == 0) {
std::cout << i << std::endl;
}
}
This prints only even numbers between 1 and 20.
The break statement immediately terminates the loop.
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
std::cout << i << std::endl;
}
The loop stops when i becomes 5.
The continue statement skips the current iteration and moves to the next iteration.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
std::cout << i << std::endl;
}
The number 3 is skipped.
A for loop can be placed inside another for loop. This is called a nested loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
std::cout << i << "," << j << std::endl;
}
}
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
std::cout << "* ";
}
std::cout << std::endl;
}
Output:
*
* *
* * *
* * * *
* * * * *
<= when < is required, or vice versa.Correct structure:
for (int i = 0; i < 10; i++) {
std::cout << i;
}
#include <iostream>
int main() {
int n;
std::cout << "Enter a number: ";
std::cin >> n;
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
std::cout << i
<< " is even"
<< std::endl;
}
else {
std::cout << i
<< " is odd"
<< std::endl;
}
}
return 0;
}
When a variable is declared inside the initialization part of a for loop, its scope is normally limited to the loop.
for (int i = 1; i <= 5; i++) {
std::cout << i;
}
// i is not available here
The variable i can be used inside the loop but not after
the loop.
for (int i = 1; i <= 10; i++) {
std::cout << i << std::endl;
}
| Part | Purpose |
|---|---|
| Initialization | Sets the starting value of the loop variable. |
| Condition | Determines whether another iteration should execute. |
| Update | Changes the loop variable after each iteration. |
| Body | Contains the statements repeated by the loop. |
| break | Terminates the loop immediately. |
| continue | Skips the current iteration. |
for (int i = 1; i <= 10; i++) {
std::cout << i;
}
Question: Which part of a for loop changes the loop variable after each iteration?