Do…While Loop
The do…while loop is similar to the while loop, but the code inside the loop body is executed at least once, even if the condition is false. The syntax for a do…while loop is as follows:
do {
// code to be executed repeatedly
} while (condition);
The condition statement is evaluated after each iteration of the loop. If the condition is true, the code inside the loop body is executed. If the condition is false, the loop terminates.
Here is an example of a do…while loop:
Task
Edit the above code to print 0 to 9. Run the above code and experiment with the code to get used to it.
JavaScript Loops Summary
Here is a summary of the JavaScript loops that were covered in this chapter:
- The for loop is the most common type of loop in JavaScript.
- The while loop is used to execute code repeatedly as long as a condition is true.
- The do…while loop is similar to the while loop, but the code inside the loop body is executed at least once, even if the condition is false.