for loop 

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.



while loop :


The syntax of the while loop is:


while (test expression)

{


// statements inside the body of the loop


}


How while loop works??

The while loop evaluates the test expression inside the parenthesis ().

If the test expression is true, statements inside the body of while loop are executed. Then, the test expression is evaluated again.

The process goes on until the test expression is evaluated to false.

If the test expression is false, the loop terminates (ends).



Example :

#include <stdio.h>

void main()

{

    int i = 1;


    while (i <= 5)

    {

        printf(" %d ", i);

        ++i;

    }

}


Output : 1 2 3 4 5


Now Let's iterate above example


Here, we have initialized i to 1.

When i is 1, the test expression i <= 5 is true. Hence, the body of the while loop is executed. This prints 1 on the screen and the value of i is increased to 2.

Now, i is 2, the test expression i <= 5 is again true. The body of the while loop is executed again. This prints 2 on the screen and the value of i is increased to 3.

This process goes on until i becomes 6. When i is 6, the test expression i <= 5 will be false and the loop terminates.



do...while loop:

The do..while loop is similar to the while loop with one important difference. The body of do...while loop is executed at least once. Only then, the test expression is evaluated.



The syntax of the do...while loop is:


do

{


// statements inside the body of the loop


}while(test expression)


How do...while loop works??

The body of do...while loop is executed once. Only then, the test expression is evaluated.

If the test expression is true, the body of the loop is executed again and the test expression is evaluated.

This process goes on until the test expression becomes false.

If the test expression is false, the loop ends.



Example :


#include <stdio.h>

void main()

{


    int number, sum = 0;


    // the body of the loop is executed at least once


    do

    {

        printf("Enter a number: ");

        scanf("%d", &number);

        sum += number;

    }

    while(number != 0);

    printf("Sum = %d",sum);


}


First run:

Enter a number: 10

Enter a number: 20

Enter a number: 30

Enter a number: 40

Enter a number: 0

Sum = 100

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post