Do…while loop in C programming

C programming supports three types of looping statements for loop, while loop and do...while loop. Among three do...while loop is most distinct loop compared to others.

do...while is an exit controlled looping statement. We use do...while loop when there is a need to check condition after execution of loop body. do...while loop in any case executes minimum once.

Looping statements whose condition is checked after execution of its loop body is called as Exit controlled loop

We use do...while loop if we need to execute loop body minimum once. For example – consider a program to validate user input and run in loop until user feeds valid input. In this case the input statement should run minimum once and should repeat in loop until user provides valid input.

Syntax of do...while loop

do
{
    // Body of do while loop
} while (condition);

Parts of do...while loop

Similar to while, do...while loop contains two parts – body of loop and loop condition.

  • Body of loop contains single or set of statements to repeat.
  • The loop condition is a boolean expression evaluating to an integer value. If loop condition is true then loop repeats otherwise terminates.

Similar to while you can put loop counter variable-initialization statement before loop and variable-update before end of do...while loop.

Be careful while writing do...while loop. Unlike other two loops do...while contain semicolon at the end.

How do...while loop works?

do...while loop works in two step.

  1. Initially program control transfers to body of loop. It executes all statements inside loop body and transfers control to loop condition.
  2. Loop condition contains set of relational and logical expressions. If conditional expression evaluates 1 (true) then loop repeats again otherwise if conditional expression evaluates 0 (false) loop terminates.

The above two steps are repeated until loop condition is met.

Flowchart of do...while loop

do while loop flowchart

Example program to demonstrate do...while loop

Let us write a C program to print natural numbers from 1 to 10 using do...while loop.

/**
 * C program to print natural numbers using do while loop
 */

#include <stdio.h>

int main()
{
    /* Loop counter variable declaration */
    int n=1;

    do
    {
        /* Body of loop */
        printf("%d ", n);

        /* Update loop counter variable */
        n++;

    } while(n <= 10); /* Loop condition */

    return 0;
}

Output –

1 2 3 4 5 6 7 8 9 10

Explanation of the above program

  • The statement int n=1; declares an integer loop counter variable initialized with 1.
  • Next program control directly enters in loop body and executes the statement printf("%d ", n);. It print current value of n i.e. 1 for first run.
  • The statement n++; increments the value of n by 1.
  • Finally, loop condition part receives program control. Inside loop condition it evaluates (n <= 10). If the statement is true then the loop will continue otherwise terminate. For now it checks with n=1 i.e. while(1 <= 10); which is true hence loop continues.
  • In next iteration it prints 2 and increment n again by 1. Finally checks the loop condition while(n <= 10); which is again true.

The above 2-4 steps are repeated till condition is true.