1
00:00:04,720 --> 00:00:09,720
Nesting a for loop within another for
loop is a powerful way to process data

2
00:00:09,720 --> 00:00:13,220
In fact, that's something that's done a
lot. It sounds like something special, but

3
00:00:13,220 --> 00:00:17,119
it's just really one block of code
inside another block. So let's see an

4
00:00:17,119 --> 00:00:21,529
example of that, using for loops to
generate times tables. Now these were used

5
00:00:21,529 --> 00:00:25,910
to teach children multiplication and
normally went up to 12. So I'm going to

6
00:00:25,910 --> 00:00:38,380
call my new Python file timestables.py.

7
00:00:38,380 --> 00:00:46,420
Alright, so in terms of the code, I'm gonna start by typing
for i in range parenthesis 1 comma 13 closing

8
00:00:46,430 --> 00:00:53,330
parenthesis and a colon. Then on the next
line for j in range parenthesis 1 comma

9
00:00:53,330 --> 00:00:58,220
13 right parenthesis colon. Then we're going to
print on the line after that, print

10
00:00:58,220 --> 00:01:04,940
parenthesis double quotes left and right
curly brace with a 0 in the middle, times,

11
00:01:04,940 --> 00:01:09,380
and left and right curly brace with a 1 in
the middle is and left and right curly braces

12
00:01:09,380 --> 00:01:14,869
with 2 in the middle. Then closing double
quotes dot format. Then we're gonna type

13
00:01:14,869 --> 00:01:24,409
j comma i comma i times j and we've got two
closing right parentheses. What I'm going

14
00:01:24,409 --> 00:01:29,960
to do on the next line then, is print, and
we'll just print a series of dashes

15
00:01:29,960 --> 00:01:37,080
to separate the output. Ok.

16
00:01:37,080 --> 00:01:39,799
So what have we done here?
Well firstly, we've got an outer loop

17
00:01:39,799 --> 00:01:44,030
on line one that goes around 12 times.
Inside that on line two, we've got another

18
00:01:44,030 --> 00:01:50,100
loop that also goes around 12 times and
it'll do that each time the outer loop loops.

19
00:01:50,100 --> 00:01:54,500
When i has the value 1, j will loop
through the values from 1 to 12.

20
00:01:54,500 --> 00:01:58,800
i will then become a 2 and j goes
through 1 to 12 again, and so on.

21
00:01:58,800 --> 00:02:02,359
So I'm gonna run the program first and then we'll
step it through the debugger. So when we

22
00:02:02,359 --> 00:02:09,560
run it first, you can see we've got the
output there. We're printing the value of

23
00:02:09,560 --> 00:02:13,459
j first as, you can see, and you can see
that looping from 1 to 12. The second

24
00:02:13,460 --> 00:02:19,020
value on each line after times is the value
of i, so in the first table, i is 1.

25
00:02:19,020 --> 00:02:22,520
When we go around the outer loop again,
i is 2. If we scroll down we see the next set of

26
00:02:22,520 --> 00:02:25,920
tables there, and that's giving us the
second table, and so on.

27
00:02:25,920 --> 00:02:29,560
Now the debugger can make, can
help with making sense of this.

28
00:02:29,560 --> 00:02:33,460
So what I'm going to do is set a breakpoint on
line three, the line that does the

29
00:02:33,470 --> 00:02:39,680
printing, and then let's run the program
in the debugger. Firstly we can see the

30
00:02:39,680 --> 00:02:45,950
values of i and j in the variables pane.
Not all IDEs do this, but IntelliJ also

31
00:02:45,950 --> 00:02:49,520
shows the values in the code pane. You
can see it at the top of the screen at

32
00:02:49,520 --> 00:02:53,360
the end of lines one and two. That's
helpful because you can focus on the

33
00:02:53,360 --> 00:02:56,810
code and still see the values changing,
but it also means that I can switch to

34
00:02:56,810 --> 00:03:00,340
the console tab so you can see the
output being printed and still see the

35
00:03:00,340 --> 00:03:05,000
values of our variables. So I'm gonna go
ahead and do that. Alright, so firstly,

36
00:03:05,000 --> 00:03:10,010
let's try this. I'm going to step over
the first line and we get back to

37
00:03:10,010 --> 00:03:15,800
line two - this is the inner for loop. Step
over again, which I'll do.

38
00:03:15,800 --> 00:03:20,420
You can see j now has the value of two. When I
step over that line again, we get back to

39
00:03:20,420 --> 00:03:25,190
line two, ready for j to get its next
value. So I'm going to continue stepping

