Difference between while and do-while loop in C with syntax and flow

The diagram below shows the difference between while and do-while loops in C programming.

Let's break it down and understand how these two loops differ — one step at a time.

1. Syntax:
First, here's what the general syntax looks like for both loops:

i. while loop:
while(condition)
{
  // code inside the loop
}

ii. do-while loop:
do
{
  // code inside the loop
} while(condition);

2. When the condition is checked:
while loop: The condition is checked before the loop starts. So if the condition is false right away, the loop won't run at all.
do-while loop: Here, the condition is checked after the loop runs. That means the code inside the loop will run at least once, no matter what.

3. How iteration works:
Iteration just means repeating a set of instructions until a condition is no longer true.
In a while loop: If the condition is false at the beginning, the loop body won’t run.
In a do-while loop: The loop body runs once before checking the condition — so it always runs at least once.

4. Flow of control:
A flowchart helps show how control moves through each loop. In a while loop, control goes straight to the condition check. In a do-while loop, it first goes to the loop body, then checks the condition. That’s the key difference in how they work behind the scenes.


C Program to Illustrate Use of While Loop:

// C program that displays the numbers from 1 to 10:

#include<stdio.h>

int main()
{
 int i = 1;
 while(i <= 10)
 {
  printf("\n%d", i);
  i++;
 }
 return 0;
}

Output: 1 2 3 4 5 6 7 8 9 10

Explanation:
In this program, we first declare an integer variable i and initialize it to 1 using int i = 1;.

Then, the program checks the condition in the while loop: i <= 10. Since 1 is less than or equal to 10, the condition is true and the loop begins executing.

Inside the loop, printf("\n%d", i); prints the current value of i, which is 1. Then, i++; increases the value of i by 1, so i becomes 2.

The control goes back to the top of the while loop and checks the condition again: 2 <= 10. Since it's still true, the loop continues.

This process repeats until i becomes 11. At that point, the condition i <= 10 is false, and the loop stops. Control exits the loop, and the program ends.


C program to illustrate use of do while loop

/* C program given below displays series of number 1 ,2,3.....10 */
#include <stdio.h>
int main()
{
 int i=1;
 do
 {
  printf("\n %d",i);
  i += 1;
 } while(i<=10);
}

Output: 1 2 3 4 5 6 7 8 9 10

Explanation:
The variable i is declared as an integer and initialized to 1 using int i = 1;.

Since this is a do-while loop, the program begins by executing the loop body first — without checking the condition. The printf("\n%d", i); statement prints 1 to the screen.

Then i += 1; increments the value of i by 1, so now i becomes 2.

After that, the program checks the condition in the while part: i <= 10. Since 2 is less than or equal to 10, the condition is true, and the loop runs again.

This process continues — printing each value of i from 1 to 10 — until i becomes 11. At that point, the condition i <= 10 becomes false, and the loop terminates.

Note: The do-while loop always executes its body at least once, even if the condition is false at the start.


Previous Topic:-->> Nested while loop in C || Next topic:-->>Difference Between goto and loop in C.


Other Topics: