1
00:00:00,420 --> 00:00:02,480
Normally, the loops are very easy to control.

2
00:00:02,550 --> 00:00:08,460
You simply need to trace the counter eye during each run, but nested loops rely on two counters I.

3
00:00:09,420 --> 00:00:12,180
And so it can be hard to follow two counters at the same time.

4
00:00:12,190 --> 00:00:15,300
So it helps to visualize the runtime using breakpoints.

5
00:00:17,860 --> 00:00:20,710
In this lesson, you're going to learn to debug loops using breakpoints.

6
00:00:23,130 --> 00:00:26,670
All right, first thing to do is apply breakpoints to the nested loop.

7
00:00:29,540 --> 00:00:34,130
And so during the runtime, we're going to visualize and keep track of the counters I enjoy as the updates

8
00:00:34,610 --> 00:00:36,230
to launch a new debugging session.

9
00:00:40,400 --> 00:00:46,520
And as expected, everyone from the outer loop runs the inner loop 10 times, we can see that by looking

10
00:00:46,520 --> 00:00:52,070
at the counters and this is going to keep happening till the outer loop breaks so we can go ahead and

11
00:00:52,070 --> 00:00:53,180
stop the debugger.

12
00:00:54,480 --> 00:00:59,490
All right, now, you were given the task of printing a half pyramid using Java, the pyramid just 10

13
00:00:59,490 --> 00:01:04,650
rows, the first row starts with one star and the number of stars keeps increasing by one.

14
00:01:08,830 --> 00:01:10,270
But if you run the current code.

15
00:01:19,320 --> 00:01:24,840
This should be our output once again, the code you open doesn't behave like we want it to.

16
00:01:24,930 --> 00:01:29,310
Something is wrong and using breakpoints, we can visualize the runtime and fix it step by step.

17
00:01:30,000 --> 00:01:33,960
So I'm going to add one more breakpoint right here, then launch a new debugging session.

18
00:01:40,670 --> 00:01:44,270
It seems that each pass from the outer loop runs the inner loop 10 times.

19
00:01:47,950 --> 00:01:51,550
Every pass that follows princi row of 10 stars on the same line.

20
00:01:52,430 --> 00:01:57,280
If you look back at the output, the most obvious step is to add a new line after every arrow.

21
00:02:01,450 --> 00:02:05,500
So we can add a new line after the inner loop runs to completion right over here.

22
00:02:11,390 --> 00:02:12,560
Stop the debugger.

23
00:02:14,110 --> 00:02:14,980
We run the code.

24
00:02:22,800 --> 00:02:26,160
And this pyramid should have 10 rows, so this is progress.

25
00:02:27,810 --> 00:02:32,010
But how do we make sure that each row has as many stars as the order that it appears in?

26
00:02:34,230 --> 00:02:37,620
It might help to launch another debugging session and visualize the runtime.

27
00:02:46,280 --> 00:02:51,470
And I hope you can see the problem each passed from the outer loop, runs the inner loop 10 times.

28
00:02:52,460 --> 00:02:57,770
Instead, the first pass should run the inner loop once, the second pass should run the inner loop

29
00:02:57,770 --> 00:02:58,370
twice.

30
00:02:58,670 --> 00:03:04,360
And so the amount of times the inner loop runs should depend on the index of the outer loop.

31
00:03:04,970 --> 00:03:08,960
So the inner loop should keep running as long as J is smaller than I.

32
00:03:23,390 --> 00:03:24,920
OK, this is great progress.

33
00:03:26,510 --> 00:03:31,020
But why are there only one, two, three, four, nine lines?

34
00:03:31,490 --> 00:03:33,680
Wow, another bug that we have to fix.

35
00:03:34,870 --> 00:03:37,060
So we can launch another debugging session.

36
00:03:39,300 --> 00:03:45,240
And by visualizing the runtime, the issue becomes clear, the first pass from the outer loop doesn't

37
00:03:45,240 --> 00:03:48,960
do anything, so only nine lines from the pyramid shows up.

38
00:03:49,500 --> 00:03:52,650
That's because one eye is zero and zero.

39
00:03:52,960 --> 00:03:54,320
This condition is false.

40
00:03:54,360 --> 00:03:57,990
So we need to make sure that Jay is less than or equal to I.

41
00:04:17,130 --> 00:04:22,860
And perfect and displays ten rows each row has as many stars as the order that it appears in.

42
00:04:28,580 --> 00:04:30,500
The first loop runs only once.

43
00:04:35,300 --> 00:04:37,400
The second inner loop runs twice.

44
00:04:39,240 --> 00:04:41,160
A third inner loop runs three times.

45
00:04:44,990 --> 00:04:50,480
And pretty much the number of times the inner loop runs depends on the index of the outer loop.

46
00:04:58,530 --> 00:05:03,120
And honestly, it's really satisfying seeing the code, working with your own eyes.

