Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,030 --> 00:00:07,110
Now that we can store data and manipulate values, we need to discuss about code execution. And the code
1
2
00:00:07,110 --> 00:00:10,260
executes from top to bottom without stopping.
2
3
00:00:10,290 --> 00:00:17,340
Of course, an algorithm cannot be created just by lines of code written without any flow control.
3
4
00:00:17,430 --> 00:00:22,830
This is why in this section we will be discussing about flow control instructions. And they're very
4
5
00:00:22,830 --> 00:00:27,700
important because otherwise nothing will be possible with programming.
5
6
00:00:27,720 --> 00:00:30,540
So let's find out some flow control instructions.
6
7
00:00:30,750 --> 00:00:38,250
Firstly, we need to discuss the IF instruction because this one is the most common. And IF is very simple,
7
8
00:00:38,280 --> 00:00:44,670
you just use the operator IF and then a statement that needs to resolve in either true or false.
8
9
00:00:44,700 --> 00:00:50,220
In this case, I'm using the operator double equals (==), which actually compares.
9
10
00:00:50,220 --> 00:00:53,790
It doesn't assign it compares the value from "a" with 1.
10
11
00:00:53,880 --> 00:00:57,720
And since this is actually true, this will return true.
11
12
00:00:57,750 --> 00:01:04,170
So in this case, if we'll go through the first section which will execute this part of the code, if
12
13
00:01:04,170 --> 00:01:11,010
"a" wasn't equal with 1, then it would have went to the else branch and it would execute that part
13
14
00:01:11,010 --> 00:01:11,580
of the code.
14
15
00:01:11,590 --> 00:01:18,330
Keep in mind that the else branch is optional so it can be completely removed if not necessary.
15
16
00:01:18,690 --> 00:01:23,000
For example, a code can be just if "a" == 1 do something.
16
17
00:01:23,010 --> 00:01:25,800
If not, then I don't care about that part of the code.
17
18
00:01:25,830 --> 00:01:32,340
Now let's look at another important function which is similar with IF but has something particular.
18
19
00:01:32,370 --> 00:01:34,830
This one is called the MATCH instruction.
19
20
00:01:34,980 --> 00:01:40,080
And in other programming languages you might be familiar with the instruction SWITCH because it's very
20
21
00:01:40,080 --> 00:01:40,560
similar.
21
22
00:01:40,770 --> 00:01:47,640
So the main difference between an IF statement and the MATCH statement is the fact that in a IF statement
22
23
00:01:47,640 --> 00:01:51,330
we can have a statement made out of multiple instructions.
23
24
00:01:51,330 --> 00:01:58,440
Well, here in MATCH we have a variable, let's say "a", and then if that variable has multiple numbers
24
25
00:01:58,440 --> 00:02:03,180
or strings that can be, then it's much easier to write this.
25
26
00:02:03,190 --> 00:02:09,030
Keep in mind that with IF we can actually implement a MATCH instruction, but with a MATCH, we
26
27
00:02:09,030 --> 00:02:14,160
cannot really implement a complete IF instruction. In some cases, for example, in this one we can,
27
28
00:02:14,160 --> 00:02:15,690
but in some others we cannot.
28
29
00:02:15,780 --> 00:02:18,870
Let's quickly look at how we can implement this MATCH with an IF.
29
30
00:02:18,870 --> 00:02:22,920
If "a" == 1, then.
30
31
00:02:23,870 --> 00:02:24,830
Print is one.
31
32
00:02:26,900 --> 00:02:34,160
And then there is another instruction called else if (ELIF) it's basically else and if combined and "a" == 2
32
33
00:02:34,160 --> 00:02:36,800
print is 2.
33
34
00:02:38,550 --> 00:02:41,160
We could have opted for this implementation.
34
35
00:02:41,730 --> 00:02:50,940
But as you can see, if we add more values here like three, four, etc., then the MATCH is much more
35
36
00:02:50,940 --> 00:02:55,200
pleasant for the eye to parse rather than using an IF statement.
36
37
00:02:55,620 --> 00:03:01,740
So you might be considering what to use based on what values you are comparing.
37
38
00:03:05,210 --> 00:03:11,300
While the IF instruction and the MATCH instruction actually branch out the code, we have other types
38
39
00:03:11,300 --> 00:03:15,410
of flow control which actually deal with looping code.
39
40
00:03:15,440 --> 00:03:21,170
And the most common one is the FOR instruction. And the FOR instruction works like this:
40
41
00:03:21,950 --> 00:03:31,940
We have a value and we can give it a range, a list and some other things to iterate. Also FOR might
41
42
00:03:31,940 --> 00:03:39,620
be called the iteration operator because what it does, it goes through a list of values and parses
42
43
00:03:39,620 --> 00:03:40,580
each one of them.
43
44
00:03:40,700 --> 00:03:46,040
So if we were to run this, it will print zero, one, two, three, four.
44
45
00:03:47,130 --> 00:03:47,730
Let's see.
45
46
00:03:48,300 --> 00:03:52,930
So for the FOR instruction, we have zero one, two, three, four, because it starts from zero.
46
47
00:03:53,310 --> 00:03:57,600
And of course, it needs to be lower than five to actually finish.
47
48
00:03:57,600 --> 00:04:03,390
One pro of the FOR instruction is the fact that we can iterate lists like the one described in the previous
48
49
00:04:03,390 --> 00:04:05,420
example and also ranges.
49
50
00:04:05,430 --> 00:04:08,640
So for example, counting and so on, so forth.
50
51
00:04:08,650 --> 00:04:15,590
One disadvantage for the FOR instruction is the fact that we cannot parse values that we cannot count.
51
52
00:04:15,600 --> 00:04:18,560
And for this we can use the WHILE instruction.
52
53
00:04:18,570 --> 00:04:26,430
The WHILE and FOR are very similar in a lot of cases because they both loop code, they both work in
53
54
00:04:26,430 --> 00:04:31,150
similar ways because they test the condition to loop another time.
54
55
00:04:31,170 --> 00:04:37,500
But one major difference is the fact that the FOR knows exactly how many steps it needs to take and
55
56
00:04:37,680 --> 00:04:38,880
the WHILE doesn't.
56
57
00:04:38,890 --> 00:04:42,330
In this example, we can clearly see what that means.
57
58
00:04:42,360 --> 00:04:48,360
For example, we have the HP of the player and then we will subtract different values.
58
59
00:04:48,360 --> 00:04:50,430
In fact, random values from that.
59
60
00:04:50,430 --> 00:04:52,950
HP until it reaches zero.
60
61
00:04:53,010 --> 00:04:59,430
This might be, for example, while health is more than zero, but we don't know exactly how much because
61
62
00:04:59,430 --> 00:05:03,540
in this range we actually subtract from 10 to 30 points.
62
63
00:05:03,540 --> 00:05:10,500
And then by just running this, we can see that sometimes this happens with less steps and some
63
64
00:05:10,500 --> 00:05:13,730
other times if more steps. Let's quickly try it.
64
65
00:05:13,740 --> 00:05:15,560
So I'm going to press F6.
65
66
00:05:15,570 --> 00:05:16,680
So in this case.
66
67
00:05:17,680 --> 00:05:19,180
We started with 100.
67
68
00:05:19,570 --> 00:05:21,640
And then the health decreased.
68
69
00:05:21,760 --> 00:05:24,350
And then we ended up with seven steps.
69
70
00:05:24,370 --> 00:05:25,900
Let's run it again.
70
71
00:05:27,400 --> 00:05:32,950
Now it seems that the random did a better job, so we ended up with six steps.
71
72
00:05:33,880 --> 00:05:36,700
This could not have been implemented with a FOR.
72
73
00:05:36,730 --> 00:05:38,890
Now let's look at functions.
7778
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.