This loop is use to execute group of statement for n number of times based on condition. This statement has three expression in its parenthesis as.

  1. Initialization: – The variable will be initialize with one value. This part will be executed first time.
  2. Condition: – This part will be checked to make decision as the condition is true then the control of compiler will get inside the loop. (if the condition becomes false then the loop will be terminated)
  3. Updation: – This part is use to either increment or decrement the value of variable.

Syntax: –

For(initialization; condition; updation)
                {
                Statements;
                }

                Ex: –

int i;
for(i=0;i<5;i++)
                {
                cout<<i<<”\n”;
                }

Flow chart of for loop: –

flow chart of for loop.png

Nested for loop: – When one for loop is created inside another for loop then inner for loop is called as nested for loop is called as nested for loop.

Syntax: –

For(initialization; condition; updation)    //outer for loop
                {
                For(initialization; condition; updation)    //nested / inner for loop
                                {
                                _ _ _ _ _
                                _ _ _ _ _
                                }
                }

Note: – The life of inner for loop exist the till the outer for loop exist. Whenever the inner for loop is activated, the variable of inner for loop value. When it was given for first time.

Advertisements