Examples of for Loop in C++

Example 1

The below example demonstrates the use of a for loop in a C++ program.

C++




// C++ program to illustrate for loop to print numbers from
// 1 to n
  
#include <iostream>
using namespace std;
  
int main()
{
    // initializing n (value upto which you want to print
    // numbers
    int n = 5;
    int i; // initialization of loop variable
    for (i = 1; i <= n; i++) {
        cout << i << " ";
    }
  
    return 0;
}


Output

1 2 3 4 5 

Explanation

The above program uses a for loop to print numbers from 1 to n (here n=5). The loop variable i iterates from 1 to n and in each iteration condition is checked (is i<=n i.e i<=5 ), if true then it prints the value of i followed by a space and increment i. When the condition is false loop terminates.

Example 2

Example to demonstrate the use of a for loop in a C++ program for printing numbers from n to 1 (i.e. reverse).

C++




// C++ program to illustrate for loop to print numbers from
// n to 1 (reverse counting).
  
#include <iostream>
using namespace std;
  
int main()
{
    // initializing n (value upto which you want to print
    // numbers
    int n = 5;
    int i; // initialization of loop variable
    for (i = n; i >= 1; i--) {
        cout << i << " ";
    }
  
    return 0;
}


Output

5 4 3 2 1 

Explanation
The above program uses a for loop to print numbers from n to 1 (here n=5) the loop variable i iterates from n to 1 and in each iteration condition is checked (is i>=1) if true then it prints the value of i followed by a space and decrement i. When the condition is false loop terminates.

So we can conclude that no matter what the updation is, as long as the condition is satisfied, for loop will keep executing.

Note: Scope of the loop variables that are declared in the initialization section is limited to the for loop block.

Example 3

The program demonstrates the use of a loop with different kinds of updation.

C++




// C++ program to illustrate the use of differnt loop
// updation
#include <iostream>
#include <string>
using namespace std;
  
// driver code
int main()
{
  
    // for loop with different kind of initialization
    for (int i = 32; i > 0; i = i / 2) {
        cout << "Geeks" << endl;
    }
  
    return 0;
}


Output

Geeks
Geeks
Geeks
Geeks
Geeks
Geeks

for Loop in C++

In C++, for loop is an entry-controlled loop that is used to execute a block of code repeatedly for the specified range of values. Basically, for loop allows you to repeat a set of instructions for a specific number of iterations.

for loop is generally preferred over while and do-while loops in case the number of iterations is known beforehand.

Similar Reads

Syntax of for Loop

The syntax of for loop in C++ is shown below:...

Flowchart of for Loop in C++

Flowchart of for Loop in C++...

Examples of for Loop in C++

Example 1...

C++ Nested for Loop

...

Multiple Variables in for Loop

...

C++ Infinite for Loop

...

Range-Based for Loop in C++

A nested for loop is basically putting one loop inside another loop. Every time the outer loop runs, the inner loop runs all the way through. It’s a way to repeat tasks within tasks in your program....

for_each Loop in C++

...