For Loops - Beau teaches JavaScript

**Introduction to Loops**

Loops are a fundamental concept in programming that allow us to execute a block of code repeatedly for a specified number of times or until a certain condition is met. In this article, we will explore the basics of loops, including types of loops, how to use them, and examples of different loop structures.

**Pushing Zero onto an Array**

One common type of loop is the `for` loop, which allows us to execute a block of code repeatedly for a specified number of times. In this example, we have an array variable `r`, which contains numbers from 0 to 4. We want to push these numbers onto another array. To do this, we initialize a variable `i` to 0 and use the `for` loop to execute a block of code that pushes each number onto the array. The loop will continue until `i` is greater than or equal to the length of the array.

`// Initialize i to 0`

`let i = 0;`

`// Loop through the array and push numbers onto another array`

for (let r of [0, 1, 2, 3, 4]) {

// Push each number onto the array

push(r);

}

**Incrementing a Variable**

After we have pushed all the numbers onto the array, we need to increment `i` to move on to the next iteration of the loop. This is done using the increment operator `++`. In this case, after we finish executing the block of code for each number, we will increment `i` by 1.

`// Increment i by 1`

`i++;`

**Breaking Out of a Loop**

Another common use of loops is to break out of them early. This can be done using the `break` statement. In this example, we want to break out of the loop as soon as `i` is greater than 2. We add the `break` statement after the increment operator.

`// Use the break statement to exit the loop`

for (let r of [0, 1, 2, 3, 4]) {

// Push each number onto the array

push(r);

if (i > 2) {

// Break out of the loop

break;

}

}

**Iterating Through an Array**

Loops can also be used to iterate through arrays. In this example, we have an array `r` and we want to console.log each item in it. We initialize a variable `i` to 0 and use the `for` loop to execute a block of code that console.log's each item in the array.

`// Initialize i to 0`

`let i = 0;`

`// Loop through the array and console.log each item`

for (let r of [1, 2, 3, 4]) {

// Console.log each item

console.log(r);

// Increment i by 1

`i++;`

}

**Nested For Loops**

When we have multi-dimensional arrays, we can use nested for loops to iterate through both the array and any subarrays. In this example, we have a nested array with three inner arrays. We want to console.log each item in the outer array, as well as each item in the inner arrays.

`// Initialize i to 0`

`let i = 0;`

`// Loop through the outer array`

for (let r of [1, 2, 3]) {

`// Initialize J to 0`

let j = 0;

`// Loop through the inner arrays`

for (let s of r) {

// Console.log each item

console.log(s);

// Increment J by 1

`j++;`

}

// Increment i by 1

`i++;`

}

**Conclusion**

In conclusion, loops are a fundamental concept in programming that allow us to execute a block of code repeatedly for a specified number of times or until a certain condition is met. We have explored the basics of loops, including types of loops, how to use them, and examples of different loop structures. With these concepts under our belt, we can now write more complex programs that manipulate arrays and perform calculations.

