Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Wednesday, November 25, 2020

Difference between while & do while loop in C

The major difference is that the while loop has the condition at the starting point whereas the do-while loop has the condition at the end of the loop

Difference between while & do while loop in C


do...wile loop is similar to while loop, however is a difference between them: In while loop, condition is evaluated first and the statements inside loop body gets executed, on the other hand in do...while loop, statements inside do...while gets executed first and then condition is evaluated.

A simple example of while loop:

#include <stdio.h>
#include <conio.h>	
void main()
{	
 int i = 1;					
 while(i < 5){
  printf("protec");
  i++;
 }
 getch();
} 

Same example using do...while loop:

#include <stdio.h>					
#include <conio.h>						
void main()					
{
 int i = 1;
 do{
  printf("protec");			
  i++;
 }while(i < 5);			  
 getch();	   
}

Output:

Protec
Protec
Protec
Protec
Protec

Conclusion:

If you try and compare both the set of codes above, you will notice that there is not much difference between while and do while loop.

The major difference is that the while loop has the condition at the starting point whereas the do...while loop has the condition at the end of the loop.

Also, if you notice a minor fact here, there is a semicolon at the end of the do...while looping condition whereas it does not exists in case of the while loop.

The statements within the while loop will never execute if the while condition is false however in case of a do...while loop the block statements are going to be executed at least once.


while loop:

  1. Entry conditioned loop
  2. Condition is checked before loop execution
  3. Never execute loop if condition is false
  4. There is no semicolon at the end of while statement
  5. Lower execution time and speed
  6. No fixed number of iterations

do...while loop:

  1. Exit conditioned loop
  2. Condition is checked at the end of loop
  3. executes false condition at least once since condition is checked later
  4. There is semicolon at the end of while statement
  5. Higher execution time and speed
  6. Minimum one number of iterations


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us