Difference between "while" loop and "do while" loop

What is the difference between while loop and do while loop. I used to think both are completely same.Then I came across following piece of code:

do < printf("Word length. "); scanf("%d", &wdlen); >while(wdlen<2); 
This code works perfectly. It prints word length and tascans the input. But when I changed it to
while(wdlen

It gives a blank screen. It do not work. So there is some functional difference between both loops. Can anybody explain it? Is there any other difference in these two?

28.1k 22 22 gold badges 127 127 silver badges 157 157 bronze badges asked Sep 2, 2010 at 9:27 narayanpatra narayanpatra 5,651 13 13 gold badges 53 53 silver badges 61 61 bronze badges

while loop checks the condition initially and then executes.. but do-while executes the body atleast once even if the condition is false..

Commented Jan 3, 2013 at 19:22 Both are equally broken because neither of them check the return value of scanf() . Commented May 4, 2015 at 19:41

16 Answers 16

The do while loop executes the content of the loop once before checking the condition of the while.

Whereas a while loop will check the condition first before executing the content.

In this case you are waiting for user input with scanf(), which will never execute in the while loop as wdlen is not initialized and may just contain a garbage value which may be greater than 2.

33.5k 21 21 gold badges 112 112 silver badges 174 174 bronze badges answered Sep 2, 2010 at 9:28 2,866 22 22 silver badges 20 20 bronze badges

"wdlen (assumedly starts at 0) and will never be < 2" - back in my math class I was taught that 0 < 2 ;-)

Commented Sep 2, 2010 at 9:32 0 < 2 == false? Are you sure? Commented Sep 2, 2010 at 9:33 Thanks buddy. I understood. Any other difference in these two? Commented Sep 2, 2010 at 9:35

If I were a compiler and wdlen would not be initialized, I'd skip the whole block because if it's undefined it might as well be always > 2.

Commented Oct 7, 2012 at 14:22

While : your condition is at the begin of the loop block, and makes possible to never enter the loop.

Do While : your condition is at the end of the loop block, and makes obligatory to enter the loop at least one time.

answered Sep 2, 2010 at 9:29 Guillaume Lebourgeois Guillaume Lebourgeois 3,873 1 1 gold badge 21 21 silver badges 23 23 bronze badges tnx for good replay.There is only one difference??is there other difference?? Commented Jul 20, 2012 at 13:41 @AminM The latter requires two extra characters to type. – user4734394 Commented Jul 6, 2017 at 11:21

While is sometimes used almost like an if because when the condition isn't true the loop never gets entered, and inside the loop is code to deal with the condition being true until it isn't anymore.

Commented Oct 29, 2018 at 20:28

There are other programming languages, where while . do is also written as while . do but do . while is written as repeat . until . This is more in line with common English language.

Commented Dec 12, 2018 at 13:06
do < printf("Word length. "); scanf("%d", &wdlen); >while(wdlen<2); 

A do-while loop guarantees the execution of the loop at least once because it checks the loop condition AFTER the loop iteration. Therefore it'll print the string and call scanf, thus updating the wdlen variable.

while(wdlen

As for the while loop, it evaluates the loop condition BEFORE the loop body is executed. wdlen probably starts off as more than 2 in your code that's why you never reach the loop body.

answered Sep 2, 2010 at 9:33 720 4 4 silver badges 7 7 bronze badges "probably starts off less than 2". while(wdlen<2) . oops? Commented Sep 2, 2010 at 9:50

do while in an exit control loop. while is an entry control loop.

answered Mar 19, 2013 at 4:29 31 1 1 bronze badge

While:

  1. entry control 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

Do-while:

  1. exit control 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.
3,743 3 3 gold badges 32 32 silver badges 52 52 bronze badges answered Apr 22, 2013 at 13:04 Unnati shah Unnati shah 31 1 1 bronze badge

The difference is in when the condition gets evaluated. In a do..while loop, the condition is not evaluated until the end of each loop. That means that a do..while loop will always run at least once. In a while loop, the condition is evaluated at the start.

Here I assume that wdlen is evaluating to false (i.e., it's bigger than 1) at the beginning of the while loop, so the while loop never runs. In the do..while loop, it isn't checked until the end of the first loop, so you get the result you expect.

answered Sep 2, 2010 at 9:30 6,067 4 4 gold badges 38 38 silver badges 55 55 bronze badges

Do while loop will be executed atleast once. but while loop will check the condition first and then it may or may not get executed depending on the condition.

In your example wdlen may assume any garbage value which is > 2 so while loop will never get executed.

whereas do while loop will be ececuted and will tell u to enter the value and check that value in terminating condition

answered Sep 2, 2010 at 10:47 4,170 16 16 gold badges 52 52 silver badges 62 62 bronze badges
while(wdlen

If wdlen (assuming it's a stack variable) is not initialized or assigned a value before the while loop is entered, it will contain whatever was in that space in memory before (i.e. garbage). So if the garbage value is < 2, the loop executes, otherwise it doesn't.

do< . >while(wdlen<2) 

will execute once and then checks on condition to run loop again, and this time it might succeed if by chance wdlen which is uninitialized is found to be less than 2.