"WEBVTTKind: captionsLanguage: enfor Loops you can run the same code multiple times using a loop the most common Loop in JavaScript is called a for Loop because it runs four a specific number of times here's the structure of the for Loop you always start with the word four and then you're going to have something in these parentheses you usually have three items there's the initialization and then you put a semicolon then you have the condition and then a semicolon and then the final expression and then the whatever is going to run in the for Loop would be in these curly braces over here now all of these things are optional but you have them in most Flor Loops the initialization is run just once to set up the the loop variable and every time the loop is run the condition statement is evaluated at the beginning the loop keeps going until the condition is false and then the final expression is evaluated at the end of each Loop iteration and is usually used to increment or decrement your Loop counter let me give you an example of this so in this example first outside of the for loop we're going to create an array called R array and set it to an empty array now we have the for Loop this is the initialization we are going to create the variable I and set that I equal to zero now here's the condition I is less than five so we are going to keep running the for Loop while I is less than five so as long as I is less than five we'll continue running this for loop at the end of each run of the loop we are going to increment I i+ plus just means we're going to add one to I so it's going to go through the loop the first time it's going to run this code within the brackets and it's going to push I onto the the array and remember I starts off with Z at zero it's going to push zero onto the array and then it's going to increment I remember it does this at the end so now I is one it's going to go through is going to push one onto the array it's going to go through push two push three push four after four we'll increment I to five before the for Loop goes it's going to check is I less than five no I is actually five I is not less than five so it's going only going to put push the number 0 through four onto the array let's check that out by console. logging it so yeah we got the array 0 1 2 3 4 you can also use a break statement to break out of a loop early so now I have if I is more than two break so if we run that it's just going to put 0 one two because after two I is more than two so it breaks out of the loop and then we're done with the loop now it's also common for Loops to iterate through an array so if an array already exists and has some items in it we can Loop through and and console.log or print out each item in that array so we're going to initialize I to equal Zer if I is less than r. length so that's the array and length is the length of the array so if I is less than the length of the array we are going to continue going through the array so once we get to the end of the array we'll be done with the for loop at the end we're going to increment I by One and we're just going to console.log R and then we just put the I variable as the index that we're of the array that we're trying to console. log so the first we're going to get the array index zero because I will equal Z and index 0 will be 10 then we'll do index one 2 3 4 because each run through the array we're going to increment I up one and that's going to go to the next index of the array if we run that see we got 10 9 876 if you have a multi-dimensional array you can use nested for Loop Loops to Loop through both the array and any subarrays let me show you an example of that okay so let's look at this we have this array variable which is a nested array so here's just there's one array there's the beginning of the array and there's the end of the array and inside the array are three other arrays so inside the first array here's index zero here's index one here's index two and in each of those indexes is another array so we have index zero and index one in each array so you can use nested for Loops to Loop through these both arrays here's our first for Loop here's the end of that and then here's our nested for Loop so we're going to initialize I to zero if I is less than array. length and the length of the array see we have 1 2 3 so the length of the array is three but now inside the the nested for Loop we have to initialize the variable differently so now instead of I we have J now if J is less than array index i. length when we're looking at the array index i. length that's the first item in the array which is the first array within the array so now the length of this is just two we have one two so we're going to then we're going to increment J so we're going to run through this first floor Loop and inside the first for loop we're basically going to go into the first array and now we're going to console.log each item in the first array we're going to array index I index J so the first index is what index in this first array which which would be if we're going to index zero that's this and then the index J will be what index in the second array in the nest array so we would get index zero in index one for J it's going to go through this inner for Loop to get both indexes in the first array then it's going to go back to the outer for Loop to bump up to this next array and then we're going to go through the inner four loop again to get both indexes in here and so on if we log if we conso that log that it's going to say 10 9 8 7 6 1 2 3 4 5 6 so the first 109876 was from up here and then we have the 1 2 3 4 5 6 from down here well the those are for loops and nested for Loops thanks for watching my name is Bo KS don't forget to subscribe and remember use your code for goodfor Loops you can run the same code multiple times using a loop the most common Loop in JavaScript is called a for Loop because it runs four a specific number of times here's the structure of the for Loop you always start with the word four and then you're going to have something in these parentheses you usually have three items there's the initialization and then you put a semicolon then you have the condition and then a semicolon and then the final expression and then the whatever is going to run in the for Loop would be in these curly braces over here now all of these things are optional but you have them in most Flor Loops the initialization is run just once to set up the the loop variable and every time the loop is run the condition statement is evaluated at the beginning the loop keeps going until the condition is false and then the final expression is evaluated at the end of each Loop iteration and is usually used to increment or decrement your Loop counter let me give you an example of this so in this example first outside of the for loop we're going to create an array called R array and set it to an empty array now we have the for Loop this is the initialization we are going to create the variable I and set that I equal to zero now here's the condition I is less than five so we are going to keep running the for Loop while I is less than five so as long as I is less than five we'll continue running this for loop at the end of each run of the loop we are going to increment I i+ plus just means we're going to add one to I so it's going to go through the loop the first time it's going to run this code within the brackets and it's going to push I onto the the array and remember I starts off with Z at zero it's going to push zero onto the array and then it's going to increment I remember it does this at the end so now I is one it's going to go through is going to push one onto the array it's going to go through push two push three push four after four we'll increment I to five before the for Loop goes it's going to check is I less than five no I is actually five I is not less than five so it's going only going to put push the number 0 through four onto the array let's check that out by console. logging it so yeah we got the array 0 1 2 3 4 you can also use a break statement to break out of a loop early so now I have if I is more than two break so if we run that it's just going to put 0 one two because after two I is more than two so it breaks out of the loop and then we're done with the loop now it's also common for Loops to iterate through an array so if an array already exists and has some items in it we can Loop through and and console.log or print out each item in that array so we're going to initialize I to equal Zer if I is less than r. length so that's the array and length is the length of the array so if I is less than the length of the array we are going to continue going through the array so once we get to the end of the array we'll be done with the for loop at the end we're going to increment I by One and we're just going to console.log R and then we just put the I variable as the index that we're of the array that we're trying to console. log so the first we're going to get the array index zero because I will equal Z and index 0 will be 10 then we'll do index one 2 3 4 because each run through the array we're going to increment I up one and that's going to go to the next index of the array if we run that see we got 10 9 876 if you have a multi-dimensional array you can use nested for Loop Loops to Loop through both the array and any subarrays let me show you an example of that okay so let's look at this we have this array variable which is a nested array so here's just there's one array there's the beginning of the array and there's the end of the array and inside the array are three other arrays so inside the first array here's index zero here's index one here's index two and in each of those indexes is another array so we have index zero and index one in each array so you can use nested for Loops to Loop through these both arrays here's our first for Loop here's the end of that and then here's our nested for Loop so we're going to initialize I to zero if I is less than array. length and the length of the array see we have 1 2 3 so the length of the array is three but now inside the the nested for Loop we have to initialize the variable differently so now instead of I we have J now if J is less than array index i. length when we're looking at the array index i. length that's the first item in the array which is the first array within the array so now the length of this is just two we have one two so we're going to then we're going to increment J so we're going to run through this first floor Loop and inside the first for loop we're basically going to go into the first array and now we're going to console.log each item in the first array we're going to array index I index J so the first index is what index in this first array which which would be if we're going to index zero that's this and then the index J will be what index in the second array in the nest array so we would get index zero in index one for J it's going to go through this inner for Loop to get both indexes in the first array then it's going to go back to the outer for Loop to bump up to this next array and then we're going to go through the inner four loop again to get both indexes in here and so on if we log if we conso that log that it's going to say 10 9 8 7 6 1 2 3 4 5 6 so the first 109876 was from up here and then we have the 1 2 3 4 5 6 from down here well the those are for loops and nested for Loops thanks for watching my name is Bo KS don't forget to subscribe and remember use your code for good\n"