Difference Between while and do while Loop [With Example]

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Previously you have learned about while loop and do while loop in C. Now, we will see the basic difference between while and do while with example.

Difference1:

The while loop is a pre-test loop but do-while is a post-test loop.

The while loop is pre-test loop, where firstly the condition is checked and if the condition is true then only the statements of the while loop execute. The do-while loop is a post-test loop. In the do-while loop, the statements of the do-while loop are executed after that, the condition is evaluated, and if the condition is true then again the statements of the do-while loop are executed.

Differences in the syntax of while and do-while loop

Difference2: They have different syntaxes.

Syntax of while loop

while (condition)
 {
   // statements
 }

Syntax of the do-while loop

do
 {
   // statements 
 } while(condition);

Difference3: The do-while should be terminated semicolon but while loop can’t.

While loop can’t be terminated with a semicolon but the do-while loop should be terminated with a semicolon.

Difference4:

The condition of the while loop is at the top of the loop but the condition of the do-while loop is at the bottom of the loop.

Difference5: They have different flowcharts.

Flowchart for while loop

Flowchart for while loop

Flowchart for do-while loop

do while loop flowchart

Execution of while loop and do-while loop

Difference4: The while loop can execute 0 to N times, but the do-while loop executes 1 to N times.

The statements of the do-while loop execute at least 1 time in every condition. In the while loop, the test expression evaluates false in first checking then the statements of the while loop is not executed. But the condition of the do-while loop is checked at the end of the loop, so it is guaranteed that the statements of the do-while loop execute at least once.

 //while loop
 #include<stdio.h>
 int main()
 {
   int i=1;

   while (i<1)
   {
     printf("Hello\n");
     i++;
   }

   printf("Loop completed");

   return 0;
 }

Output:-

Loop completed

 // do-while loop
 #include<stdio.h>
 int main()
 {
   int i=1;

   do
   {
     printf("Hello\n");
     i++;
   } while(i<1);

   printf("Loop completed");

   return 0;
 }

Output:-

Hello
Loop completed

The statements of while loop will execute or not it depend on the condition of the while loop. If the condition of the while loop is true then the statements of the while execute, and it will execute until the condition becomes false.

The statements of the do-while loop execute one time then it checks the condition. So, the first time execution of the do-while loop statements doesn’t depend upon the condition of the do-while loop. After executing one time the statements of the do-while loop, the condition is checked. If the condition is true then statements of the do-while loop execute again until the condition becomes false.

Sample program of while and do-while loop

C Program to Print Even Numbers using while loop

 #include<stdio.h>
 int main()
 {
   int m,n;

   printf("Enter range: ");
   scanf("%d %d",&m, &n);

   while (m<=n)
   {
     if(m%2==0) printf("%d ",m);
     ++m;
   }

   return 0;
 }

Output:-

Enter range: 5 15
6 8 10 12 14

C Program to Print Even Numbers using the do-while loop

 #include<stdio.h>
 int main()
 {
   int m,n;

   printf("Enter range: ");
   scanf("%d %d",&m, &n);

   do
   {
     if(m%2==0) printf("%d ",m);
     ++m;
   } while(m<=n);

   return 0;
 }

Output:-

Enter range: 10 20
10 12 14 16 18 20

C Program to Print Odd Numbers using while loop

 #include<stdio.h>
 int main()
 {
   int m,n;

   printf("Enter range: ");
   scanf("%d %d",&m, &n);

   while (m<=n)
   {
     if(m%2!=0) printf("%d ",m);
     ++m;
   }

   return 0;
 }

Output:-

Enter range: 5 15
5 7 9 11 13 15

Print Odd Numbers using the do-while loop

 #include<stdio.h>
 int main()
 {
   int m,n;

   printf("Enter range: ");
   scanf("%d %d",&m, &n);

   do 
   {
     if(m%2!=0) printf("%d ",m);
     ++m;
   } while(m<=n);

   return 0;
 }

Output:-

Enter range: 10 20
11 13 15 17 19

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Recommended Posts

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Reply