All right, welcome to exercise number for this coach had printed diagonal, but if you run it, it
prints a line of stars.
Whoever wrote this code didn't do a very good job.
Now, at first glance, I'm not entirely sure how we're going to use the counters, A.J., to achieve
the desired output.
So what I'm going to do is use breakpoints to visualize the runtime and hopefully then I can observe
some kind of pattern.
So I'll launch the debugger.
Step through it.
OK, so I know that at this point I need to start a new line to stay consistent with the output, but
this inner loop runs 10 times.
It should only run once during the first pass and twice in the second pass, three times in the third
pass, etc., So the amount of times the inner loop runs depends on the outer loop index, not a static
value of ten.
So we'll put J is smaller than or equal to I.
And after the inner loop runs to completion, will print a new line.
Kate, let's try this out.
And the first pass is going to be a zero.
Jay is going to start at zero as well, so they're going to equal each other.
This condition is going to be true.
The inner loop runs once.
Now, just one, the inner loop breaks, starts a new line.
And now in the second pass, the inner loop is going to run twice.
Until Jay exceeds the outer loop index.
Prince, a new line, and eventually we're going to get our pyramid.
OK, cool.
Now, we don't want a pyramid, we want a diagonal.
So how are we going to do this?
Once again, I'm going to visualize the run time and hopefully I can see some kind of pattern.
OK, so it's fine if this one prince.
In the second pass, I don't want the first loop to print anything.
But we want the second interlude Apprentice star.
In the third pass, I don't want the first loop to print anything.
I don't want the second loop to print anything.
But we want the third in the loop to print star.
Now, I think I see the pattern when the inner loop counter equals the outer loop counter, that's when
we want to print the star.
So let's put our condition.
If Jay is equal to I.
We're going to print the star.
Run the code now.
Wait, what happened?
Let's visualize the runtime.
OK, so here Jake was I.
So it should print.
In the second pass, when Jay, smaller than I, nothing happens.
But when the indices are equal, it prints star.
I see, even though we can't print a Starwind Jay less than I, we still need to provide the empty space
needed to print the pyramid.
So we're going to add an statement.
Otherwise, when J is less than I.
We're going to print three spaces.
All right, let's see how this would look.
OK, so here Jay is equal to I, so we should print.
And the second pass, when Jay smarter than I print an empty space.
But when the indices are equal, it prints a star.
Now, the inner breaks moving on to the third pass.
I to zero prints and empty space, another empty space.
Princess Star, and if you keep going, eventually you're going to achieve a very nice diagonal.
As expected, it works beautifully.
We just debugged exercise number for.