tags:

views:

209

answers:

6

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<2){
        printf("Word length... ");
        scanf("%d", &wdlen);
    } 

It gives a blank screen. It do not work. So there is some functional difference between both loops. Can anybody explain it? Thanks in advance.

EDIT : Is there any other difference in these two ?

+8  A: 

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 (assumedly starts at 0) and will never be > 2.

[Update]

That was a test for you all :)

hydrogen
"wdlen (assumedly starts at 0) and will never be < 2" - back in my math class I was taught that 0 < 2 ;-)
Péter Török
0 < 2 == false? Are you sure?
JoostK
But surely `0<2`!
James
@hydrogen Thanks buddy. I understood. Any other difference in these two?
Only semantics my friend.
hydrogen
+3  A: 

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.

Guillaume Lebourgeois
A: 

Probably wdlen starts with a value >=2, so in the second case the loop condition is initially false and the loop is never entered.

In the second case the loop body is executed before the wdlen<2 condition is checked for the first time, so the printf/scanf is executed at least once.

sth
Larger than 2, I assume you mean?
Stephen
@Stephen: yes, changed that :)
sth
@sth Heh, well caught. Your change actually made me catch an error in my answer (I had said "it must be greater than 2"!), so cheers ;).
Stephen
+1  A: 

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.

Stephen
A: 
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<2){
    printf("Word length... ");
    scanf("%d", &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.

Mahmoud
"probably starts off less than 2"... `while(wdlen<2)`... oops?
Stephen
oops indeed, my friend.
Mahmoud
+1  A: 

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

SPB