Loops: while and for Flashcards

1
Q

what are the 5 loops?

A

for
for in
for of
do while
while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

write a blank while loop

A

while (condition){
//code
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

f the loop body has a single statement, we can omit the ________.

A

curly braces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

write a blank do while loop

A

do {
// loop body
} while (condition);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

when to use a do while loop?

A

when you want the body of the loop to execute at least once regardless of the condition being truthy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

the acronym I use for three conditions in a loop

A

VCI

variable
condition
increment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

We can force the exit at any time using the special ____.

A

break directive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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).

A

continue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

for (let i = 0; i < 10; i++) {
if (i % 2 == 0) continue;
alert(i);
}

//returns and why?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

for (let i = 0; i < 10; i++) {
if (i % 2 != 0) continue;
alert(i);
}

//returns and why?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly