1
00:00:06,029 --> 00:00:07,594
Hi, welcome to this new video.

2
00:00:08,734 --> 00:00:11,358
We are going to start
talking about iteration.

3
00:00:11,358 --> 00:00:15,680
And this is one of the kind of, the
cornerstones of, one of the things that

4
00:00:15,680 --> 00:00:20,007
we can do with programming is basically
repeating things over and over and

5
00:00:20,007 --> 00:00:23,043
not necessarily repeating
them exactly the same.

6
00:00:23,043 --> 00:00:25,745
But rather kind of how it can repeat and

7
00:00:25,745 --> 00:00:29,714
learn from every iteration
that we perform, right?

8
00:00:29,714 --> 00:00:33,458
So let's start understanding
the notion of iteration.

9
00:00:33,458 --> 00:00:37,674
We're going to be understanding the notion
of iteration as a sequence, right?

10
00:00:37,674 --> 00:00:42,418
So think about a sequence of
instructions for the code to be repeated

11
00:00:42,418 --> 00:00:47,754
a specific number of times to
achieve a particular result, right?

12
00:00:47,754 --> 00:00:49,244
As you saw in our last video,

13
00:00:49,244 --> 00:00:53,828
we were starting to understand primitives
that have a sequence of vertices, right?

14
00:00:53,828 --> 00:00:55,661
So you might be thinking, well,

15
00:00:55,661 --> 00:01:00,036
it's kind of quite manual to type those
vertices over and over and over again.

16
00:01:00,036 --> 00:01:02,500
What if I want to have 1000 vertices,
right?

17
00:01:02,500 --> 00:01:06,370
So it's clear that many
operations within computing

18
00:01:06,370 --> 00:01:10,678
between programming need to
be performed automatically.

19
00:01:10,678 --> 00:01:15,656
Or need to be displayed in such a way
that the computer would understand how

20
00:01:15,656 --> 00:01:18,865
to iterate or
go through a large amount of data or

21
00:01:18,865 --> 00:01:22,418
repeat something perhaps many many times,
right.

22
00:01:22,418 --> 00:01:25,842
So that's something that is actually
quite easy to do with specific

23
00:01:25,842 --> 00:01:27,768
constructs that we have for that.

24
00:01:27,768 --> 00:01:30,952
So we're going to be introducing
something called the for loop.

25
00:01:30,952 --> 00:01:37,000
A for loop is our first out of a few
different loops that we will evaluate,

26
00:01:37,000 --> 00:01:41,808
but the for loop really gives
us a simple way to iterate.

27
00:01:41,808 --> 00:01:43,592
So let's look at the syntax together.

28
00:01:43,592 --> 00:01:44,862
So it says four and

29
00:01:44,862 --> 00:01:49,748
we have a variable which is in this
case num representing a number.

30
00:01:49,748 --> 00:01:51,596
And then we use in range,

31
00:01:51,596 --> 00:01:55,391
range is going to give us
a series of numbers, right.

32
00:01:55,391 --> 00:02:00,068
We saying four,
because we want a range between 0 and 3.

33
00:02:00,068 --> 00:02:04,964
One of the things that we actually
will see within this kind of

34
00:02:04,964 --> 00:02:09,932
range function that we're using
is that we will start at 0.

35
00:02:09,932 --> 00:02:13,866
0 it's by definition especially or
by convention in a way,

36
00:02:13,866 --> 00:02:16,961
the way we're starting
an iteration process.

37
00:02:16,961 --> 00:02:22,728
And you can see as we run this code and
we're using here an indentation in python.

38
00:02:22,728 --> 00:02:27,451
The indentation meaning the space
that is granted below the for

39
00:02:27,451 --> 00:02:31,281
loop, will suggest that all
the lines after the for

40
00:02:31,281 --> 00:02:35,536
loop that are indented belong to that for
loop, right?

41
00:02:35,536 --> 00:02:36,658
It's a series,

42
00:02:36,658 --> 00:02:42,334
it's a part of the code that specifically
will be repeated many times.

43
00:02:42,334 --> 00:02:45,454
And why do we need this variable of num?

44
00:02:45,454 --> 00:02:50,278
Because we want to keep track
in which iteration are we in so

45
00:02:50,278 --> 00:02:56,488
many times we can think of it as
a variable I standing for iterator, right.

