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

What is the difference between a while and do while loop

JavaScript

8th Oct 2019, 8:29 AM
Emmanuel Adeniyi
5 Answers
+ 9
The 'while' and 'do...while' statements in JavaScript are similar to conditional statements, which are blocks of code that will execute if a specified condition results in 'true'. • 'while' and 'do...while' loops are conditionally based. • 'while' statement is a loop that executes as long as the specified condition evaluates to 'true'. • 'do...while' statement, which is very similar to 'while' with the major difference being that a 'do...while' loop will always execute once, even if the condition is never true.
8th Oct 2019, 2:59 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 6
While loop is entry controlled, do-while is exit controlled.
8th Oct 2019, 9:20 AM
Sonic
Sonic - avatar
+ 4
do while translates to: Do this once, and then check, if you do it again. while translates to: Do this only if and while the condition checks out (so maybe not even once).
8th Oct 2019, 10:53 AM
HonFu
HonFu - avatar
+ 1
Let understand by example While(not dead) { Code Eat Sleep } Do { Code Eat Sleep While(not dead )
8th Oct 2019, 11:25 AM
Programmer Raja
Programmer Raja - avatar
0
Simply do while loop always runs it's statements atleast once, unlike original while loop. while loop follows: check condition condition false? -> break condition true? -> run statements once check condition again condition false? -> break condition true? -> run statements once check condition again (loop until break) do while loop follows: run statements once (The only difference) check condition condition false? -> break condition true? -> run statements once check condition again condition false? -> break condition true? -> run statements once check condition again (loop until break) do { //statements } while (condition) Equals: while (true) { //statements if (condition) {break;} }
8th Oct 2019, 8:49 AM
Seb TheS
Seb TheS - avatar