About C++ loops

We have three loops including for loop in C++. A loop is used to perform a task several times. Suppose you want to print a specific output 100 times. Then you have to use print statement 100 times. But using a loop we can easily do this thing.

There are three loops in C++. These are as follows;

  1. For loop;
  2. While loop;
  3. Do while loop.

However, in this article we will discuss about only for loop in C++. You will learn about other two loops in our next two articles.

C++ for loop, syntax and use

C++ for loop works as the same process as for loop of other language like C, C# or Java.

Let’s see the syntax of C++ for loop first.

// syntax of for loop in c++

for(initialization; condition; increment/decrement){
    
    // executable code while condition is true
    
}

The initialization section initializes the initial value of variable.

The condition section will check the condition as it is true or false. When the condition will be true, the conde inside for loop will be executed. Otherwise the loop will break.

Increment or decrement section increments or decrements the value of variable. Let’s try to understand by the given program.

#include <iostream>
using namespace std;

int main(){
    int c;

    for(c = 0; c <= 100; c = c + 10){
        cout << c << " ";
    }

    return 0;

}

Output of this for loop program:

c-for-loop-for-loop-in-c

How the program work

Now we will see how this for loop works in the above program. Look at the table bellow;

Value of c c <= 100 Action Incrementing c
0
true
print c = 0
c = 10
10
true
print c = 10
c = 20
20
true
print c = 20
c = 30
30
true
print c = 30
c = 40
40
true
print c = 40
c = 50
50
true
print c = 50
c = 60
60
true
print c = 60
c = 70
70
true
print c = 70
c = 80
80
true
print c = 80
c = 90
90
true
print c = 90
c = 100
100
true
print c = 100
c = 110
110
false
Loop break
--------

C++ nested for loop, syntax and use

When we use a loop inside another loop then that is called nested loop. Here we will learn to use a for loop as the nested loop. Let’s see the syntax for nested for loop bellow;

for(i = 1; i < 5; i++){    // outer loop

    // code to execute for outer loop
    
    for(j = 1; j < 5; j++){    // first inner loop
    
        // code to execute for first inner loop
        
        for(k = 1; k < 5; k++){    // second inner loop
        
            // code to execute for second inner loop
        }
    }
}

Every inner loop will iterate maximum times for one iteration of their immediate outer loop.

In the above example first inner loop will iterate 4 times for each iteration of outer loop.

And the second inner loop will iterate 4 times for each iteration of first inner loop.

That means, the second inner loop will iterate (4 * 4 * 4) = 64 times.

The first inner loop will iterate (4 * 4) = 16 times and the outer loop will iterate 4 times.

However, using inner loop at more than 2 levels should be avoided. It takes too much time to execute the program. We will learn about time complexity later.

Now, let’s see an example program using nested for loop.

Example program using nested for loop

// program using nested loop in c++

#include <iostream>
using namespace std;

int main(){
    int c, d;

    for(c = 1; c <= 5; c++){
        cout << c << ". Outer loop\n";

        for(d = 1; d < 4; d++){
            cout << "\t"<< d << ". Inner loop\n";
        }
    }
    return 0;
}

Output of this nested for loop program

1. Outer loop
        1. Inner loop
        2. Inner loop
        3. Inner loop
2. Outer loop
        1. Inner loop
        2. Inner loop
        3. Inner loop
3. Outer loop
        1. Inner loop
        2. Inner loop
        3. Inner loop
4. Outer loop
        1. Inner loop
        2. Inner loop
        3. Inner loop
5. Outer loop
        1. Inner loop
        2. Inner loop
        3. Inner loop

You may have noticed that the inner loop iterates 3 times for each iteration of outer loop. However we will practice more program using C++ nested loop in our C++ programming section.

Infinite for loop and their use

The loop which never stops is called infinite loop. The condition of infinite loop will always become true. The infinite loop will works until the space of your memory becomes full.

We can make an infinite for loop in different way. If we use double semicolon inside the for loop then it will be an infinite for loop.

Note: You should not run these program if you use a low configured computer or you have a limited space in the main memory. Your computer may stop working.

#include <iostream>
using namespace std;

int main(){
    int c;

    for(; ;){
        cout << "Infinite loop\n";
    }
    return 0;
}

Output of infinite for loop

infinite loop
infinite loop
infinite loop
infinite loop
............
............

We can also make an infinite for loop using our own condition which will never false. See the following program.

#include <iostream>
using namespace std;

int main(){
    int k;

    for(k = 1; k >= 1; k++){
        cout << "Infinite loop\n";
    }
    return 0;
}

This for loop is also an infinite for loop. Because the value of variable k will never be less than or equal to 1 if we increment its value by 1 each time. So, the condition will never become false. Which results in an infinite for loop.

Previous pageswitch statement in C++

Next pagewhile loop in C++