This article will show you how a JavaScript do while loop is constructed, and what it us used for, with code examples.
do while loops are one of the simplest kinds of loops, and appear in most programming languages. A do while loop runs a block of code repeatedly, until a condition is met. When the do while condition is met, the loop stops.
It’s important to get familiar with using loops – and understanding how loops work – as they form the backbone of just about every game and application you might write. Make sure you’re comfortable with how loops work, when the condition is checked, and how many times a loop is expected to execute, so your application behaves in the way you intend!
Loops are used to iterate through arrays, used to count, and used to retry actions. For example, loops are used to display table rows on screen (looping over each record in a database and printing it), and used to move enemies around on-screen in video games (looping at a set time interval and updating the position of objects on-screen).
What is a do while Loop?
A do while loop will execute a block of code repeatedly while a given condition is true. When that condition changes to false, the loop will exit and the code following it will continue executing.
It is similar to the while loop, except that the condition which causes the loop to repeat or exit is checked at the end of the loop, rather than at the beginning.
As the conditions which determine whether the loop should continue or not are checked at the end of the loop, the code in the loop will always run at least once.
JavaScript do while Loop Syntax
The JavaScript do while loop has the following syntax:
do { STATEMENTS } while (CONDITION);
Note that:
- CONDITION can be any JavaScript expression which evaluates to a boolean true/false value
- Unlike while loops, in a do while the condition appears at, and is checked at, the end of the loop, rather than the beginning
- This makes the do while loop behave slightly differently – The STATEMENTS inside the loop will always execute at least once
- STATEMENTS can be any number of lines of JavaScript code which will be executed every time the loop repeats
- The do while loop will execute the STATEMENTS so long as the CONDITION expression evaluates as true or truthy
- So, to break out of the do while loop, your STATEMENTS will need to change something in the CONDITION so that it can no longer be considered true
JavaScript do while Loop Examples
Below are some examples of while loops. The statements inside each loop will execute until the loop condition is no longer true.
Notice that the code being executed in the loop changes the condition of the loop so that it exits. Also notice that the statements within the loop will execute at least once due to the condition being checked at the end of the loop rather than the beginning.
var i = 1; do { console.log(i); i++; } while (i <= 5);
Above, a do while loop is executed with the condition that the variable i is less than or equal to 5. The code statements executed by the loop increment the value of i after printing it – so that eventually the condition which exits the loop is met.
As the condition is checked at the end of the loop, even if the initial value of i is greater than 5, the loop will run once and print the initial value of i and increment it.
Watch Out for Infinite Loops!
If your loop never reaches a condition which will cause it to exit, your loop will run indefinitely, freezing your application! This is called an infinite loop, and should be avoided at all costs!