Do While Loop in C

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

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

C Do While loop 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; the C Do While Loop terminates if it fails.

NOTE: Put a semi-colon in the Do While loop after the While condition.

Do While Loop in C Flow Chart

Do While Loop in C Programming Flow Chart

Flow chart sequence of a Do while loop in this C Programming

  1. Variable initialization, and then it enters the Do While loop.
  2. Execute/Run a group of statements within the Programming loop.
  3. Next, use Increment and Decrement Operator inside the loop to increment or decrements the values.
  4. Next, it checks the 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, the compiler exits from it.

Do While Loop in C Programming Example

This program helps you to understand the Do While Loop.

#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 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 is: 30 

Analysis

  1. In this C do while loop program, the User will enter any value below 10, and the total variable will be 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 the ++ 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 it.
  5. In the next line, we used one more printf statement to show that it comes 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 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.

What is the difference between While loop and Do While in C Programming?

Although the C Do While and While loop looks similar, their execution differs.

  • For a While, the condition is tested at the beginning, and if the condition is True, then only statements in that will execute. It means While executes the code block only if the condition is True.
  • For Do While, the condition tests at the end. So Do While loop 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 the While and Do While loop in C.

While Example

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

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

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

  printf("\nStatement Outside of it\n");
  return 0;
}
Statement Outside of it

Do While Example

We will write the same example using the C do while loop.

#include <stdio.h>

int main()
{
 int x=0;

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

 printf("\n Statement Outside of it \n");
 return 0;
}
 X is Greater Than 10 

 Statement Outside of it

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