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,690 points
1,153 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,730 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
9,494 views
0 votes
1 answer

What is the difference between list and tuple?

List has [ ] brackets while tuple ...READ MORE

answered May 11, 2021 in Python by Mohit Khurana

edited Mar 5, 2025 8,271 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
2,266 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
1,461 views
0 votes
1 answer

Difference between pop and delete methods

Using delete leaves undefined holes in the ...READ MORE

answered Jan 18, 2022 in Python by saurabh thapa

edited Mar 5, 2025 2,980 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
8,915 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
2,111 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 17,482 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
1,489 views
0 votes
1 answer

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

copy the path the tesseract-ocr is stored ...READ MORE

answered Aug 29, 2023 in Python by swagger khan

edited Mar 5, 2025 45,182 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