41

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?

Is there any other difference in these two?

2
  • 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

67

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.

4
  • "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?
    – Joost
    Commented Sep 2, 2010 at 9:33
  • Thanks buddy. I understood. Any other difference in these two? Commented Sep 2, 2010 at 9:35
  • 2
    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.
    – AndreKR
    Commented Oct 7, 2012 at 14:22
21

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.

4
  • tnx for good replay.There is only one difference??is there other difference??
    – AminM
    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.
    – Alan Corey
    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.
    – Dominique
    Commented Dec 12, 2018 at 13:06
3
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.

1
  • "probably starts off less than 2"... while(wdlen<2)... oops?
    – Stephen
    Commented Sep 2, 2010 at 9:50
3

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

3

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.

2

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.

1

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

1
while(wdlen<2){
  ...
 }  

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.

0

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.

3
  • Larger than 2, I assume you mean?
    – Stephen
    Commented Sep 2, 2010 at 9:30
  • @Stephen: yes, changed that :)
    – sth
    Commented Sep 2, 2010 at 9:31
  • 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
    Commented Sep 2, 2010 at 9:33
0

while test the condition before executing statements within the while loop.

do while test the condition after having executed statement within the loop.

source: let us C

1
  • 2
    This question is 3 years old, and has already an answer... +1 for being a hero.
    – user1585121
    Commented Jul 2, 2014 at 13:38
0

while test the condition before executing statements in the while loop.

do while test the condition after having executed statement inside the loop.

2
  • 1
    You're not wrong, but it's maybe a bit late to be answering this? Commented Dec 16, 2014 at 8:28
  • may be it's late but I wanted to answer his question with a simple / short response Commented Dec 17, 2014 at 9:00
0

In WHILE first check the condition and then execute the program In DO-WHILE loop first execute the program at least one time then check the condition

1
  • 2
    6 years too late. There were already 12 answers to the question.
    – hpm
    Commented Oct 17, 2016 at 6:40
0

The difference between do while (exit check) and while (entry check) is that while entering in do while it will not check but in while it will first check

The example is as such:

Program 1:

int a=10;
do{
System.out.println(a);
}
while(a<10);

//here the a is not less than 10 then also it will execute once as it will execute do while exiting it checks that a is not less than 10 so it will exit the loop

Program 2:

int b=0;
while(b<10)
{
System.out.println(b);
}
//here nothing will be printed as the value of b is not less than 10 and it will not let enter the loop and will exit

output Program 1:

10

output Program 2:

[nothing is printed]

note:

output of the program 1 and program 2 will be same if we assign a=0 and b=0 and also put a++; and b++; in the respective body of the program.

0

While Loop:

while(test-condition)
{
      statements;
      increment/decrement;
}
  1. Lower Execution Time and Speed
  2. Entry Conditioned Loop
  3. No fixed number of iterations

Do While Loop:

do
{
      statements;
      increment/decrement;
}while(test-condition);
  1. Higher Execution Time and Speed
  2. Exit Conditioned Loop
  3. Minimum one number of iteration

Find out more on this topic here: Difference Between While and Do While Loop

This is valid for C programming, Java programming and other languages as well because the concepts remain the same, only the syntax changes.

Also, another small but a differentiating factor to note is that the do while loop consists of a semicolon at the end of the while condition.

0

The difference between a while constructs from Step 1 versus a do while is that any expressions within the do {} will be running at least once regardless of the condition within the while() clause.

println("\nStep 2: How to use do while loop in Scala")
var numberOfDonutsBaked = 0
do {
  numberOfDonutsBaked += 1
  println(s"Number of donuts baked = $numberOfDonutsBaked")
} while (numberOfDonutsBaked < 5)

Here is detail explaination: Explanation Visit: coderforevers

0

The most important difference between while and do-while loop is that in do-while, the block of code is executed at least once, even though the condition given is false.

To put it in a different way :

  • While- your condition is at the begin of the loop block, and makes possible to never enter the loop.
  • In While loop, the condition is first tested and then the block of code is executed if the test result is true.

Not the answer you're looking for? Browse other questions tagged or ask your own question.