FOR LOOP

 ☆ For LOOP PROGRAMME :

    The syntax of the for loop is:


    for (initializationStatement; testExpression; updateStatement)
    {

    // stastatements inside the body of loop

    }

    How for loop works?
    • The initialization statement is executed only once.
    • Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop is terminated.
    • However, if the test expression is evaluated to true, statements inside the body of for loop are executed, and the update expression is updated.
    • Again the test expression is evaluated.

    This process goes on until the test expression is false. When the test expression is false, the loop terminates.

    Example :
    #include <stdio.h>
    void main()
    {
        int i;
    
        for (i = 1; i < 11; ++i)
        {
    
            printf("%d ", i);
    
        }
    }
    



    Output :1 2 3 4 5 6 7 8 9 10

    Now Let's iterate above example
    • i is initialized to 1.
    • The test expression i < 11 is evaluated. Since 1 less than 11 is true, the body of for loop is executed. This will print the 1 (value of i) on the screen.
    • The update statement ++i is executed. Now, the value of i will be 2. Again, the test expression is evaluated to true, and the body of for loop is executed. This will print 2 (value of i) on the screen.
    • Again, the update statement ++i is executed and the test expression i < 11 is evaluated. This process goes on until i becomes 11.
    • When i becomes 11, i < 11 will be false, and the for loop terminates.

  

Comments

Popular posts from this blog

STRUCTURE

FILE HANDLING

WHILE LOOP