40
00:03:25,190 --> 00:03:28,480
through the code until j is 12,

41
00:03:29,820 --> 00:03:33,700
and there's j 12, as you can see there.
That's the end of the inner loop, so if I

42
00:03:33,700 --> 00:03:41,420
step over again now, you see we get to go
one more time and then it's hit line four.

43
00:03:41,420 --> 00:03:45,200
We're still inside the outer loop
but the inner loop has now completed.

44
00:03:45,200 --> 00:03:50,720
If I step over once more now, we're ready to
go round the outer loop again, and I'm

45
00:03:50,720 --> 00:03:58,010
going to step over twice more to see i
gets its next value 2 and j is

46
00:03:58,010 --> 00:04:01,760
starting counting from 1 again. Alright
so there's nothing really else to say

47
00:04:01,760 --> 00:04:04,610
about this. If you want to continue
stepping through the code until you're

48
00:04:04,610 --> 00:04:08,360
comfortable with what's happening, then
pause the video now and come back to it

49
00:04:08,360 --> 00:04:12,860
once you've stepped all the way through
it. What I'm going to do now is clear the

50
00:04:12,860 --> 00:04:16,639
break point, and use the resume program
button over here to let the program

51
00:04:16,639 --> 00:04:21,320
finish at normal speed, and you can see
the program is now finished. Alright, so

52
00:04:21,320 --> 00:04:25,800
let's finish this program now with a
mini challenge.

53
00:04:25,800 --> 00:04:29,960
Here's your mini-challenge. What happens
if we: 1. Indent the print another four

54
00:04:29,969 --> 00:04:33,960
spaces? In other words we move it over to the right.
What would happen? and 2. What

55
00:04:33,960 --> 00:04:37,949
would happen if we move the indent
completely for that line. So work

56
00:04:37,949 --> 00:04:40,949
out what you'd expect to happen before
you try it. Pause the video if you're

57
00:04:40,949 --> 00:04:46,000
doing that. When you're ready, come back
and we'll go through that challenge then.

58
00:04:46,000 --> 00:04:48,920
Alright so welcome back. How did you
get on? Did you work out what the effects

59
00:04:48,920 --> 00:04:54,040
of those changes would be before running
the code? Firstly, looking at line four,

60
00:04:54,040 --> 00:04:58,080
it's currently part of the block that's
controlled by the outer for loop. So that

61
00:04:58,080 --> 00:05:01,740
means that all twelve lines for j get
printed out before we get the row of

62
00:05:01,740 --> 00:05:06,389
dashes in the output. So if we indent
line four, it'll become part of the inner

63
00:05:06,389 --> 00:05:10,259
loop and should print out every time
around the loop. So let's go up here and do

64
00:05:10,260 --> 00:05:17,600
that now. I'm just going to tab over, like
so. If I run the program now,

65
00:05:17,600 --> 00:05:23,000
you can see we get a row of dashes after
every line showing the multiplications,

66
00:05:23,009 --> 00:05:26,340
and obviously it's made the output significantly
longer as well. Alright, so we'll close

67
00:05:26,340 --> 00:05:30,210
down the run window. The second part was
determining what would happen if we

68
00:05:30,210 --> 00:05:34,110
remove the indentation completely.
Alright, so removing the indentation

69
00:05:34,110 --> 00:05:38,400
completely will take line four out of
both loops. So that means it should only

70
00:05:38,400 --> 00:05:43,360
print once, right at the end of the
program. So let me do that.

71
00:05:43,360 --> 00:05:47,180
So I moved it way over to the left, so in
other words, removed all the indentation for

72
00:05:47,180 --> 00:05:53,560
that line. If we run the program again now,
you can see what's happened there.

73
00:05:53,560 --> 00:05:59,000
We've only got one row of dashes,
right at the end of all the output.

74
00:06:01,080 --> 00:06:04,040
So there's another couple of things we
need to know about for loops but we'll

75
00:06:04,040 --> 00:06:07,790
look at them in the next few videos.
We've seen the basics of how a for loop

76
00:06:07,790 --> 00:06:13,370
works, to iterate over some sequence. The
sequence can be a string or a range,

77
00:06:13,370 --> 00:06:17,510
but Python also has other sequences that we
can iterate over. I'll actually be

78
00:06:17,510 --> 00:06:21,169
talking about lists in the next section
but I'll introduce them in the next

79
00:06:21,169 --> 00:06:24,740
video. They'll give you more practice at
working with for loops, as well as a way

80
00:06:24,740 --> 00:06:30,970
to cover two more things we need to know
about them. So, see you in the next video.

