Do While Loop in C

The Do While loop in C tests the condition at the loop end. Do While loop in C executes the code block lines at least once, even the condition Fails.

The While loop discussed in our previous article tests the condition before compiler enters the code block. If the condition result is True, then only code inside the loop executes. Otherwise, it won’t run at least once. There are some circumstances where it is required to execute some operations (perform some statements) first and then check the condition. In these situations, we can use Do While loop in C.

Do While loop in C Programming Syntax

The syntax of the Do While Loop in C Programming is as shown below:

do
{
  statement 1;
  statement 2;
    ………….
  statement n;
}
While (condition);

First, it executes the lines inside the loop, and after arriving at the end, the compiler checks the while condition. If the condition returns True, it recurs the process, and if it fails, C Do While Loop terminates.

NOTE: In C Do While loop, put a semi-colon after the While condition.

Do While Loop Flow Chart

Do While Loop in C Programming Flow Chart

Flow chart sequence of a Do while loop in C Programming

  1. Variable initialization, and then it enters the Do While loop.
  2. Execute/Run a group of statements within the C Programming loop.
  3. Next, use Increment and Decrement Operator inside the loop to increment or decrements the values.
  4. Next, it checks the while condition. If the condition output is True, the code inside the C Do while loop executes again. The process will last until the condition fails.
  5. If it is False, compiler exits from the loop.

Do While Loop in C Programming Example

This C program helps you to understand the Do While Loop in C Programming

/* Do While Loop in C Programming example */

#include <stdio.h>

int main()
{
  int number, total=0;
 
  printf("\n Please Enter any integer below 10 \n");
  scanf("%d", &number);

  do
   {
     total = total + number;
     printf(" Number = %d\n", number);
     printf(" Total Value is: %d\n", total); 
     number++;
   }while (number< 10);

  printf(" Total Value from outside the Loop is: %d \n", total);
  return 0;
}
 Please Enter any integer below 10 
6
 Number = 6
 Total Value is: 6
 Number = 7
 Total Value is: 13
 Number = 8
 Total Value is: 21
 Number = 9
 Total Value is: 30
 Total Value from outside the Loop is: 30 

ANALYSIS

  1. In this C do while loop program, User will enter any value below 10, and the total variable initialized to 0.
  2. User entered value will assign to the number variable, and then the number is added to the total.
  3. In the next line, we used ++ operator to increment the number value.
  4. After this line, the value in a Number variable tests against the while condition. If the condition results true, it repeats the process. Otherwise, it will exit from the loop
  5. In the next line, we used one more printf statement to show that it is coming from outside the while loop.

Let us enter a value greater than 10 to see what will happen?

 Please Enter any integer below 10 
11
 Number = 11
 Total Value is: 11
 Total Value from outside the Loop is: 11 

If you observe from the above screenshot, we entered value 11, and it still displays the total as 11 because after executing that code, it checked the while condition. It failed, so it exits from the loop.

Difference between While loop and Do While in C Programming?

Although, Do While and While loop looks similar, they differ in their execution.

  • While loop, the condition is tested at the beginning of the loop, and if the condition is True, then only statements in that loop will be executed. It means While loop executes the code block only if the condition is True.
  • For Do While loop, the condition tests at the end of the loop. So Do While executes the statements in the code block at least once even the condition Fails.

I think you will understand it better when you see the example so, let’s write the same program using While loop and Do While loop in C

While loop in C Programming Example

In this program, we are declaring integer and then check whether 0 is greater than ten or not. If the condition is True, then it will print this statement, “X is Greater Than 10”. There is one more printf statement outside the While loop, and this statement will execute after the while loop.

#include <stdio.h>
 
int main()
{
  int x=0;

  while(x > 10)
   {
     printf("\n X is Greater Than 10\n");
   }

  printf("\nStatement Outside the while loop\n");
  return 0;
}
Statement Outside the while loop

Do While in C Programming Example

We are going to write the same example using do while in c language

/* Do While in C Example */
#include <stdio.h>

int main()
{
 int x=0;

 do
  {
    printf("\n X is Greater Than 10 \n");
  }while(x > 10);

 printf("\n Statement Outside the while loop \n");
 return 0;
}
 X is Greater Than 10 

 Statement Outside the while loop 

Although the condition fails, the statement inside the do while loop executed once because of the condition tested after the execution of the statements.