DO WHILE LOOP

 

  1. do...while loop:
  2. 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

Comments

Popular posts from this blog

STRUCTURE

FILE HANDLING

WHILE LOOP