do while
Loopdo while
Loopdo while
loopdo while
loopdo while
loop with Break Statementdo while
LoopThe JavaScript do-while
loop is also known as an exit control loop. The JavaScript do-while
is test specified condition after executing a block of code. If the condition met false, at least once execute the block of code.
do while
Loopdo{
statement(s);
} while(expression);
The do-while
loop always executes the block of code least once before it check specified condition.
Because the condition is checked only after the block of code executes once. The do-while
loop is called a post-test/exit control loop.
You have known above, the do while
loop is known as an exit control loop. For this reason, it is possible that the block of code
inside the do while
loop is executed at least once, if the specified condition met false.
do while
loopdo while
loopSee the following example of the do-while
loop.
<script>
let count = 0;
do {
count++;
document.write(count + "<br>")
} while (count < 10);
</script>
How the script works:
count
variable and assign value to it.do while
loop checks specified condition after execute block of code, if count
is less than 10
and execute the statements inside the loop body.count
by 1
.And After 10
iterations, the condition count < 10
met false and loop will be terminate.The output of the script shown below:
1
2
3
4
5
6
7
8
9
10
do while
loop with Break StatementSee the following example of the do-while
loop.
<script>
let count = 0;
do {
if(count == 5)
break;
count++;
document.write(count + "<br>")
} while (count < 10);
</script>
How the script works:
count
variable and assign value to it.do while
loop checks specified condition after executing block of code, if count
is less than 10
and execute the statements inside the loop body.count
by 1
. Using a break statement, stop the execution of loop After 5
iterations.The output of the script shown below:
1
2
3
4
5
In this tutorial, you have learned what is do-while
loop and how to use the do-while
loop.
☞ JavaScript Programming Tutorial Full Course for Beginners
☞ Learn JavaScript - Become a Zero to Hero
☞ Javascript Project Tutorial: Budget App
☞ E-Commerce JavaScript Tutorial - Shopping Cart from Scratch