In this C++ tutorial, you will learn the syntax of For loop, its algorithm, flowchart, then some examples illustrating the usage of it. Later in the tutorial, we shall go through Infinite For Loop and Nested For Loop.

C++ For Loop

For Loop can execute a block of statements in a loop based on a condition. It is similar to while loop in working, but the only difference is that for loop has provision for initialization and update in its syntax.

Syntax

Following is the syntax of for loop in C++.

</>
Copy
for (initialization; condition; update) {
  // statement(s)
}

At the start of for loop execution, initialization statement is executed and then the condition is checked. If the condition is true, statement(s) inside for block are executed. And the update statement is executed. The condition is checked again. If it evaluates to true, the statement(s) inside the for loop are executed. This cycle goes on. If at all, the condition evaluates to false, for loop execution is deemed completed and the program control comes out of the loop. And the program continues with the execution of statements after for loop if any.

Algorithm

Following would be the algorithm of for loop.

  1. Start.
  2. Execute initialize statement.
  3. Check the condition. If the condition is false, go to step 6.
  4. Execute statement(s).
  5. Execute update statement. Go to step 2.
  6. Stop.

Make sure that condition would break after a definite number of iterations. If the condition is never going to be false, then the for loop is going to execute indefinitely.

Flow Diagram

Following is the flow chart of flow diagram of for loop in C++.

C++ For Loop Flowchart

Examples

1. Basic For loop example

In this example, we shall write a for loop that prints numbers from 1 to 5. The for loop contains statement to print a number, and the condition checks if the number is within the limits.

main.cpp

</>
Copy
#include <iostream>  
using namespace std;

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

Output

1
2
3
4
5

2. For loop to compute factorial

In this example, we shall use for loop to compute factorial of a number.

main.cpp

</>
Copy
#include <iostream>  
using namespace std;

int main() { 
   int n=5;
   int factorial = 1;

   for (int i = 1; i <= n; i++) {
      factorial *= i;
   }

   cout << factorial << "\n";
}

Output

120

3. For loop to compute sum of first N natural numbers

In this example, we shall use for loop to compute the sum of first N natural numbers. We shall write a for loop with condition that it is true until it reaches given number, and during each iteration, we shall add this number to the sum.

main.cpp

</>
Copy
#include <iostream>  
using namespace std;

int main() { 
   int n=5;
   int sum = 0;

   for (int i = 1; i <= n; i++) {
      sum += i;
   }

   cout << sum << "\n";
}

Output

15

For Loop with break Statement

You can break the for loop abruptly using break statement. break statement ends the execution of the wrapping loop.

In the following example, we shall write a for loop that prints numbers from 1 to 10. But, then we include a break statement such that when i is 4, we break the for loop.

main.cpp

</>
Copy
#include <iostream>  
using namespace std;

int main() { 
   for (int i = 1; i <= 10; i++) {
      if (i == 4) {
         break;
      }
      cout << i << "\n";
   }
}

Output

1
2
3

For Loop with continue Statement

You can skip the execution of statements in a for loop during an iteration using continue statement. continue statement takes the control to the condition without executing further statements in the loop.

In the following example, we shall write a for loop that prints numbers from 1 to 6. But, then we include a continue statement such that when i is 4, we skip the execution of further statements inside for loop.

main.cpp

</>
Copy
#include <iostream>  
using namespace std;

int main() { 
   for (int i = 1; i <= 7; i++) {
      if (i == 4) {
         continue;
      }
      cout << i << "\n";
   }
}

Output

1
2
3
5
6
7

Infinite For Loop

If the condition in for loop is going to be always true, then this loop runs indefinitely. This kind of loop is called infinite for loop.

Just a simple condition like 1==1 or true, will make the for loop to run indefinitely.

main.cpp

</>
Copy
#include <iostream>  
using namespace std;

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

Output

The natural numbers are printed to the terminal indefinitely, until you interrupt and stop the program execution.

Nested For Loop

For Loop statement is just like another statement in C++. So, you can include a for loop inside the body another for loop, just like a statement.

In the following example program, we shall print a pattern that resembles a triangle, using nested for loop.

main.cpp

</>
Copy
#include <iostream>  
using namespace std;

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

Output

 *
 * *
 * * *
 * * * *
 * * * * *

Outer for loop is used to traverse the rows, and inner for loop is used to traverse the columns.

Conclusion

In this C++ Tutorial, we learned the syntax of For Loop in C++, its algorithm, flowchart, and usage with the help of example C++ programs.