do/while Loops

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 the 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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.241.101