Why does this run once? | Sololearn: Learn to code for FREE!
+ 1

Why does this run once?

Why does the following code only run once? do {} while(false)

26th Mar 2017, 8:32 PM
Benjamin Meyer
Benjamin Meyer - avatar
3 Answers
+ 11
do while is an exit control loop, meaning condition will be checked after execution. So, loop runs once, check condition, exits if false.
27th Mar 2017, 1:11 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 8
because the condition to stop the loop comes after the code to be executed in the loop. read it like: do the following. repeat it while xxx is true. these are two commands. your program does the first before it knows the second.
26th Mar 2017, 8:47 PM
Mario L.
Mario L. - avatar
do-while loops run at least once. The DO part runs, then the WHILE is checked for loop condition and since it's false, the execution of the loop stops
27th Mar 2017, 6:08 PM
Arian Mozafari
Arian Mozafari - avatar