C++ Tutorial 04 - For loop

The for Loop, loop in which the program instructs the compiler to execute a certain code FOR a set number of times.

               for ( <expression_1> ; <expression_2> ; <expression_3> )

                               <statement>


·        expression_1:  Initial, the initialization phase is run first and only once. Any loop control variables can be declared and initialized at this phase. You don't have to use a statement here as long as you use a semicolon.

·        expression_2: Condition, each time the loop is repeated, the condition is evaluated. The loop continues to execute as long as the condition is true. When the loop is successfully repeated, increment is an expression that defines how the loop control variable is incremented (that is, each time condition is evaluated to be true).

·        expression_3: modifier, allows you to update any loop control variables. (Increment / decrement)

·        statement: use, {_} for many statements.

               for ( <expression_1> ; <expression_2> ; <expression_3> )

               {

                               <statement>

                }

           

Flow Diagram


 

Code 01:


#include <iostream> 

using namespace std;

 

int main()

{

    for (int i = 0; i < 6; i++)

        cout << i << "\n";

    return 0;

}

Output of the code



 





Code 02:     This code also gives the same result.


#include <iostream>

using namespace std; 

int main()

{  

    //i variable can be declare as a constant variable.

    int i =0;

    for (   ; i < 6; )

    {   

        cout << i << endl;

         //modifier can be put like that

        i++;

    }

    return 0;

}

Output of the code
Output of the code


Post a Comment

أحدث أقدم