46
00:02:56,488 --> 00:02:59,616
Because the first time we
loop through this variable,

47
00:02:59,616 --> 00:03:01,588
the variable will start at 0, and

48
00:03:01,588 --> 00:03:05,998
then we will move to the second loop
version of the loop, and it will be one.

49
00:03:05,998 --> 00:03:10,659
The third time will be two, and
the final time would be three, rihgt.

50
00:03:10,659 --> 00:03:15,382
And at that point we will conclude
the loop, because the variable is no

51
00:03:15,382 --> 00:03:20,658
longer within the range that we've
specified and we can continue our code.

52
00:03:20,658 --> 00:03:24,530
So whenever you see this kind
of structure, the for loop,

53
00:03:24,530 --> 00:03:29,349
think of it as a section of the code
in which the program will repeat itself

54
00:03:29,349 --> 00:03:33,474
until it concludes the task
established by the loop, right?

55
00:03:34,734 --> 00:03:38,913
So let's look at an example.

56
00:03:38,913 --> 00:03:43,641
We can look at something like a tuple,
like we haven't really kind of

57
00:03:43,641 --> 00:03:48,278
described data structures yet,
we're going to look that later.

58
00:03:48,278 --> 00:03:53,776
But if you have something like a series
of words together, like hello world,

59
00:03:53,776 --> 00:03:59,954
you can do a loop that evaluates each one
of the entities within that structure.

60
00:03:59,954 --> 00:04:06,285
So hello world, there are two
strings separated by a comma.

61
00:04:06,285 --> 00:04:11,484
So if we would actually print
each one of the entities

62
00:04:11,484 --> 00:04:16,564
within the hello world,
or the words within that,

63
00:04:16,564 --> 00:04:22,374
you would get first the line hello and
then the line world.

64
00:04:23,594 --> 00:04:27,164
Similarly, we could actually
loop through a string, right?

65
00:04:27,164 --> 00:04:32,209
So strings are a collection of letters,
and we could also think,

66
00:04:32,209 --> 00:04:37,372
well, what if we want to iterate
over each one of those strings?

67
00:04:37,372 --> 00:04:40,904
So in this case,
this example, the variable I,

68
00:04:40,904 --> 00:04:45,998
which again is an arbitrary name
that we're giving to this variable.

69
00:04:45,998 --> 00:04:49,551
Will select independently the different

70
00:04:49,551 --> 00:04:54,476
characters that exist within
the string hello, right?

71
00:04:54,476 --> 00:04:57,356
So as you can see,
the result of such print function,

72
00:04:57,356 --> 00:05:00,540
you would get the h, the e,
the l, the l, the o, right?

73
00:05:00,540 --> 00:05:06,692
So for loops are used in a variety
of contexts to repeat information,

74
00:05:06,692 --> 00:05:11,582
to go through information and
repeat some operation.

75
00:05:11,582 --> 00:05:15,770
We can do a large part of our code
within a loop structure, right,

76
00:05:15,770 --> 00:05:18,564
where we do kind of complex calculations.

77
00:05:18,564 --> 00:05:22,899
And we will start seeing that loops,
a very quick way for a computer to really

78
00:05:22,899 --> 00:05:28,324
start running, maybe slower and start kind
of using a lot of computation, right?

79
00:05:28,324 --> 00:05:30,604
But that's the nature of what
we can do with computing.

80
00:05:30,604 --> 00:05:34,652
How many things can we really
evaluate at a given frame?

81
00:05:34,652 --> 00:05:39,604
And are we doing those sequentially or
are we doing those distributedly?

82
00:05:39,604 --> 00:05:43,884
That's different paradigms of
programming than we might take.

83
00:05:43,884 --> 00:05:45,136
In this case, the for

84
00:05:45,136 --> 00:05:49,878
loop suggests that we will actually do
a repetition in a single frame, right?

85
00:05:49,878 --> 00:05:53,345
So within a single frame
of our computation,

86
00:05:53,345 --> 00:05:56,806
we will perform a series of calculations.

87
00:05:56,806 --> 00:05:59,839
So this is the foundation of our for loop.

88
00:05:59,839 --> 00:06:04,048
In the next videos, we'll start seeing
how to use them within processing.