A nested loop is a loop inside of another loop.
This for a loop is going to repeat code three times, but what if I placed another loop inside of it,
one that also runs code three times?
How many times is hello going to Prince?
Before I break it down for you, I encourage you to pause, take out a pen and paper and try to trace
the runtime of the code that I'm about to re display.
I hope you figure it out.
Hopefully that went well in this lesson, you're going to learn to use nested loops.
First thing you need to do is create a new class inside your Section five folder.
This file is going to be called nested loops and make sure the class has a main method.
The nested loops as an outer loop and an inner loop.
The outer loop is the big loop and the inner loop is the smaller loop inside.
This print statement is going to run nine times because each pass from the outer loop is going to run
the inner loop to completion.
The first pass is going to run the inner loop three times.
The second pass is going to run the inner loop three times.
And the third pass is going to run the inner loop three times.
In total, the nested loop ran nine times.
So a nested loops Java, I'm going to start by making a loop that runs exactly three times the loop
starts with the counter, I equaling zero, the loop is going to keep running as long as is less than
three.
And after each run, we're going to increase the counter value by one.
And so every time this loop runs, we're going to print the value of the counter during that run, so
system that out the print line run and I'll connect the counter value during that run, I.
And great, we printed the counter during the first run, second run and third run.
Now I'm going to write a for loop inside the current one.
The loop starts with the counter J equalling zero.
The loop is going to keep running as long as J is less than three and after each run, we're going to
increase the counter J value by one.
And every time this loop runs, I'm going to print the counter value.
And before I explain what's going to happen, it might be worth it to just run the code and see the
output.
And as I expect, every pass from the outer loop runs the inner loop to completion.
The outer loop started equals zero, which is less than three, so the outer loop runs Prince, the
counter value for that run, eventually reaching the inner loop inside and now the inner loop is going
to start a J.
Equals zero, which is less than three.
It's going to run print the counter value for that run.
And the inner loop is just going to keep running until Jay isn't less than three anymore.
And once that happens, the inner breaks and in turn that erupts up all the code for the first run of
the outer loop.
Now I equals one.
And this process is just going to keep repeating until the outer loop breaks.
The big picture here is that each run from the outer loop is going to run the inner loop to completion.
Now, I'm going to let this play in case you need more time to visualize what's going on.
But if you feel comfortable what we talked about so far, then feel free to skip ahead in the video.
OK, so what are some applications of nested loops, nested loops are useful when you're working with
2D arrays, you haven't learned about arrays yet, so you're not going to see nested loops for the remainder
of this section, but they are going to come back in the next section.
I can promise you that.
All righty, let's recap in this lesson, you learned how nested for loops are run.
And a nested loop has an outer loop and an inner loop, each pass from the outer loop runs the inner
loop to completion.
And that's really it, good job you covered everything there is to know about loops in Java.
It's time to build the sections Project Pocari Areto.
It's almost poker if you feel confident.
But we talked about so far, then you can feel free to skip to the next video.
But if you want to visualize the nested loop one more time slowed down without me speaking over it,
then I'll play it for the next two minutes.
All right.
See you soon.