+ 1
What is the difference between a while and do while loop
JavaScript
6 Respostas
+ 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.
+ 6
While loop is entry controlled, do-while is exit controlled.
+ 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).
+ 1
Let understand by example
While(not dead)
{
Code
Eat
Sleep
}
Do
{
Code
Eat
Sleep
While(not dead )
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;}
}