Execute the same block of code a specified number of times or while a specified condition is true.
Back
while loop
Front
A statement that creates a loop while a specified condition is true. The loop will continue to run as long as the condition is true. It will only stop when the condition becomes false.
code example
var start = 0; // when to start
while (start < 10) { // when to stop
console.log(start);
start = start + 2; // how to get to the next item
}
Back
nested loops
Front
a loop that is contained within another loop
code example
for (var x = 0; x < 5; x = x + 1) {
for (var y = 0; y < 3; y = y + 1) {
console.log(x + "," + y);
}
}
Back
increment
Front
To increase, usually by 1; the opposite of decrement
code example
x++ or ++x // same as x = x + 1
x += 3 // same as x = x + 3
Back
for loop
Front
Loops that have a explicitly defined beginning, end, and increment (step interval).
syntax
for ( start; stop; step ) {
// do this thing
}
code example
for (var i = 0; i < 6; i = i + 1) {
console.log("Printing out i = " + i);
}
Back
decrement
Front
To decrease, usually by 1; the opposite of increment.
code example
x-- or --x // same as x = x - 1
x -= 6 // same as x = x - 6
Back
3 parts of a while loop
Front
When to start: The code that sets up the loop — defining the starting value of a variable for instance
When to stop: The logical condition to test whether the loop should continue.
How to get to the next item: The incrementing or decrementing step — for example, x = x * 3 or x = x - 1