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, 2022 in Python by Rahul
• 9,680 points
652 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, 2022 by Soham
• 9,710 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
8,011 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
6,996 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
1,400 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
789 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
2,123 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,020 points
7,659 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
1,263 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 16,751 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
833 views
0 votes
1 answer

Pytesseract : "TesseractNotFound Error: tesseract is not installed or it's not in your path", how do I fix this?

Based on my recent experience with this ...READ MORE

answered Feb 16, 2022 in Python by Soham
• 9,710 points
37,697 views
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