C++ for Loop (with example programs)

- November 22, 2017
In this article you will know about C++ 'for' loop structure, its working, its flowchart, its components and 'for' loop programs. Each heading of this tutorial is very important to clear the concept of 'for' loop, so completely read this article.

What is the 'for' Loop in C++

The 'for' loop is also known as counter-controlled loop. The 'for' loop is used to execute a statement or group of statements repeatedly for a specified number of times. The general syntax of this loop is as follows:
for (Initialization; condition; Increment or Decrement)
{
  statements;
}
Next_statement;
Where:
Condition It specifies the test condition. The 'for' loop will continue to execute the body of loop as long as the test condition remains true. If the test conditions become false at the any stage of program execution then the loop is terminated.
Initialization It specifies the statement used to assign value to a variable. This part is used to declare and initialize the control variable in most of the C++ programs. In this part of 'for' loop, the multiple variables can be declared and initialized. Each variable is written separately by comma (,). The semicolon is must be used at the end of Initialization part. Such as:
for (int a= 1, b= 4; a <=4; a++)
The initialization part of the 'for' loop is optional. If it is omitted than a semicolon ";" is used in its place.
Increment / Decrement? It specifies the increment or decrement part in 'for' loop structure. Generally, this part is used to increase or decrease the value of a control variable. After executing the body of loop then this part is executed.
In this part of 'for' loop, compound assignment expressions or multiple increment / decrement statements can be written. Each is written separated by commas. The use of increment or decrement is also optional.
statements It specifies the body of the 'for' loop. This part of 'for' loop may contain compound statements or only single statement. If there is a single statement in the body of loop then the use of braces is optional. 
 All the parts of the 'for' loop structure are optional.
In this case, the syntax of the 'for' loop structure is as follows:
for (;  ;)
The first semicolon is used instead of "initialization" part and second is used instead of "condition" part. There is no test condition in the above 'for' loop syntax. In this case the loop becomes infinite loop. C++ assumes that the condition is true.

Flowchart of for Loop

C++ 'for' loop structure flowchart is shown in the following image.
For Loop Flowchart

Working of For Loop

The 'for' loop statements are executed by following the given rules and regulations:
  • First the "Initialization" part is executed. It is executed only for the first time when control enters into the loop.
  • After execution of the initialization part then the condition is evaluated. If condition is true then body of loop is executed. 
  • After executing the body of loop, Increment/Decrement part is executed. This part is considered as last statement of the body of the 'for' loop.
  • Control again transfers to the condition after executing Increment/Decrement. If the condition is true then the body of loop and Increment/Decrement are executed. This process repeated again and again until the condition remains true. When condition becomes false then the loop is terminated (ended).
To clear the concept, look at the given program that displays counting from 1 to 15 using for loop statement.

#include <iostream>
using namespace std;
main ()
{
   int num;
for (num = 1; num<=15; n++)
   cout<<n<<endl;
   cout<<"Done";
return 0;
}
Output of the program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Done   
In the above program:
The variable "num" is initialized to 1 in the initialization part. The test condition is "num<=10". If this condition is true then the body of loop will be executed. The increment/decrement part contains "num++". It increases the value of "num" by 1 after executing the body of loop and then condition is tested again. The process remains continue until the condition true. The loop is terminated when the value of "num" becomes 11. (In this case loop ended due to false condition).

For Loop Example Programs

Example 1: This program displays the following series:
1,2,4,8,16,32,64
Using for loop.
#include <iostream>
using namespace std;
main ()
{
   int n, res;
for (n=1; n<=64; n+=n)
   cout<<n<<"\t";
return 0;
}

Example 2: The following program displays the even numbers between 1 to 15 in descending order using the 'for' loop statement.
#include <iostream>
using namespace std;
main ()
{
  int n;
for (n=15; n>=1; n--)
   {
     if (n%2==0)
        cout<<n<<endl;
    }
return 0;
}
Output of the program:
14
12
10
8
6
4
2
Example 3: The following program inputs an integer value and displays the digits of that value in reverse order using the 'for' loop statement.
#include <iostream.h>
using namespace std;
main ()
{
  long r, n;
  cout<<"Enter any integer value:";
cin>>n;
for (r= 0; n!=0; n=/10)
r= n%10 + 10* r;
cout<<"Number in reverse order ="<<r;
return 0;
}
Output of the program:
Enter any integer value: 12345
Number in reverse order: 54321

Example 4: The following program  generates Fibonacci series from 1 to 100. Each number of this series is generated by adding two previous numbers. The Fibonacci series is:
1,1,2,3,5,8,13,21.......
#include <iostream>
using namespace std;
main ()
{
  int next, last, s;
for ( next= 0, last = 1; last<=100;)
{
   cout<<last<<",";
    s = next + last;
    next = last;
    last = s;
}
return 0;
}
Output of the program:
1,1,2,3,4,8,13,21,34,55,89

Example 5. The following program calculates and displays the product of all odd numbes from 1 to 10 using 'for' loop statement.
#include <iostream>
using namespace std;
main ()
{
   unsigned long n, p;
   for (n= 1, p= 1; n<=10; n++)
       if (n%2==1)
        {
           p = p*n;
           cout<<n<<endl;
         }
cout<<"Product of odd numbers = "<<p;
return 0;
}
Output of the program:
1
3
5
7
9
Product of odd numbers = 945

Example 6: The following program inputs a table number and its length. It displays table using 'for' loop.
#include <iostream>
using namespace std;
main ()
{
  int num, c, len;
  cout<<"Enter a table number:";
  cin>>num;
  cout<<"Enter length of table:";
  cin>>len;
  for (c= 1; c<=len; c++)
     cout<<num<<'x'<<c<<'='<<num*c<<endl;
return 0;
}
Output of the program:
Enter a table number: 8
Enter length of table: 6
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48