Javascript (Loops) Flashcards
What are loops?
Loops allow us to tell computers to repeat a given block of code on its own so we do not have to write out the same code over and over.
The for
loop
The for loop is one of the most classic loops in programming. We start by defining the 3 properties of the loop with semicolon, and then we add a block that’s executed for every iteration:
for (<initialization>; <condition>; <increment>) {</increment></condition></initialization>
}
This <initialization>; <condition>; <increment> setup is very powerful, because we can do things like looping in the opposite direction, or skip items, or only iterate a portion of an array, but it’s also quite tricky to learn at first.</increment></condition></initialization>
What are the three expressions in a for loop?
- initialization (initializer) - The initialization defines where to begin the loop by declaring (or referencing) the iterator variable
2.a stopping condition- The stopping condition determines when to stop looping (when the expression evaluates to false)
- The iteration statement updates the iterator each time the loop is completed
Ex:
for (let counter = 0; counter < 4; counter++) {
console.log(counter);
}
// The following is printed:
0
1
2
3
What is a do while loop/statement?
The do..while loop might be less powerful than for loop but it’s also simpler to setup. With this loop we do something, which we define in a block, and then we decide if we want to do it again.
We have to define an index variable outside of it before we can use it, so it’s more verbose than for.
const list = [‘a’, ‘b’, ‘c’]
let i = 0
do {
console.log(list[i])
i = i + 1
} while (i < list.length)
Notice that we also have to increment i manually. If we forget to do so, we will create what’s called an infinite loop.
You can interrupt a while loop using break:
do {
if (something) break
} while (true)
and you can jump to the next iteration using continue:
do {
if (something) continue
//do something else
} while (true)
What are two additional actions we can do with a do while loop?
- We can interrupt a while loop using break:
do {
if (something) break
} while (true)
- We can jump to the next iteration using continue:
do {
if (something) continue
//do something else
} while (true)
The while
loop
The while loop is similar to do..while, but there’s a key difference: with while we first check the condition and then (maybe) we do something.
With do..while, if you remember, first we did something and then we checked if we wanted to do it again.
So it’s a similar loop, but for a different use case.
Same as do..while we have to define and increment i manually to avoid an infinite loop.
const list = [‘a’, ‘b’, ‘c’]
let i = 0
while (i < list.length) {
console.log(list[i])
i = i + 1
}
What are two additional actions we can do with a while loop?
- We can interrupt a while loop using break:
while (true) {
if (something) break
}
- We can jump to the next iteration using continue:
while (true) {
if (something) continue
//do something else
}
When should you use a while loop?
Its best to use the while loop when we don’t know in advance how many times the loop should run.
Think of it like eating. When you are eating you don’t know how many bites you are going to need to become full. You continue to eat while you are hungry.
What is the break keyword?
The break keyword allows programs to “break” out of the loop from within the loop’s block.
Ex:
for (let i = 0; i < 99; i++) {
if (i > 2 ) {
break;
}
console.log(‘Banana.’);
}
console.log(‘Orange you glad I broke out the loop!’);
//Print
Banana.
Banana.
Banana.
Orange you glad I broke out the loop!
How do we run a backward for loop?
- Set the iterator variable to the highest desired value in the initialization expression.
- Set the stopping condition for when the iterator variable is less than the desired amount.
- The iterator should decrease in intervals after each iteration.
What are nested loops?
When we have a loop running inside another loop, we call that a nested loop.
What is one way we can use a nested for loop?
To compare the elements in two arrays.
Ex:
const myArray = [6, 19, 20];
const yourArray = [19, 81, 2];
for (let i = 0; i < myArray.length; i++) {
for (let j = 0; j < yourArray.length; j++) {
if (myArray[i] === yourArray[j]) {
console.log(‘Both arrays have the number: ‘ + yourArray[j]);
}
}
}
What are infinite loops?
a loop that executes a statement or a block of statements repeatedly, without a guarding condition to determine its end
The for-of
loop
The for…of loop is a very simple kind of loop.
With this loop you don’t have to worry about the index.
const list = [‘a’, ‘b’, ‘c’]
for (const item of list) {
console.log(item)
}
It’s much simpler but one downside is that we don’t have a way to get the index value, which is often useful.
What can we do to combat the downside of the for-of loop (where we don’t have a way to get the index value)
In this case we use a “trick” by combining the array destructuring syntax and calling the entries() method on the array, which we’ll talk more about later on when we’ll get to iterators:
for (const [index, value] of [‘a’, ‘b’, ‘c’].entries()) {
console.log(index)
console.log(value)
}
Notice the use of const when we define item. This loop creates a new scope in every iteration, so we can safely use that instead of let. This is not something we can do for the for loop, where we have to use let.