+ 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?
3 odpowiedzi
+ 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.
+ 4
The code block is ran in:
-while loop after condition is checked
-do-while loop before condition checked
+ 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.