Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
0 Welcome back. 1
Let's now make coding even more fun 2
and actually take decisions with our code 3
to make it look a lot more real. 4
So let's say it that we want to write a program 5
which checks whether a person 6
is allowed to start taking a driver's license or not. 7
And if she is, then it will print that to the console. 8
And if not, it will print how many years are left 9
until the person can start taking the driver's license. 10
So let's start with an age and let's set it to 19. 11
So we have this age and we already know 12
how to check if this age is at least 18, 13
which is the legal required age to start a driving license 14
at least here in Europe. 15
So let's actually create a variable with that. 16
And I think we actually already did that. 17
Let's call this one 'is old enough'. 18
So again, with a variable name 19
that describes exactly what we want here. 20
So remember to check if the age is at least 18, 21
we need it to be greater or equal than 18. 22
And so this will then include 18. 23
So if the age is 18, the result of this operator here 24
or of this operation will then be true. 25
Only if the age is 17 or 16 or anything below, 26
it will turn out to be false. 27
Okay. 28
So we have a Boolean value now, 29
and we can use this 30
to take decisions using an 'if statement' 31
and the 'if statement' works like this. 32
All we have to do is write 'if' then open parenthesis 33
and then in here goes a condition that is evaluated. 34
And if this condition turns out to be true, 35
then this block will be executed. 36
So basically whatever code that we write 37
into this block, which is denoted by 38
these curly braces will be executed 39
whenever the condition that's here is true. 40
So in this case, let's put the condition, 41
and with condition I basically mean something 42
that is a Boolean value. 43
So let's use, 'or is old enough' variable here 44
because we already know that this will always be a Boolean. 45
So again, whenever this value here is true 46
then the code that's in this block here will be executed. 47
And if it's false, well, then it's not going to be executed. 48
So let's log to the console. 49
Sarah can start driving license 50
and let's just add a fun emoji here 51
and on the Mac, that's Command + Control + Space 52
and on Windows 10, the shortcut is Windows + . 53
and on Windows 10, the shortcut is Windows + . 54
but, adding the emoji here is not really important. 55
So let's give it a safe and try this out now. 56
And indeed we get, Sarah can start her driving license 57
and that's because the age is 19. 58
And so it's above 18. 59
So this is true. 60
And so if 'is old enough' is true 61
then this block of code here will be executed 62
which in this case is just one line 63
but it could of course be multiple lines. 64
So now let's put it to something else. 65
So 15. 66
And so now this is false. 67
And so then this code block here should not be executed. 68
So give it a safe, let's try. 69
And indeed we get no output this time. 70
And so that means that it worked. 71
So let's put it back to 19. 72
And in practice, 73
we always write this condition directly here. 74
So let's do that. 75
So what we want is just this part. 76
So let's get rid of this. 77
And so this is a lot more common. 78
Let's actually getting rid of this. 79
And so we basically do this operation here. 80
This will then return true or false. 81
And depending on that, 82
this code block will then be executed. 83
And actually let's put it back to 15 84
because now we can do something else 85
which is to actually add an 'else' block. 86
And we do that simply 87
by writing else right after this first, 'if' block, 88
and then we add another block. 89
So what do you think this 'else' block is for? 90
Well, this 'else' block will basically be executed 91
whenever this condition here is false. 92
So right now the age is below 18. 93
And so this is false. 94
And so as we already know, 95
this block here will not be executed. 96
Instead, the 'else' block will be executed. 97
So whatever code we have here. 98
So let's write some code in here. 99
And as I sad in the beginning, 100
we will calculate how many years are left 101
for Sarah to start taking her license. 102
So let's calculate that, 'years left'. 103
And so that's simply, 18 minus the age. 104
Right? 105
And then we can log in a nice string into the console, 106
which contains this variable. 107
And as we learned in the last lecture 108
the best way of doing that is to use a template literal. 109
So back ticks. 110
And then we say, Sarah is too young'. 111
'Wait another' 112
and then we insert or placeholder here basically. 113
So years left 114
and then years. 115
Okay. 116
So the result of this should be 18 minus 15, which is 3. 117
And so we should see 'Wait another 3 years". 118
So let's checked that. 119
And indeed, that's exactly what we got. 120
And so again, since this condition here 121
so this operation basically turned out to be false 122
then this block of code was executed. 123
Great. 124
Just keep in mind that this 'else' block 125
is actually optional, right? 126
So in the beginning we didn't have this 'else' block 127
and it still did work. 128
So when there is no 'else' block, 129
JavaScript will then simply move on to the next line 130
after the, 'if' statement 131
in case that the condition is false. 132
Great. 133
Now, you know what an 'if', 'else' statement is 134
and how it works. 135
And this is actually one of the most important things 136
in programming. 137
We take decisions with code all the time 138
which is essentially what we did here 139
so that we can execute certain parts of our program 140
based on certain conditions. 141
Okay. 142
Now what we have here is 'if', 'else' statement, 143
which is the official name 144
of this kind of construction here, 145
is called a control structure. 146
So basically this 147
if this structure here is called an 'if', 'else' 148
control structure 149
and it is called a control structure 150
because this structure actually allows us 151
to have more control over the way that our code is executed. 152
For example, in this, 'if', 'else' statement 153
the whole code does not just execute in a linear way. 154
So JavaScript does not execute 155
all of this code here, linearly. 156
Instead we can now control blocks of code 157
that should execute 158
and blocks that should not execute. 159
And so again that gives us a lot more control 160
over how our code works 161
and that's why this is called a control structure. 162
Okay. 163
And there are other control structures 164
that we're gonna go into a bit later. 165
All right. 166
So I hope that you can see how this really takes our code 167
to the next level. 168
Now just to make sure 169
that you really understood this concept 170
let's create another, a very small and simple example here. 171
And this time let's actually create a variable conditionally 172
and not just always use''console.log'. 173
So let's create a birth year variable. 174
I think we don't have it in this lecture yet. 175
That's right. 176
And so now what we want to do 177
is to create a variable called 'century' 178
which will basically contain the century 179
in which this person was born. 180
So in this case, it was the 20th century. 181
But for example, if the person was born 182
in, like, let's say 2015, then it would be the 21st century. 183
So to do that, we can, of course write 184
an 'if', 'else' statement, 185
so we can say if the birth year is below 186
or equal to 2000, 187
then let century equal 20. 188
And so that then means the 20th century. 189
And if not, then let century be 21. 190
So we're assuming that the person is not born 191
in the 19th century. 192
So we're not accounting for something like this, 193
for example. 194
So instead we always have the 20th or the 21st century. 195
Now to actually make this work 196
we need to define the century variable outside 197
of the, 'if' or 'else' blocks. 198
We will go deeply into why that is 199
but for now, what you need to know is 200
that this is because a variable that we define 201
inside of any code block, which is what this year is, 202
so this is a code block, and this is a code block. 203
And any variable that we declare 204
inside of one of these blocks will not be accessible 205
outside of the block. 206
So let me just show that to you very quickly. 207
So if I try to read 'century' now, then I will get a 208
an error here, you see, century is not defined. 209
And so what we need to do is to define 'century' outside 210
and simply leave it empty. 211
And then in here we can then conditionally reassign it. 212
Okay. 213
And so now let's see how this works. 214
And indeed now we get the century 20. 215
So 1998 is below 2000 216
and so this person was born in the 20th century. 217
Now let's say 2012 218
and so that should not be 21st century. 219
And indeed that's exactly the result. 220
So I hope that this example too made sense to you. 221
Now, don't worry about the fact 222
that we had to declare the century variable outside 223
of the blocks, but matters here is that you understand 224
the logic of the 'if', 'else' statement. 225
Okay? 226
And I hope that was clear enough 227
but anyway, let's quickly recap. 228
We can take decisions using code, 229
using the, 'if', 'else' statement. 230
And for that we write 'if', and then we open apprentices 231
and in there we need a condition 232
and the condition is essentially 233
any code that returns a true or a false value. 234
And so this is a perfect example of that 235
because this operator here will return true or false right? 236
And so we can use this operator to create a condition 237
essentially. 238
Then if the condition is true, this block will be executed. 239
So all the code that is in there 240
no matter how many lines of code there are. 241
Now, if the condition turns out to be false 242
then this 'else' block will be executed instead. 243
So JavaScript will then skip this first block 244
and go to the second block 245
which is this one, but the 'else' block 246
is actually optional. 247
So if you don't have the 'else' block 248
then simply no code will be executed. 249
And this in a nutshell 250
is how the, 'if', 'else' statement works 251
and remember that this is really powerful. 252
So make sure that you really understand how this works. 253
And when you do understand, then you can move on 254
to the next video where there is a small challenge 255
waiting for you.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.