Explore Loops
Loops
Let's explore loops!
Explore:
- Change the numbers in each of these loops to see how it affects the output.
- Write your own while, do...while, and for loop.
// This is a while looplet i = 0;while (i < 4) {console.log('This piece of code ran. i equals: ' + i);i++;}
// This is a do...while looplet i = 0;do {console.log('This piece of code ran. i equals: ' + i);i++;} while (i < 4)
// This is a for loopfor (let i = 0; i < 4; i++) {console.log('This piece of code ran. i equals: ' + i);}// This is a for loop going in the opposite directionfor (let j = 10; j > 0; j--) {console.log('This piece of code ran. i equals: ' + i);}// Note that i is a variable so you can rename it any valid variable name.// i is a common name for an iterator variable. If you already have an i variable j is also common.for (let thisIsAVariableYouCanNameItWhateverYouWant = 0; thisIsAVariableYouCanNameItWhateverYouWant < 2; thisIsAVariableYouCanNameItWhateverYouWant++) {console.log('This piece of code ran. thisIsAVariableYouCanNameItWhateverYouWant equals: ' + thisIsAVariableYouCanNameItWhateverYouWant);}