do while loop in c


  • The last of C’s loops is the do-while. Unlike the for and the while loops, in which the condition is tested at the top of the loop, the do-while loop checks its condition at the bottom of the loop.
  • This means that a do-while loop will always execute at least once.
  • Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop.

    Syntax:

    do
    {
      statement(s);
    }
    while(condition);
    

Flow Diagram for do while loop:

    do while loop in c

    Example:

    #include <stdio.h>
    int main()
    {
      int j=0;
      do
      {
    	printf("Value of variable j is: %d\n", j);
    	j++;
      }
      while (j<=3);
      return 0;
    }
    

    Output:

    Value of variable j is: 0
    Value of variable j is: 1
    Value of variable j is: 2
    Value of variable j is: 3