Normally, the loops are very easy to control.
You simply need to trace the counter eye during each run, but nested loops rely on two counters I.
And so it can be hard to follow two counters at the same time.
So it helps to visualize the runtime using breakpoints.
In this lesson, you're going to learn to debug loops using breakpoints.
All right, first thing to do is apply breakpoints to the nested loop.
And so during the runtime, we're going to visualize and keep track of the counters I enjoy as the updates
to launch a new debugging session.
And as expected, everyone from the outer loop runs the inner loop 10 times, we can see that by looking
at the counters and this is going to keep happening till the outer loop breaks so we can go ahead and
stop the debugger.
All right, now, you were given the task of printing a half pyramid using Java, the pyramid just 10
rows, the first row starts with one star and the number of stars keeps increasing by one.
But if you run the current code.
This should be our output once again, the code you open doesn't behave like we want it to.
Something is wrong and using breakpoints, we can visualize the runtime and fix it step by step.
So I'm going to add one more breakpoint right here, then launch a new debugging session.
It seems that each pass from the outer loop runs the inner loop 10 times.
Every pass that follows princi row of 10 stars on the same line.
If you look back at the output, the most obvious step is to add a new line after every arrow.
So we can add a new line after the inner loop runs to completion right over here.
Stop the debugger.
We run the code.
And this pyramid should have 10 rows, so this is progress.
But how do we make sure that each row has as many stars as the order that it appears in?
It might help to launch another debugging session and visualize the runtime.
And I hope you can see the problem each passed from the outer loop, runs the inner loop 10 times.
Instead, the first pass should run the inner loop once, the second pass should run the inner loop
twice.
And so the amount of times the inner loop runs should depend on the index of the outer loop.
So the inner loop should keep running as long as J is smaller than I.
OK, this is great progress.
But why are there only one, two, three, four, nine lines?
Wow, another bug that we have to fix.
So we can launch another debugging session.
And by visualizing the runtime, the issue becomes clear, the first pass from the outer loop doesn't
do anything, so only nine lines from the pyramid shows up.
That's because one eye is zero and zero.
This condition is false.
So we need to make sure that Jay is less than or equal to I.
And perfect and displays ten rows each row has as many stars as the order that it appears in.
The first loop runs only once.
The second inner loop runs twice.
A third inner loop runs three times.
And pretty much the number of times the inner loop runs depends on the index of the outer loop.
And honestly, it's really satisfying seeing the code, working with your own eyes.