Loops Flashcards
1
Q
while
A
While (condition) { //while this is true //excite code }
while (numb < 20) {
console.log(numb++);
}
2
Q
do while
A
do {
numb+=1;
console.log(numb);
} while (numb < 20);
• do while executes code block first prior to conditional statement i
3
Q
for
A
for (var i = 0; i<20; i++) { console.log(i); } each time the loop is run 1 is added to the i variable. The loop terminates when i equals 20 • each ; section can have more than 1 expression which you can do by separating with , see next card
4
Q
For loop multi-expression
A
for (var i =0, phrase="hi mom"; i<20; i++) { console.log(phrase + " " + I); }
• notice the , separating the multi-expressions.
5
Q
Nested for loop
A
for (i=1; i<=4; i++) { console.log(i + " Outer loop"); for (j=1; j<=4;j++) { console.log(j + " inner loop"); } } • first loop iterates once. Inner loop 4 times. Then first loop 1 inner loop 4; etc
6
Q
Loop
A
• used to repeat statements or task
7
Q
conditional
A
- a statement that executes code after checking a condition.
- if something is true, do this. Otherwise, do that
8
Q
Loop
A
- As long as something is true, keep doing this
* e.g. As long as you are thirsty, keep drinking water.