Tutorial Study Image

For loop in C++ with Examples


August 18, 2022, Learn eTutorial
1124

For loop executes a series of statements multiple times and shortens the code that manages the loop variable. This means, that a for loop is a type of repetition control structure that allows us to write a loop that is executed a certain number of times. The loop allows us to perform an n number of steps in a single line.

In the previous tutorial, we learned about the basic concept of the loop in C++.

Syntax of for loop in C++


for (initialization expr; test expr; update expr)
{    
     // body of the loop
     // statements we want to execute
}
 

For example


for(int i = 0; i < n; i++){

}
 

A loop variable is used to control the loop in a for loop. First, assign a value to this loop variable, then determine whether it is less than or greater than the counter value. The loop body is executed and the loop variable is updated if the statement is true. Steps are repeated until the exit condition is reached.

  • Initialization Expression: We must set the loop counter to some value in this expression. For example, int i=1;
  • Test Expression: We must test the condition in this expression. If the condition is true, we will execute the body of the loop and proceed to  update the expression; otherwise, we will exit the for loop. For example, i<= 10;
  • Update Expression: After executing the loop body, this expression increments or decrements the loop variable by some value. for example i++

Flow Chart of for loop

C++ : For Loop

C++ program examples to demonstrate the for loop

Example 1: Display a text 5 times


#include <iostream>
using namespace std;

int main()
{
    for (int i = 1; i <= 10; i++)
    {
        cout << "Learn eTutorials\n";
    }

    return 0;
}

Output:

Learn eTutorials
Learn eTutorials
Learn eTutorials
Learn eTutorials
Learn eTutorials

We can check how this program works

Iteration Variable i <= 5 Action
1st i = 1 true Learn eTutorials is printed and i is increased to 2.
2nd i = 2 true Learn eTutorials is printed and i is increased to 3.
3rd i = 3 true Learn eTutorials is printed and i is increased to 4.
4th i = 4 true Learn eTutorials is printed and i is increased to 5.
5th i = 5 true Learn eTutorials is printed and i is increased to 6.
6th i = 6 false The loop is terminated

Example 2: Print Numbers From 1 to 5


#include <iostream>

using namespace std;

int main() {
        for (int i = 1; i <= 5; ++i) {
        cout << i << "\n";
    }
    return 0;
}

Output:

1
2
3
4
5

In the above example, we are printing the numbers from 1 to 5. For that, we first have initialized a variable with value “1” and in the condition parameter we check the condition i <= 5 and if the condition is true the block of the for loop will execute, and after the execution, the increment/decrement parameter will be executed and in that the value of the variable i is increment and the loop will continue till the condition   i <= 10 becomes false.

Iteration Variable i <= 5 Action
1st i = 1 true 1 is printed and i is increased to 2.
2nd i = 2 true 2 is printed and i is increased to 3.
3rd i = 3 true 3 is printed and i is increased to 4.
4th i = 4 true 4 is printed and i is increased to 5.
5th i = 5 true 5 is printed and i is increased to 6.
6th i = 6 false The loop is terminated