For loop in C++ language

  • The for loop is the easiest C++ loop to understand. All its loopcontrol elements are gathered in one place, while in the other loop constructions they are scattered about the program, which can make it harder to unravel how these loops work.
  • The for loop is a generic iterator in C++, it can step through the items in any ordered sequence or other iterable object.
  • for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax

for(initialization; expression; increment)
  statement;

Flow Diagram

for loop in C++

Example

// fordemo.cpp 
// demonstrates simple FOR loop 
#include using namespace std; 

int main() 
{ 
  int j; //define a loop variable 

  for(j=0; j<15; j++) //loop from 0 to 14, 
    cout << j * j << “ “; //displaying the square of j 
  cout << endl; 
  return 0;
} 

Output

 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196