Nested Loops in Java

Nested Loops in Java


Nested Loops in Java

$1,000 OFF ANY Springboard Tech Bootcamps with my code ALEXLEE. See if you qualify for the JOB GUARANTEE: https://bit.ly/3HX970h

🌅 Source Code - https://github.com/alexlorenlee/JavaT…

Nested loops in java are just a for loop inside a for loop. A common way to use nested loops in java is to get elements from a 2d array. To do that, your outer for loops takes care of the rows and your inner for loop takes care of the columns.

Java loops can be VERY tricky, but don’t worry too much about it right now! Use the code I provided and after time you’ll get the hang of java loops :) If you followed along, congrats! You learned-by-doing!

I hope you enjoyed this tutorial on nested for loops java! I like to have a nice mix of tutorials and actual projects for you all :)

Do you want to learn how to code from scratch, in Java? Check out my Java Tutorial For Beginners:    • Coding For Beginners In Python  

How are you using nested for loops in java? -

Disclosure: The Springboard link provided is linked to my affiliate account \u0026 supports the channel.

~

Alex Lee


Content

0.03 -> our nested for loops the spawn of satan am i going to burn alive and explode
3.87 -> when i have to write a program with nested for loops well if you watch this
7.35 -> video you'll be just fine hey it's Alex back again helping you get
10.2 -> working code quicker in your Java programs on this channel I make Java
13.5 -> tutorials just like this one so if you're new here consider subscribing
17 -> let's kick it off as we do here on the channel
21.93 -> we'll do our nested for-loops program get finish throw a class in here like so
31.38 -> and we'll call it nested for-loops take the public static void main hit finish
38.25 -> and we're all set up so let's get into this nested for-loops explanation I did
42.42 -> do a video covering regular for loops so I recommend you watch that one on the
46.739 -> screen now before you watch this one what a regular for loop does is repeat
51.12 -> code and it also has a secret changing variable that changes each time it
55.92 -> repeats so to make a regular for loop we just type this for let me make our
62.43 -> variable then we say how many times we want it to repeat we increment it by 1
67.409 -> and then we throw our code inside of the curly braces so we'll go ahead and print
73.92 -> like it's so freaking cold outside and if we run that we get its freak'n so
85.439 -> freaking cold outside printed 5 times the secret variable is called eye and it
90.329 -> changes each time its run this is especially useful for getting elements
95.549 -> from an array so if we had a string array let's say of colors like this and
104.88 -> we throw some colors in there we say red let's do blue and green I also have a
113.49 -> video on a race if you want to watch that video I'll have it on the card now
116.64 -> if we want to get each of these values from the array red blue and green we can
121.649 -> just throw it inside of a for loop we'll set it up just like the other one except
128.52 -> we'll go until the end of the array which is
131.59 -> one two three like that we'll go up by one and what do we want to do well we
140.35 -> want to print out each element of colors so we go into colors and then get each
147.91 -> element the first time this is run it will get color zero since we start at
154.66 -> zero the next time it will get color one and then color two which corresponds to
160.03 -> red blue and green so let's save that and run it so you can see if we get red
166.45 -> blue angry now let's add some little dividers here just to organize some
172.15 -> things space them out then we'll get into the meat of it yeah the meat of
181.14 -> nested for-loops I've been programming for over seven years and I'd say 90% of
187.9 -> the time you're gonna be using nested for-loops
190.63 -> to get elements from a 2d array if you want to learn more about 2d arrays I'll
194.5 -> have it in the car now but we can just make one here and do an example of how
200.5 -> to use nested for-loops to get all the elements in a 2d array so it's pretty
204.579 -> much exactly like what we did here so let's first make a 2d array called fancy
210.04 -> colors and we'll have it have some of the same values here we'll do red blue
218.23 -> and green on the first row and then on the second row now let's make this a
224.62 -> little bigger on the second row we'll do let's see cyan magenta turquoise
238.14 -> turquoise then how do you spell turquoise if you know how to spell
244.42 -> turquoise leave it in the comments all statements enter the semicolon so we'll
248.53 -> have that semicolon to get elements from a 1d array like we did here is we just
253.69 -> got the first one by doing colors zero we've got the second one and then the
259.18 -> third one to get values of a 2d array it's the same thing except we
265.02 -> crack of the rose so we go to the first row and then get the first second and
270.449 -> third and then we go to the second row and get the first second and third so we
276.659 -> have to take care of the rows first so let's make a for loop to take care of
283.349 -> the rows will start at row zero we'll call this row zero because it's easier
288.96 -> for for loops and then we want to go up until Row two so it we go zero one and
297.599 -> then there's no index two so we will stop we increment it and this will take
304.59 -> care of our rows it'll do this row and then this room but now we need to do the
310.169 -> columns of each row so we know how to get the columns of each row that's
314.4 -> exactly this right because we get this column in this column in this column so
318.87 -> we pretty much just do this right so let's do that
323.479 -> it's I equals 0 I is less than 3 just like this boom and there's an underline
334.74 -> here because we already used the variable I here so we have to change it
340.59 -> and most of the time what we'll do is just change it to J and then if you add
345.21 -> another for loop you change it to K and so on it really doesn't matter this is
349.62 -> just a convention that is widely used and people use it all the time for
353.58 -> nested for loop and a nested for loop is just a for loop in a for loop now we'll
357.509 -> go through each column let's print out the fancy colors fancy colors the eye is
367.05 -> our row and the J is our columns so we'll do I J and this is probably
377.069 -> confusing as hell right now so I'm just going to change this variable because
381.719 -> it's not very clear we'll say row so the first for loop is
386.699 -> looping through the rows row 0 row 1 and the second for loop is taking care of
394.02 -> the columns so we do column instead like that's so
402.24 -> we have column column column and we'll just place it row column an easy way to
411.479 -> remember the order is that an array starts with a row so that might help
416.789 -> someone out there enough for me talking let's save it run and see what happens
423.199 -> we get red blue green cyan magenta turquoise and that's exactly the order
430.979 -> we had in our 2d array red blue green cyan magenta turquoise don't worry too
436.02 -> much if you didn't get every single detail about this we're just gonna doing
440.31 -> another exam because that's how we do it here we know that nested for let's make
444.3 -> this a little better we know that nested for loops are just a
447.81 -> for loop in a for loop which sounds kind of confusing
450.72 -> but it's not too bad we'll start at zero for the first one and maybe go to five
459.169 -> just like that and what do we want to repeat five times well we want to repeat
464.25 -> another fully five times like this and we have to make a new variable since
469.05 -> eyes already taken and we'll say we want to repeat this code ten times and
474.77 -> increment that by one to see what's going on we can print the values I and J
481.889 -> so let's do that we can do that just like this by using some simple sort of
490.74 -> complicated looking string combinations so what I'm doing now is printing the
498.659 -> value or the string I and then tacking on the actual value of I and then I'll
505.71 -> tack on the value of J separated by a comma so just sort of format it a little
510.21 -> nicer like this and then tack on the real value of J so let's save it
516.68 -> minimize this and run it and see what those values are and this really gave me
524.37 -> a great idea of what is actually going on when I first started
528.91 -> to code so if we just look at this one more time so this gets done ten times
534.04 -> with its own variable J and then this whole thing gets run five times with its
537.58 -> variables I and J so it's printing out 50 times play around with it change the
542.44 -> numbers and I think you'll start to get the hang of it over time
545.56 -> it looks confusing I tried my best to explain it how I understood it and
550.3 -> hopefully it helps you up all the code will be in the description so don't try
553.6 -> and memorize any of it you can copy and paste it from there question of the day
558.91 -> what are you using nested for loops in your java program is it for an
562.21 -> assignment or a class project what are you working on let me know in the
566.14 -> comments I'd love to see what you're doing make sure to subscribe so you see
570.7 -> all my newest videos the day they come out if this is helpful smash that like
574 -> button and share it if you think it might helps one you know you could be
576.67 -> anywhere in the world but you're here with me but I appreciate it catch it

Source: https://www.youtube.com/watch?v=sk9xYje9XAQ