Do While

Do...While Statement Loops

The do...while statement loop repeats until a specified condition comes back as FALSE. The statement (i.e. i+= 1;) executes once before the condition is checked; to execute multiple statements, use a block statement (i.e. {...}) to group the statements together. If the condition (i.e. i < 5) is TRUE, the statement will loop again. At the end of every execution, the condition is checked. When the condition is FALSE, the loop stops and control passes.

//do while loops

var i = 0; 
do /* do */ {
  i += 1; /* statement */
  console.log(i);
} while (i < 5); /* while (condition) */

// Practice

var text = "";
var i = 0;

do { 
  text += "The number is: " + i;
  i++;
} while (i < 10); // "The number is: 0", "The number is: 1", ... "The number is: 10"

Last updated