xxxxxxxxxx
/*
`do while` is essentially like the basic `while` statement
except for one key difference, the body is always executed
at least once.
Syntax:
do {
body
} while (condition);
This is generally only useful if you need the body to run
at least once, before the condition is checked.
*/
let i = 10;
do {
console.log(i);
i--;
} while (i > 0);
/*
Outputs:
10
9
8
7
6
5
4
3
2
1
*/
xxxxxxxxxx
var i=0;
while (i < 10) {
console.log(i);
i++;
}
//Alternatively, You could break out of a loop like so:
var i=0;
while(true){
i++;
if(i===3){
break;
}
}
xxxxxxxxxx
let arr = ['jan', 'feb', 'mar', 'apr', 'may'], i = 0;
// do something at least 1 time even if the condition is false
do{
console.log(arr[i]);
i++;
}while(arr.includes('dec'));
// output: jan
xxxxxxxxxx
let i =0
do{
console.log(i)
i++;
}
while(i>10);// even i =0 which is not greater then 10 it will console.log 0 for
//time as it execute code at least first time
xxxxxxxxxx
let count = 0;
let max = 10;
while (count < max) {
console.log(count)
count = count + 1
}
xxxxxxxxxx
let cupsOfSugarNeeded = 8; //Specific amount of cups needed
let cupsAdded = 0; //Initialize the cups to zero
do {
cupsAdded ++; //Add sugar one at a time
} while (cupsAdded < cupsOfSugarNeeded); //Keep adding while the cups is less than the amount needed
xxxxxxxxxx
var i = 1;
var msg = '';
while(i < 10) {
msg += i + ' x 5 = ' + (i + 5) + '<br />';
i++;
}
document.getElementById('answer').innerHTML = msg;