1
00:00:06,045 --> 00:00:08,574
Okay, welcome to this new video.

2
00:00:08,574 --> 00:00:12,414
I'm excited to share with you
the notion of nested loops.

3
00:00:12,414 --> 00:00:15,666
And nested loops are a bit
of a tricky concept, but

4
00:00:15,666 --> 00:00:19,941
let's just break it down together and
see what it means, right?

5
00:00:19,941 --> 00:00:23,364
So the idea of a nested loop is that
you could actually use a loop within

6
00:00:23,364 --> 00:00:24,246
a loop, right?

7
00:00:24,246 --> 00:00:28,484
Meaning that you have a for loop or
any particular kind of loop, and

8
00:00:28,484 --> 00:00:31,780
within that you can create another one,
right?

9
00:00:31,780 --> 00:00:34,244
You might be wondering,
why is that necessary?

10
00:00:34,244 --> 00:00:37,783
Well, there are certain constructs,
like a grid, for instance,

11
00:00:37,783 --> 00:00:41,338
that makes sense to be thinking of
it as a loop within a loop, right?

12
00:00:41,338 --> 00:00:45,884
For any number of rows, you have
a certain number of columns, right?

13
00:00:45,884 --> 00:00:49,020
And so we'll see examples
of how that works, right?

14
00:00:49,020 --> 00:00:52,254
But for that first, let's just
create a bit of a definition and

15
00:00:52,254 --> 00:00:56,419
understanding that we have something that
we would call the outer loop, right?

16
00:00:56,419 --> 00:01:01,667
Which is the first loop that
we basically describe, and

17
00:01:01,667 --> 00:01:06,701
then within that we would
have an inner loop, right?

18
00:01:06,701 --> 00:01:11,577
Let's see an example of how
those two loops would actually

19
00:01:11,577 --> 00:01:14,171
work with each other, right?

20
00:01:14,171 --> 00:01:16,875
In this particular example,
we have a variable, i,

21
00:01:16,875 --> 00:01:19,948
that represents the outer loop
that goes between 1 and 3.

22
00:01:19,948 --> 00:01:23,517
So we have the number 1 and 2, right?

23
00:01:23,517 --> 00:01:28,105
As you can see on the top, and the j
variable, which is a convention that we

24
00:01:28,105 --> 00:01:31,809
tend to use whenever you have
a nested loop, you use I and j.

25
00:01:31,809 --> 00:01:35,339
This is, again, up to you but
you could use x and y.

26
00:01:35,339 --> 00:01:36,694
But you'll see i and

27
00:01:36,694 --> 00:01:42,626
j kind of quite regularly within code that
other people might use conventions, right?

28
00:01:42,626 --> 00:01:45,482
So the j variable goes from 4 to 7.

29
00:01:45,482 --> 00:01:47,574
So we have 4, 5 and 6.

30
00:01:49,034 --> 00:01:53,794
So let's see, what would be
the execution of the print function

31
00:01:53,794 --> 00:01:57,685
that prints at every stage of the loop i,
j, right?

32
00:01:57,685 --> 00:02:02,245
And the diagram that you can see
on the right represents that i

33
00:02:02,245 --> 00:02:05,206
initially will be stuck at 1, right?

34
00:02:05,206 --> 00:02:06,307
So I will be 1.

35
00:02:06,307 --> 00:02:11,794
So we would initially print 1 and 4, 1,4.

36
00:02:11,794 --> 00:02:14,901
We would stay with one and
iterate over the j, right?

37
00:02:14,901 --> 00:02:17,396
I mean, the inner loop will be iterating.

38
00:02:17,396 --> 00:02:22,000
So we're going to have 1,5 and
continue to iterate over j.

39
00:02:22,000 --> 00:02:23,684
And we're going to have 1, 6.

40
00:02:25,384 --> 00:02:30,302
Once we finish with the inner loop, we're
going to go back to the outer loop, right?

41
00:02:30,302 --> 00:02:32,824
And we're going to move
the outer loop to 2.

42
00:02:32,824 --> 00:02:37,568
So in this case we have 2, and then we
go back to the inner loop once more.

43
00:02:37,568 --> 00:02:42,674
So we're going to have 2,4,
2,5, and 2,6, right?

44
00:02:42,674 --> 00:02:47,931
So it's important to understand that
the final sequence that you're going to

45
00:02:47,931 --> 00:02:53,194
end up is 1,4, 1,5, 1,6,
2,4, 2,5, 2,6, right?

46
00:02:53,194 --> 00:02:57,146
That's the order in which those
loops are going to execute, right?

47
00:02:57,146 --> 00:03:03,514
You basically freeze the first loop
while you execute the inner loop first.

48
00:03:03,514 --> 00:03:06,874
Then you move to the second
iteration of the outer loop and

49
00:03:06,874 --> 00:03:09,088
you execute the inner loop first.

50
00:03:09,088 --> 00:03:13,261
So it might be a concept that is
more easy to kind of grasp as we

51
00:03:13,261 --> 00:03:17,824
use it within the code and
we see some kind of graphic examples.

52
00:03:17,824 --> 00:03:19,667
So we're going to do
that in the next video.