What is difference between while loop and do-while loop ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is difference between while loop and do-while loop ?

Besides the difference in syntax, what exactly is the difference between these two and what are the conditions in which one would be preferred over the other?

19th Jan 2020, 2:15 PM
Gulfam Samoo
Gulfam Samoo - avatar
3 Answers
+ 6
I believe the difference is that the while loops runs zero times if a condition is already met, whereas a do-while loop runs at least once if a condition is met. As for which one is better, it depends on whether you want a loop to run at least one time or not.
19th Jan 2020, 2:24 PM
Jianmin Chen
Jianmin Chen - avatar
+ 4
The code block is ran in: -while loop after condition is checked -do-while loop before condition checked
19th Jan 2020, 2:29 PM
Seb TheS
Seb TheS - avatar
+ 1
Both loops repeat a statement or a set of statements. In do while loop as the condition is written at end in syntax : do{ statement while(condition)} so when we run the program , loop body statements will must execute at least once even if condition is not satisfied. But in while loop , it depends all on condition and it will execute them only when condition is satisfied.
19th Jan 2020, 4:33 PM
Muhammad Umais
Muhammad Umais - avatar