Loops: while and for Flashcards
what are the 5 loops?
for
for in
for of
do while
while
write a blank while loop
while (condition){
//code
};
f the loop body has a single statement, we can omit the ________.
curly braces
write a blank do while loop
do {
// loop body
} while (condition);
when to use a do while loop?
when you want the body of the loop to execute at least once regardless of the condition being truthy.
the acronym I use for three conditions in a loop
VCI
variable
condition
increment
We can force the exit at any time using the special ____.
break directive.
The _______ directive is a “lighter version” of break. It doesn’t stop the whole loop. Instead, it stops the current iteration and forces the loop to start a new one (if the condition allows).
continue
for (let i = 0; i < 10; i++) {
if (i % 2 == 0) continue;
alert(i);
}
//returns and why?
1, 3, 5, 7, 9
For even values of i, the continue directive stops executing the body and passes control to the next iteration of for (with the next number). So the alert is only called for odd values.
for (let i = 0; i < 10; i++) {
if (i % 2 != 0) continue;
alert(i);
}
//returns and why?
0,2,4,6,8
For even values of i, the continue directive stops executing the body and passes control to the next iteration of for (with the next number). So the alert is only called for even values.