Nested Loops in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
our nested for loops the spawn of satan am i going to burn alive and explode when i have to write a program with nested for loops well if you watch this video you'll be just fine hey it's Alex back again helping you get working code quicker in your Java programs on this channel I make Java tutorials just like this one so if you're new here consider subscribing let's kick it off as we do here on the channel we'll do our nested for-loops program get finish throw a class in here like so and we'll call it nested for-loops take the public static void main hit finish and we're all set up so let's get into this nested for-loops explanation I did do a video covering regular for loops so I recommend you watch that one on the screen now before you watch this one what a regular for loop does is repeat code and it also has a secret changing variable that changes each time it repeats so to make a regular for loop we just type this for let me make our variable then we say how many times we want it to repeat we increment it by 1 and then we throw our code inside of the curly braces so we'll go ahead and print like it's so freaking cold outside and if we run that we get its freak'n so freaking cold outside printed 5 times the secret variable is called eye and it changes each time its run this is especially useful for getting elements from an array so if we had a string array let's say of colors like this and we throw some colors in there we say red let's do blue and green I also have a video on a race if you want to watch that video I'll have it on the card now if we want to get each of these values from the array red blue and green we can just throw it inside of a for loop we'll set it up just like the other one except we'll go until the end of the array which is one two three like that we'll go up by one and what do we want to do well we want to print out each element of colors so we go into colors and then get each element the first time this is run it will get color zero since we start at zero the next time it will get color one and then color two which corresponds to red blue and green so let's save that and run it so you can see if we get red blue angry now let's add some little dividers here just to organize some things space them out then we'll get into the meat of it yeah the meat of nested for-loops I've been programming for over seven years and I'd say 90% of the time you're gonna be using nested for-loops to get elements from a 2d array if you want to learn more about 2d arrays I'll have it in the car now but we can just make one here and do an example of how to use nested for-loops to get all the elements in a 2d array so it's pretty much exactly like what we did here so let's first make a 2d array called fancy colors and we'll have it have some of the same values here we'll do red blue and green on the first row and then on the second row now let's make this a little bigger on the second row we'll do let's see cyan magenta turquoise turquoise then how do you spell turquoise if you know how to spell turquoise leave it in the comments all statements enter the semicolon so we'll have that semicolon to get elements from a 1d array like we did here is we just got the first one by doing colors zero we've got the second one and then the third one to get values of a 2d array it's the same thing except we crack of the rose so we go to the first row and then get the first second and third and then we go to the second row and get the first second and third so we have to take care of the rows first so let's make a for loop to take care of the rows will start at row zero we'll call this row zero because it's easier for for loops and then we want to go up until Row two so it we go zero one and then there's no index two so we will stop we increment it and this will take care of our rows it'll do this row and then this room but now we need to do the columns of each row so we know how to get the columns of each row that's exactly this right because we get this column in this column in this column so we pretty much just do this right so let's do that it's I equals 0 I is less than 3 just like this boom and there's an underline here because we already used the variable I here so we have to change it and most of the time what we'll do is just change it to J and then if you add another for loop you change it to K and so on it really doesn't matter this is just a convention that is widely used and people use it all the time for nested for loop and a nested for loop is just a for loop in a for loop now we'll go through each column let's print out the fancy colors fancy colors the eye is our row and the J is our columns so we'll do I J and this is probably confusing as hell right now so I'm just going to change this variable because it's not very clear we'll say row so the first for loop is looping through the rows row 0 row 1 and the second for loop is taking care of the columns so we do column instead like that's so we have column column column and we'll just place it row column an easy way to remember the order is that an array starts with a row so that might help someone out there enough for me talking let's save it run and see what happens we get red blue green cyan magenta turquoise and that's exactly the order we had in our 2d array red blue green cyan magenta turquoise don't worry too much if you didn't get every single detail about this we're just gonna doing another exam because that's how we do it here we know that nested for let's make this a little better we know that nested for loops are just a for loop in a for loop which sounds kind of confusing but it's not too bad we'll start at zero for the first one and maybe go to five just like that and what do we want to repeat five times well we want to repeat another fully five times like this and we have to make a new variable since eyes already taken and we'll say we want to repeat this code ten times and increment that by one to see what's going on we can print the values I and J so let's do that we can do that just like this by using some simple sort of complicated looking string combinations so what I'm doing now is printing the value or the string I and then tacking on the actual value of I and then I'll tack on the value of J separated by a comma so just sort of format it a little nicer like this and then tack on the real value of J so let's save it minimize this and run it and see what those values are and this really gave me a great idea of what is actually going on when I first started to code so if we just look at this one more time so this gets done ten times with its own variable J and then this whole thing gets run five times with its variables I and J so it's printing out 50 times play around with it change the numbers and I think you'll start to get the hang of it over time it looks confusing I tried my best to explain it how I understood it and hopefully it helps you up all the code will be in the description so don't try and memorize any of it you can copy and paste it from there question of the day what are you using nested for loops in your java program is it for an assignment or a class project what are you working on let me know in the comments I'd love to see what you're doing make sure to subscribe so you see all my newest videos the day they come out if this is helpful smash that like button and share it if you think it might helps one you know you could be anywhere in the world but you're here with me but I appreciate it catch it
Info
Channel: Alex Lee
Views: 154,830
Rating: undefined out of 5
Keywords: nested loops in java, nested loop, nested loop java, nested for loops java, nested loops java, java loops, java nested loops, how to use nested for loops in java, nested for loop, for loops java, java, nested for loops in java, loops in java, nested for loops, how to use nested for loop in java, how to use nested loop in java, how to use a nested for loop in java, nested loop in java, nested for loop in java, java (programming language), for loop, nested loops, learn java
Id: sk9xYje9XAQ
Channel Id: undefined
Length: 10min 2sec (602 seconds)
Published: Thu Nov 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.