For Loops - Beau teaches JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
for 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 for a specific number of times here is the structure of the for loop we also thought for and then you're going to have something in these parentheses you usually have three items there's the initialization and then you could have 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 four loops the initialization is run just once to set up 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 our 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 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 on to the the array and run our eye starts off with zero at zero it's going to push zero on to the array and then it's going to increment I remember it does this F n so now is one it's going to go through is going to push one onto the array it's going to go through push to push three push for after four will increment I to five before the for loop goes it's going to check is I less than five no I actually five I not less than five so it's going only going to put push the number zero through four on to the array let's check it out by console that logging in 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 2 break so if we run that it's just going to put 0 1 2 because after 2 I is more than 2 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 cancel that log or print out each item in that array so we're going to initialize I to equals 0 if I is less than our dot 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 1 and we're just going to cancel that log R and then we just put the I variable as the index that will of the array that we're trying to cancel that log so the first one we get the array index 0 because I will equal 0 and X 0 B 10 then we'll do index 1 2 3 4 because each run through the array we're going to increment I 1 up 1 and that's going to go to the next index of the array if we run that C but we got 10 9 8 7 6 if you have a multi-dimensional array you can use nested for loops to loop through both the array and any sub arrays let me show you an example of that ok 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 the rain there's the end of the array and inside the array are three other arrays so inside the first rate here is index 0 here's and next one one here's index two and in each of those indexes is another array so we have index 0 and index 1 and each array so you can use nested for-loops to loop through these both arrays here's your 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 0 if I is less than or a dot length and the length of the array see we have 1 2 3 so the length of the array is 3 but now inside the nested for loop we have to initialize the variable differently so now it's at I we have J now if J is less than array index I dot length when we're looking at the array index I dot 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 2 we have 1 2 so we're going to end 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 count without lock 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 for going to X 0 that's this and then the index J will be what index in the second array in the nested array so we would get in X 0 and X 1 for J it's going to go through this inner for loop to get both and that is 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 for loop again to get both indexes in here and so on if we log if we console that log that it's going to say 10 9 8 7 6 1 2 3 4 5 6 so the first 2 9 8 7 6 was from up here and then we have the 1 2 3 4 5 6 from down here well those are four loops and nested for-loops thanks for watching my name is beau Carnes don't forget to subscribe and remember use your coach we're good
Info
Channel: freeCodeCamp.org
Views: 62,631
Rating: undefined out of 5
Keywords: for loops, for, nested, iteration, iterate
Id: 24Wpg6njlYI
Channel Id: undefined
Length: 6min 36sec (396 seconds)
Published: Mon Mar 13 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.