Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:01,280 --> 00:00:02,910
Welcome back.
1
2
00:00:02,910 --> 00:00:05,510
Let's now make coding even more fun
2
3
00:00:05,510 --> 00:00:08,330
and actually take decisions with our code
3
4
00:00:08,330 --> 00:00:10,883
to make it look a lot more real.
4
5
00:00:12,320 --> 00:00:15,220
So let's say it that we want to write a program
5
6
00:00:15,220 --> 00:00:16,990
which checks whether a person
6
7
00:00:16,990 --> 00:00:21,030
is allowed to start taking a driver's license or not.
7
8
00:00:21,030 --> 00:00:24,520
And if she is, then it will print that to the console.
8
9
00:00:24,520 --> 00:00:28,130
And if not, it will print how many years are left
9
10
00:00:28,130 --> 00:00:31,593
until the person can start taking the driver's license.
10
11
00:00:32,760 --> 00:00:37,760
So let's start with an age and let's set it to 19.
11
12
00:00:38,480 --> 00:00:40,930
So we have this age and we already know
12
13
00:00:40,930 --> 00:00:44,640
how to check if this age is at least 18,
13
14
00:00:44,640 --> 00:00:48,810
which is the legal required age to start a driving license
14
15
00:00:48,810 --> 00:00:50,780
at least here in Europe.
15
16
00:00:50,780 --> 00:00:53,423
So let's actually create a variable with that.
16
17
00:00:54,710 --> 00:00:57,300
And I think we actually already did that.
17
18
00:00:57,300 --> 00:00:59,417
Let's call this one 'is old enough'.
18
19
00:01:00,540 --> 00:01:03,250
So again, with a variable name
19
20
00:01:03,250 --> 00:01:05,473
that describes exactly what we want here.
20
21
00:01:06,900 --> 00:01:11,010
So remember to check if the age is at least 18,
21
22
00:01:11,010 --> 00:01:16,010
we need it to be greater or equal than 18.
22
23
00:01:16,620 --> 00:01:19,140
And so this will then include 18.
23
24
00:01:19,140 --> 00:01:22,950
So if the age is 18, the result of this operator here
24
25
00:01:22,950 --> 00:01:25,660
or of this operation will then be true.
25
26
00:01:25,660 --> 00:01:29,810
Only if the age is 17 or 16 or anything below,
26
27
00:01:29,810 --> 00:01:31,573
it will turn out to be false.
27
28
00:01:32,410 --> 00:01:33,370
Okay.
28
29
00:01:33,370 --> 00:01:35,550
So we have a Boolean value now,
29
30
00:01:35,550 --> 00:01:36,870
and we can use this
30
31
00:01:36,870 --> 00:01:40,680
to take decisions using an 'if statement'
31
32
00:01:40,680 --> 00:01:43,300
and the 'if statement' works like this.
32
33
00:01:43,300 --> 00:01:47,880
All we have to do is write 'if' then open parenthesis
33
34
00:01:47,880 --> 00:01:51,650
and then in here goes a condition that is evaluated.
34
35
00:01:51,650 --> 00:01:54,250
And if this condition turns out to be true,
35
36
00:01:54,250 --> 00:01:57,530
then this block will be executed.
36
37
00:01:57,530 --> 00:01:59,720
So basically whatever code that we write
37
38
00:01:59,720 --> 00:02:02,380
into this block, which is denoted by
38
39
00:02:02,380 --> 00:02:04,690
these curly braces will be executed
39
40
00:02:04,690 --> 00:02:08,320
whenever the condition that's here is true.
40
41
00:02:08,320 --> 00:02:09,581
So in this case, let's put the condition,
41
42
00:02:09,581 --> 00:02:13,360
and with condition I basically mean something
42
43
00:02:13,360 --> 00:02:15,400
that is a Boolean value.
43
44
00:02:15,400 --> 00:02:18,840
So let's use, 'or is old enough' variable here
44
45
00:02:18,840 --> 00:02:22,200
because we already know that this will always be a Boolean.
45
46
00:02:22,200 --> 00:02:25,813
So again, whenever this value here is true
46
47
00:02:25,813 --> 00:02:29,750
then the code that's in this block here will be executed.
47
48
00:02:29,750 --> 00:02:34,080
And if it's false, well, then it's not going to be executed.
48
49
00:02:34,080 --> 00:02:37,763
So let's log to the console.
49
50
00:02:40,280 --> 00:02:44,023
Sarah can start driving license
50
51
00:02:46,880 --> 00:02:49,300
and let's just add a fun emoji here
51
52
00:02:50,660 --> 00:02:54,700
and on the Mac, that's Command + Control + Space
52
53
00:02:54,700 --> 00:02:56,600
and on Windows 10, the shortcut is Windows + .
53
54
00:02:56,600 --> 00:02:57,860
and on Windows 10, the shortcut is Windows + .
54
55
00:02:57,860 --> 00:03:00,763
but, adding the emoji here is not really important.
55
56
00:03:01,800 --> 00:03:05,373
So let's give it a safe and try this out now.
56
57
00:03:06,630 --> 00:03:11,350
And indeed we get, Sarah can start her driving license
57
58
00:03:11,350 --> 00:03:14,380
and that's because the age is 19.
58
59
00:03:14,380 --> 00:03:16,620
And so it's above 18.
59
60
00:03:16,620 --> 00:03:18,490
So this is true.
60
61
00:03:18,490 --> 00:03:21,610
And so if 'is old enough' is true
61
62
00:03:21,610 --> 00:03:25,030
then this block of code here will be executed
62
63
00:03:25,030 --> 00:03:27,360
which in this case is just one line
63
64
00:03:27,360 --> 00:03:30,150
but it could of course be multiple lines.
64
65
00:03:30,150 --> 00:03:33,010
So now let's put it to something else.
65
66
00:03:33,010 --> 00:03:34,360
So 15.
66
67
00:03:34,360 --> 00:03:36,520
And so now this is false.
67
68
00:03:36,520 --> 00:03:41,520
And so then this code block here should not be executed.
68
69
00:03:41,690 --> 00:03:44,030
So give it a safe, let's try.
69
70
00:03:44,030 --> 00:03:46,573
And indeed we get no output this time.
70
71
00:03:47,580 --> 00:03:50,373
And so that means that it worked.
71
72
00:03:51,420 --> 00:03:53,900
So let's put it back to 19.
72
73
00:03:53,900 --> 00:03:54,800
And in practice,
73
74
00:03:54,800 --> 00:03:59,280
we always write this condition directly here.
74
75
00:03:59,280 --> 00:04:01,180
So let's do that.
75
76
00:04:01,180 --> 00:04:04,530
So what we want is just this part.
76
77
00:04:04,530 --> 00:04:06,690
So let's get rid of this.
77
78
00:04:06,690 --> 00:04:09,790
And so this is a lot more common.
78
79
00:04:09,790 --> 00:04:11,570
Let's actually getting rid of this.
79
80
00:04:11,570 --> 00:04:14,980
And so we basically do this operation here.
80
81
00:04:14,980 --> 00:04:17,170
This will then return true or false.
81
82
00:04:17,170 --> 00:04:18,410
And depending on that,
82
83
00:04:18,410 --> 00:04:21,430
this code block will then be executed.
83
84
00:04:21,430 --> 00:04:24,010
And actually let's put it back to 15
84
85
00:04:24,010 --> 00:04:26,130
because now we can do something else
85
86
00:04:26,130 --> 00:04:29,840
which is to actually add an 'else' block.
86
87
00:04:29,840 --> 00:04:30,720
And we do that simply
87
88
00:04:30,720 --> 00:04:35,383
by writing else right after this first, 'if' block,
88
89
00:04:35,383 --> 00:04:37,160
and then we add another block.
89
90
00:04:37,160 --> 00:04:40,230
So what do you think this 'else' block is for?
90
91
00:04:40,230 --> 00:04:43,800
Well, this 'else' block will basically be executed
91
92
00:04:43,800 --> 00:04:47,090
whenever this condition here is false.
92
93
00:04:47,090 --> 00:04:49,500
So right now the age is below 18.
93
94
00:04:49,500 --> 00:04:50,830
And so this is false.
94
95
00:04:50,830 --> 00:04:52,240
And so as we already know,
95
96
00:04:52,240 --> 00:04:54,940
this block here will not be executed.
96
97
00:04:54,940 --> 00:04:57,800
Instead, the 'else' block will be executed.
97
98
00:04:57,800 --> 00:05:00,190
So whatever code we have here.
98
99
00:05:00,190 --> 00:05:02,510
So let's write some code in here.
99
100
00:05:02,510 --> 00:05:03,930
And as I sad in the beginning,
100
101
00:05:03,930 --> 00:05:06,290
we will calculate how many years are left
101
102
00:05:06,290 --> 00:05:09,820
for Sarah to start taking her license.
102
103
00:05:09,820 --> 00:05:13,560
So let's calculate that, 'years left'.
103
104
00:05:15,610 --> 00:05:20,133
And so that's simply, 18 minus the age.
104
105
00:05:21,360 --> 00:05:22,193
Right?
105
106
00:05:23,950 --> 00:05:26,520
And then we can log in a nice string into the console,
106
107
00:05:26,520 --> 00:05:29,070
which contains this variable.
107
108
00:05:29,070 --> 00:05:31,030
And as we learned in the last lecture
108
109
00:05:31,030 --> 00:05:34,720
the best way of doing that is to use a template literal.
109
110
00:05:34,720 --> 00:05:36,290
So back ticks.
110
111
00:05:36,290 --> 00:05:41,047
And then we say, Sarah is too young'.
111
112
00:05:42,160 --> 00:05:44,193
'Wait another'
112
113
00:05:45,630 --> 00:05:50,050
and then we insert or placeholder here basically.
113
114
00:05:50,050 --> 00:05:51,480
So years left
114
115
00:05:53,140 --> 00:05:55,700
and then years.
115
116
00:05:55,700 --> 00:05:57,330
Okay.
116
117
00:05:57,330 --> 00:06:02,330
So the result of this should be 18 minus 15, which is 3.
117
118
00:06:03,290 --> 00:06:06,320
And so we should see 'Wait another 3 years".
118
119
00:06:06,320 --> 00:06:07,453
So let's checked that.
119
120
00:06:09,060 --> 00:06:12,180
And indeed, that's exactly what we got.
120
121
00:06:12,180 --> 00:06:15,270
And so again, since this condition here
121
122
00:06:15,270 --> 00:06:18,920
so this operation basically turned out to be false
122
123
00:06:18,920 --> 00:06:22,350
then this block of code was executed.
123
124
00:06:22,350 --> 00:06:23,270
Great.
124
125
00:06:23,270 --> 00:06:25,180
Just keep in mind that this 'else' block
125
126
00:06:25,180 --> 00:06:27,340
is actually optional, right?
126
127
00:06:27,340 --> 00:06:29,840
So in the beginning we didn't have this 'else' block
127
128
00:06:29,840 --> 00:06:31,550
and it still did work.
128
129
00:06:31,550 --> 00:06:33,310
So when there is no 'else' block,
129
130
00:06:33,310 --> 00:06:36,730
JavaScript will then simply move on to the next line
130
131
00:06:36,730 --> 00:06:38,200
after the, 'if' statement
131
132
00:06:38,200 --> 00:06:41,390
in case that the condition is false.
132
133
00:06:41,390 --> 00:06:42,223
Great.
133
134
00:06:42,223 --> 00:06:44,820
Now, you know what an 'if', 'else' statement is
134
135
00:06:44,820 --> 00:06:46,680
and how it works.
135
136
00:06:46,680 --> 00:06:49,420
And this is actually one of the most important things
136
137
00:06:49,420 --> 00:06:50,640
in programming.
137
138
00:06:50,640 --> 00:06:52,930
We take decisions with code all the time
138
139
00:06:52,930 --> 00:06:55,290
which is essentially what we did here
139
140
00:06:55,290 --> 00:06:58,400
so that we can execute certain parts of our program
140
141
00:06:58,400 --> 00:07:00,740
based on certain conditions.
141
142
00:07:00,740 --> 00:07:01,820
Okay.
142
143
00:07:01,820 --> 00:07:04,780
Now what we have here is 'if', 'else' statement,
143
144
00:07:04,780 --> 00:07:06,380
which is the official name
144
145
00:07:06,380 --> 00:07:09,160
of this kind of construction here,
145
146
00:07:09,160 --> 00:07:10,993
is called a control structure.
146
147
00:07:12,170 --> 00:07:13,250
So basically this
147
148
00:07:16,658 --> 00:07:19,400
if this structure here is called an 'if', 'else'
148
149
00:07:19,400 --> 00:07:20,946
control structure
149
150
00:07:20,946 --> 00:07:23,590
and it is called a control structure
150
151
00:07:23,590 --> 00:07:26,050
because this structure actually allows us
151
152
00:07:26,050 --> 00:07:30,850
to have more control over the way that our code is executed.
152
153
00:07:30,850 --> 00:07:33,120
For example, in this, 'if', 'else' statement
153
154
00:07:33,120 --> 00:07:36,820
the whole code does not just execute in a linear way.
154
155
00:07:36,820 --> 00:07:39,430
So JavaScript does not execute
155
156
00:07:39,430 --> 00:07:41,600
all of this code here, linearly.
156
157
00:07:41,600 --> 00:07:44,590
Instead we can now control blocks of code
157
158
00:07:44,590 --> 00:07:46,010
that should execute
158
159
00:07:46,010 --> 00:07:48,620
and blocks that should not execute.
159
160
00:07:48,620 --> 00:07:51,120
And so again that gives us a lot more control
160
161
00:07:51,120 --> 00:07:52,990
over how our code works
161
162
00:07:52,990 --> 00:07:55,603
and that's why this is called a control structure.
162
163
00:07:56,670 --> 00:07:57,503
Okay.
163
164
00:07:57,503 --> 00:07:59,100
And there are other control structures
164
165
00:07:59,100 --> 00:08:00,950
that we're gonna go into a bit later.
165
166
00:08:01,790 --> 00:08:02,623
All right.
166
167
00:08:02,623 --> 00:08:06,620
So I hope that you can see how this really takes our code
167
168
00:08:06,620 --> 00:08:08,520
to the next level.
168
169
00:08:08,520 --> 00:08:09,500
Now just to make sure
169
170
00:08:09,500 --> 00:08:11,570
that you really understood this concept
170
171
00:08:11,570 --> 00:08:15,470
let's create another, a very small and simple example here.
171
172
00:08:15,470 --> 00:08:19,430
And this time let's actually create a variable conditionally
172
173
00:08:19,430 --> 00:08:22,730
and not just always use''console.log'.
173
174
00:08:22,730 --> 00:08:27,150
So let's create a birth year variable.
174
175
00:08:27,150 --> 00:08:31,223
I think we don't have it in this lecture yet.
175
176
00:08:32,100 --> 00:08:33,313
That's right.
176
177
00:08:34,680 --> 00:08:36,060
And so now what we want to do
177
178
00:08:36,060 --> 00:08:39,030
is to create a variable called 'century'
178
179
00:08:39,030 --> 00:08:41,950
which will basically contain the century
179
180
00:08:41,950 --> 00:08:44,320
in which this person was born.
180
181
00:08:44,320 --> 00:08:48,030
So in this case, it was the 20th century.
181
182
00:08:48,030 --> 00:08:50,490
But for example, if the person was born
182
183
00:08:50,490 --> 00:08:55,380
in, like, let's say 2015, then it would be the 21st century.
183
184
00:08:55,380 --> 00:08:57,370
So to do that, we can, of course write
184
185
00:08:57,370 --> 00:08:58,763
an 'if', 'else' statement,
185
186
00:09:00,450 --> 00:09:05,450
so we can say if the birth year is below
186
187
00:09:06,670 --> 00:09:08,383
or equal to 2000,
187
188
00:09:10,380 --> 00:09:13,880
then let century equal 20.
188
189
00:09:17,730 --> 00:09:20,423
And so that then means the 20th century.
189
190
00:09:22,690 --> 00:09:26,923
And if not, then let century be 21.
190
191
00:09:32,010 --> 00:09:35,130
So we're assuming that the person is not born
191
192
00:09:35,130 --> 00:09:38,210
in the 19th century.
192
193
00:09:38,210 --> 00:09:42,870
So we're not accounting for something like this,
193
194
00:09:42,870 --> 00:09:44,190
for example.
194
195
00:09:44,190 --> 00:09:47,293
So instead we always have the 20th or the 21st century.
195
196
00:09:49,040 --> 00:09:50,990
Now to actually make this work
196
197
00:09:50,990 --> 00:09:54,310
we need to define the century variable outside
197
198
00:09:54,310 --> 00:09:56,620
of the, 'if' or 'else' blocks.
198
199
00:09:56,620 --> 00:09:59,260
We will go deeply into why that is
199
200
00:09:59,260 --> 00:10:01,640
but for now, what you need to know is
200
201
00:10:01,640 --> 00:10:04,400
that this is because a variable that we define
201
202
00:10:04,400 --> 00:10:09,060
inside of any code block, which is what this year is,
202
203
00:10:09,060 --> 00:10:12,350
so this is a code block, and this is a code block.
203
204
00:10:12,350 --> 00:10:13,990
And any variable that we declare
204
205
00:10:13,990 --> 00:10:17,050
inside of one of these blocks will not be accessible
205
206
00:10:17,050 --> 00:10:19,500
outside of the block.
206
207
00:10:19,500 --> 00:10:22,693
So let me just show that to you very quickly.
207
208
00:10:24,400 --> 00:10:28,422
So if I try to read 'century' now, then I will get a
208
209
00:10:28,422 --> 00:10:33,422
an error here, you see, century is not defined.
209
210
00:10:33,440 --> 00:10:37,950
And so what we need to do is to define 'century' outside
210
211
00:10:37,950 --> 00:10:39,750
and simply leave it empty.
211
212
00:10:39,750 --> 00:10:43,853
And then in here we can then conditionally reassign it.
212
213
00:10:45,480 --> 00:10:46,820
Okay.
213
214
00:10:46,820 --> 00:10:49,053
And so now let's see how this works.
214
215
00:10:50,260 --> 00:10:53,600
And indeed now we get the century 20.
215
216
00:10:53,600 --> 00:10:57,270
So 1998 is below 2000
216
217
00:10:57,270 --> 00:11:01,080
and so this person was born in the 20th century.
217
218
00:11:01,080 --> 00:11:03,240
Now let's say 2012
218
219
00:11:03,240 --> 00:11:06,063
and so that should not be 21st century.
219
220
00:11:07,290 --> 00:11:10,973
And indeed that's exactly the result.
220
221
00:11:11,840 --> 00:11:15,270
So I hope that this example too made sense to you.
221
222
00:11:15,270 --> 00:11:17,050
Now, don't worry about the fact
222
223
00:11:17,050 --> 00:11:20,220
that we had to declare the century variable outside
223
224
00:11:20,220 --> 00:11:23,270
of the blocks, but matters here is that you understand
224
225
00:11:23,270 --> 00:11:26,640
the logic of the 'if', 'else' statement.
225
226
00:11:26,640 --> 00:11:27,473
Okay?
226
227
00:11:27,473 --> 00:11:29,890
And I hope that was clear enough
227
228
00:11:29,890 --> 00:11:32,610
but anyway, let's quickly recap.
228
229
00:11:32,610 --> 00:11:34,590
We can take decisions using code,
229
230
00:11:34,590 --> 00:11:36,370
using the, 'if', 'else' statement.
230
231
00:11:36,370 --> 00:11:40,720
And for that we write 'if', and then we open apprentices
231
232
00:11:40,720 --> 00:11:42,720
and in there we need a condition
232
233
00:11:42,720 --> 00:11:44,610
and the condition is essentially
233
234
00:11:44,610 --> 00:11:48,580
any code that returns a true or a false value.
234
235
00:11:48,580 --> 00:11:50,960
And so this is a perfect example of that
235
236
00:11:50,960 --> 00:11:55,540
because this operator here will return true or false right?
236
237
00:11:55,540 --> 00:11:59,220
And so we can use this operator to create a condition
237
238
00:11:59,220 --> 00:12:00,130
essentially.
238
239
00:12:00,130 --> 00:12:04,550
Then if the condition is true, this block will be executed.
239
240
00:12:04,550 --> 00:12:06,440
So all the code that is in there
240
241
00:12:06,440 --> 00:12:09,400
no matter how many lines of code there are.
241
242
00:12:09,400 --> 00:12:12,380
Now, if the condition turns out to be false
242
243
00:12:12,380 --> 00:12:16,070
then this 'else' block will be executed instead.
243
244
00:12:16,070 --> 00:12:19,480
So JavaScript will then skip this first block
244
245
00:12:19,480 --> 00:12:21,640
and go to the second block
245
246
00:12:21,640 --> 00:12:23,820
which is this one, but the 'else' block
246
247
00:12:23,820 --> 00:12:25,190
is actually optional.
247
248
00:12:25,190 --> 00:12:27,230
So if you don't have the 'else' block
248
249
00:12:27,230 --> 00:12:30,210
then simply no code will be executed.
249
250
00:12:30,210 --> 00:12:31,580
And this in a nutshell
250
251
00:12:31,580 --> 00:12:34,360
is how the, 'if', 'else' statement works
251
252
00:12:34,360 --> 00:12:37,580
and remember that this is really powerful.
252
253
00:12:37,580 --> 00:12:41,130
So make sure that you really understand how this works.
253
254
00:12:41,130 --> 00:12:43,870
And when you do understand, then you can move on
254
255
00:12:43,870 --> 00:12:46,760
to the next video where there is a small challenge
255
256
00:12:46,760 --> 00:12:47,763
waiting for you.
20662
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.