c++

C++ for loop

In this article, we are going to learn about for loop in C++

In computer programming, loops are used to repeat a block of code until certain condition is full filled .

For example, if we want to print a sentence 100 times, instead of writing the same sentence 100 times, we will use a loop to do so.

That was just a simple example, we can achieve much more efficiency and sophistication in our programs by making effective use of loops.

There are basically 3 types of loop:

Let’s study more about for loop.


C++ for loop

The syntax of for loop is given below:

for(initialization ; condition ; update){
    // body of loop
}

In the above syntax:

Algorithm Flowchart of for Loop

Example 1:

In this example we’re going to print numbers from 1 to 10. Following is its program:

#include <iostream> 
using namespace std;
int main() { 
    for (int i = 1; i <= 10; ++i) { 
        cout << i << " "; 
    } 
    return 0; 
}

Output 1:

1 2 3 4 5 6 7 8 9 10

Explanation of the above program

Iteration Variable i <= 10 Action
1st i = 1 true 1 is printed. i is increased to 2.
2nd i = 2 true 2 is printed. i is increased to 3.
3rd i = 3 true 3 is printed. i is increased to 4.
4th i = 4 true 4 is printed. i is increased to 5.
5th i = 5 true 5 is printed. i is increased to 6.
6th i = 6 true 6 is printed. i is increased to 7.
7th i = 7 true 7 is printed. i is increased to 8.
8th i = 8 true 8 is printed. i is increased to 9.
9th i = 9 true 9 is printed. i is increased to 10.
10th i = 10 true 10 is printed. i is increased to 11.
11th i = 11 false loop is terminated.

Hope you’ve understood the tracing of above example, now guess the working of below example.

Example 2:

Following program prints ‘Hi !’ five times on the console.

#include <iostream> 
using namespace std; 
int main() { 
    for (int i = 1; i <= 5; ++i) { 
        cout << "Hi ! " << endl; 
    } 
    return 0; 
}

Output :

Hi !
Hi !
Hi !
Hi !
Hi !

Example 3:

The following program prints the sum of 1st 'n' natural number, 'n' will be taken by the user.

#include <iostream>
using namespace std;
int main() {
int num, sum;
sum = 0;
cout << "Enter a positive integer: ";
cin >> num;
for (int count = 1; count <= num; ++count) {
sum += count;
}
cout << "Sum = " << sum << endl;
return 0;
}

Output :

Suppose user inputs 10, i.e. num = 10

Enter a positive integer: 10
Sum = 55

The explanation is given below,

In the above example, we have two variables num and sum. The sum variable is assigned with 0 and the num variable is assigned with the value provided by the user.

Here in the for loop :-


C++ infinite for loop

An infinite loop occurs when condition for running a loop always true. As a result of this the block of code inside the body of loop keeps on repeating infinitely (until the memory if full).

Let’s look on an example of it using for loop.

// infinite for loop 
for(int i = 1; i > 0; i++) { 
    // block of code
}

In the above program, the condition is always true which will then run the code for infinite times.