Difference between while loop and do while loop

0 votes


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

do { printf("Word length... "); scanf("%d", &wdlen); } while(wdlen<2);


This code works perfectly and also prints word length and tascans the input but, once I changed it to

while(wdlen<2){ printf("Word length... "); scanf("%d", &wdlen); }

It gives a blank screen and fails to work. So there are some functional differences between both the loops. Can anybody explain this to me and also could I know if there is any difference between them?

Feb 8 in Python by Rahul
• 9,680 points
94 views

1 answer to this question.

0 votes

The do while loop helps in executing the content of the loop once before checking the condition of the while. Wheras, a while loop will check the condition prior to executing the content. In this case you are waiting for a user input with scanf(), which will never execute in the while loop as wdlen which is not initialized and probably just contains a garbage value which could be greater than 2.

answered Feb 8 by Soham
• 9,670 points

Related Questions In Python

0 votes
1 answer

What's the difference in Qt between setVisible, setShown and show/hide

show() is just a convenience function for ...READ MORE

answered Apr 17, 2018 in Python by anonymous
6,065 views
0 votes
1 answer

What is the difference between list and tuple?

Lists are mutable(values can be changed) whereas ...READ MORE

answered May 5, 2018 in Python by aayushi
• 750 points
5,293 views
0 votes
2 answers

Do-while loop in Python

A more pythonic way would be this: ``` while ...READ MORE

answered Jul 6, 2020 in Python by Composer3
704 views
0 votes
1 answer

Difference between '==' and 'is'

'==' checks for the equality of the ...READ MORE

answered May 14, 2018 in Python by Nietzsche's daemon
• 4,260 points
329 views
0 votes
1 answer

Difference between pop and delete methods

I think you mean del() and not ...READ MORE

answered Jun 25, 2018 in Python by Hamartia's Mask
• 1,580 points
1,134 views
0 votes
1 answer

Why there is no do while loop in python

There is no do...while loop because there ...READ MORE

answered Aug 6, 2018 in Python by Priyaj
• 58,100 points
4,811 views
0 votes
1 answer

How to use nested if else statement in python?

The working of nested if-else is similar ...READ MORE

answered Nov 19, 2018 in Python by Nabarupa
678 views
0 votes
1 answer

How to write inline if statement for print in Python?

Inline if-else expression must always contain the else ...READ MORE

answered Dec 4, 2018 in Python by Nymeria
• 3,560 points

edited Dec 6, 2018 by Nymeria 15,623 views
0 votes
1 answer

what is an else if statement in python?

if condition:    statement elif condition:     statement #this is how you use ...READ MORE

answered Mar 15, 2019 in Python by Waseem
• 4,540 points
362 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP