Another type of while
loop is the do
/while
loop. This is useful if you always want to execute the code in the loop at least once and the expression cannot be tested until the code has executed at least once.
For example, the following do
/while
loop executes until days
is equal to Wednesday
:
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
var i=0;
do{
var day=days[i++];
console.log("It's " + day);
} while (day != "Wednesday");
This is the output at the console:
It's Monday
It's Tuesday
It's Wednesday