-: Welcome back.
Let's now make coding even more fun
and actually take decisions with our code
to make it look a lot more real.
So let's say it that we want to write a program
which checks whether a person
is allowed to start taking a driver's license or not.
And if she is, then it will print that to the console.
And if not, it will print how many years are left
until the person can start taking the driver's license.
So let's start with an age and let's set it to 19.
So we have this age and we already know
how to check if this age is at least 18,
which is the legal required age to start a driving license
at least here in Europe.
So let's actually create a variable with that.
And I think we actually already did that.
Let's call this one 'is old enough'.
So again, with a variable name
that describes exactly what we want here.
So remember to check if the age is at least 18,
we need it to be greater or equal than 18.
And so this will then include 18.
So if the age is 18, the result of this operator here
or of this operation will then be true.
Only if the age is 17 or 16 or anything below,
it will turn out to be false.
Okay.
So we have a Boolean value now,
and we can use this
to take decisions using an 'if statement'
and the 'if statement' works like this.
All we have to do is write 'if' then open parenthesis
and then in here goes a condition that is evaluated.
And if this condition turns out to be true,
then this block will be executed.
So basically whatever code that we write
into this block, which is denoted by
these curly braces will be executed
whenever the condition that's here is true.
So in this case, let's put the condition,
and with condition I basically mean something
that is a Boolean value.
So let's use, 'or is old enough' variable here
because we already know that this will always be a Boolean.
So again, whenever this value here is true
then the code that's in this block here will be executed.
And if it's false, well, then it's not going to be executed.
So let's log to the console.
Sarah can start driving license
and let's just add a fun emoji here
and on the Mac, that's Command + Control + Space
and on Windows 10, the shortcut is Windows + .
but, adding the emoji here is not really important.
So let's give it a safe and try this out now.
And indeed we get, Sarah can start her driving license
and that's because the age is 19.
And so it's above 18.
So this is true.
And so if 'is old enough' is true
then this block of code here will be executed
which in this case is just one line
but it could of course be multiple lines.
So now let's put it to something else.
So 15.
And so now this is false.
And so then this code block here should not be executed.
So give it a safe, let's try.
And indeed we get no output this time.
And so that means that it worked.
So let's put it back to 19.
And in practice,
we always write this condition directly here.
So let's do that.
So what we want is just this part.
So let's get rid of this.
And so this is a lot more common.
Let's actually getting rid of this.
And so we basically do this operation here.
This will then return true or false.
And depending on that,
this code block will then be executed.
And actually let's put it back to 15
because now we can do something else
which is to actually add an 'else' block.
And we do that simply
by writing else right after this first, 'if' block,
and then we add another block.
So what do you think this 'else' block is for?
Well, this 'else' block will basically be executed
whenever this condition here is false.
So right now the age is below 18.
And so this is false.
And so as we already know,
this block here will not be executed.
Instead, the 'else' block will be executed.
So whatever code we have here.
So let's write some code in here.
And as I sad in the beginning,
we will calculate how many years are left
for Sarah to start taking her license.
So let's calculate that, 'years left'.
And so that's simply, 18 minus the age.
Right?
And then we can log in a nice string into the console,
which contains this variable.
And as we learned in the last lecture
the best way of doing that is to use a template literal.
So back ticks.
And then we say, Sarah is too young'.
'Wait another'
and then we insert or placeholder here basically.
So years left
and then years.
Okay.
So the result of this should be 18 minus 15, which is 3.
And so we should see 'Wait another 3 years".
So let's checked that.
And indeed, that's exactly what we got.
And so again, since this condition here
so this operation basically turned out to be false
then this block of code was executed.
Great.
Just keep in mind that this 'else' block
is actually optional, right?
So in the beginning we didn't have this 'else' block
and it still did work.
So when there is no 'else' block,
JavaScript will then simply move on to the next line
after the, 'if' statement
in case that the condition is false.
Great.
Now, you know what an 'if', 'else' statement is
and how it works.
And this is actually one of the most important things
in programming.
We take decisions with code all the time
which is essentially what we did here
so that we can execute certain parts of our program
based on certain conditions.
Okay.
Now what we have here is 'if', 'else' statement,
which is the official name
of this kind of construction here,
is called a control structure.
So basically this
if this structure here is called an 'if', 'else'
control structure
and it is called a control structure
because this structure actually allows us
to have more control over the way that our code is executed.
For example, in this, 'if', 'else' statement
the whole code does not just execute in a linear way.
So JavaScript does not execute
all of this code here, linearly.
Instead we can now control blocks of code
that should execute
and blocks that should not execute.
And so again that gives us a lot more control
over how our code works
and that's why this is called a control structure.
Okay.
And there are other control structures
that we're gonna go into a bit later.
All right.
So I hope that you can see how this really takes our code
to the next level.
Now just to make sure
that you really understood this concept
let's create another, a very small and simple example here.
And this time let's actually create a variable conditionally
and not just always use''console.log'.
So let's create a birth year variable.
I think we don't have it in this lecture yet.
That's right.
And so now what we want to do
is to create a variable called 'century'
which will basically contain the century
in which this person was born.
So in this case, it was the 20th century.
But for example, if the person was born
in, like, let's say 2015, then it would be the 21st century.
So to do that, we can, of course write
an 'if', 'else' statement,
so we can say if the birth year is below
or equal to 2000,
then let century equal 20.
And so that then means the 20th century.
And if not, then let century be 21.
So we're assuming that the person is not born
in the 19th century.
So we're not accounting for something like this,
for example.
So instead we always have the 20th or the 21st century.
Now to actually make this work
we need to define the century variable outside
of the, 'if' or 'else' blocks.
We will go deeply into why that is
but for now, what you need to know is
that this is because a variable that we define
inside of any code block, which is what this year is,
so this is a code block, and this is a code block.
And any variable that we declare
inside of one of these blocks will not be accessible
outside of the block.
So let me just show that to you very quickly.
So if I try to read 'century' now, then I will get a
an error here, you see, century is not defined.
And so what we need to do is to define 'century' outside
and simply leave it empty.
And then in here we can then conditionally reassign it.
Okay.
And so now let's see how this works.
And indeed now we get the century 20.
So 1998 is below 2000
and so this person was born in the 20th century.
Now let's say 2012
and so that should not be 21st century.
And indeed that's exactly the result.
So I hope that this example too made sense to you.
Now, don't worry about the fact
that we had to declare the century variable outside
of the blocks, but matters here is that you understand
the logic of the 'if', 'else' statement.
Okay?
And I hope that was clear enough
but anyway, let's quickly recap.
We can take decisions using code,
using the, 'if', 'else' statement.
And for that we write 'if', and then we open apprentices
and in there we need a condition
and the condition is essentially
any code that returns a true or a false value.
And so this is a perfect example of that
because this operator here will return true or false right?
And so we can use this operator to create a condition
essentially.
Then if the condition is true, this block will be executed.
So all the code that is in there
no matter how many lines of code there are.
Now, if the condition turns out to be false
then this 'else' block will be executed instead.
So JavaScript will then skip this first block
and go to the second block
which is this one, but the 'else' block
is actually optional.
So if you don't have the 'else' block
then simply no code will be executed.
And this in a nutshell
is how the, 'if', 'else' statement works
and remember that this is really powerful.
So make sure that you really understand how this works.
And when you do understand, then you can move on
to the next video where there is a small challenge
waiting for you.