Jonas: Welcome back to the next lecture,
and this is a very important one, because in this one
we're gonna start taking decisions using code.
So JavaScript, as well as most other programming languages
out there, has a couple of control structures
which are features that allow us to execute
only certain parts of our code,
or to execute parts multiple times.
The first control structure that we will talk about
is the 'if / else' statement which is a conditional
which basically allows us to take decisions using code.
Let me show you how that works.
First, let's bring back some of our variables.
So, let's say first name...
...is, again, 'John'.
Now, let's create a variable that we didn't use before,
which is for the civil status.
So, "civilStatus", and so this one basically says
if you're married, or single, or divorced
or something like that.
Let's just say that John is a 'single', so he's not married.
Now what we want to do here is to log
a string to the console, whether he is married or not.
For that, we can use an 'if / else' statement
to take decisions: we write 'if', and then
we open parenthesis, then in here
we have to write a logical condition,
so something that returns a Boolean 'true' or 'false' value.
That's something like we did before,
so for example, this one here.
This will return a 'true' or 'false' value,
so it's a perfect logic condition.
Now, in this case we don't want a greater
over smaller comparison like the one that I showed you,
but instead we want an equality operator.
Basically, what we want to do is to test
if the civil status is 'married'.
Let me show you how to do that.
We can say civil status equals...
...'married'.
So, this condition here will return 'true'
if the civil status is 'married', and 'false' if it's not.
This here, again, is like a logical operator
which returns 'true' or 'false'.
There's also the version with just two equals,
but let's talk about that one a bit later.
Again, this here will test whether the
civil status is 'married' or not.
Then, we open curly braces, and then this here
is the block, the 'if' block, and in here we can now
write our instructions, basically;
in this case, our 'console.log()',
and what we want to log to console is
'firstName...
...is married!'
If the civil status is 'married', then this text here
will be logged to the console,
and, if not, well then nothing happens.
So let's reload it and, indeed, nothing happens.
That's because right now, this here turns out
to be 'false' because the civil status
is actually 'single' and not 'married'.
This is different, so when we have 'false',
well, then this piece of code is not executed.
Let's change it here to 'married' for a second
just to see if it's working just fine, and it is.
Now it says 'John is married!', because the civil status
is the exact same thing as 'married' here.
Anyway, let's change it back to 'single',
because now I want to show you
the 'else' part of the 'if / else' statement.
We can have only the 'if' part,
but we can also add an 'else' statement.
So, we can say 'else', then we have the 'else' block here.
What this does is that, if this is not 'true' here,
so if it's 'false', then it will execute the 'else' block.
Let's test it out...
...will hopefully marry soon.
That, of course, means that he is not married at this point.
Let's test it, and let's keep in mind that here it tests
'single', and so this part here will not be executed,
because this here is false, and that means
that we're gonna jump right to the 'else' block,
so this text here should be logged to the console.
Check it out, (takes a breath) and yeah it works.
So, 'John will hopefully marry soon'.
Now, let's say that we actually
have a Boolean variable already.
We can have a variable called 'isMarried'...
...and let's set it to 'false', and let's then
test what happens in that case.
So, instead of this test here we could put
'isMarried', and then we could put equals 'true',
but that part is actually not really necessary,
because what we need here in this condition
is a 'true' or 'false' value, but this 'isMarried' variable
is already a Boolean; right now it is set to 'false',
but we could also set it to 'true', so let's do that.
Again, what we want here is a true/false value,
but 'isMarried' is already 'true', it is already a Boolean,
so this part is not even necessary.
Does that make sense?
Here, we just need a true/false value, again,
so no need to write the 'true'.
Let's test that, and now it says 'John is married!' here,
coming from line 173, so this one here.
Of course, this one here is still coming from
this 'if / else' statement from before
where we say that the civil status
is 'single' and not 'married'.
This here is just another example where I wanted to show you
that it's actually enough to pass a 'true' or 'false' value
here into this condition.
I hope that makes sense, and that you now learned
how we can make decisions using code.
Of course, we could do much more difficult
and much more complex stuff into these blocks,
the 'if' block or the 'else' block.
Now, just to finish this lecture,
let's bring back the code from our coding challenge
that we coded up before, and let's basically
write something better to the console.
What we did here is to simply log this string here,
then together with this true/false value.
That's not really the best solution, right,
to just print true/false to the console?
Now that you know the 'if / else' statement,
we can do a lot better than this, right?
What we can do, instead of all of this,
let's comment it out actually...
...is to write 'if...
...BMIMark is greater...
...than BMIJohn'.
Well in this case we want to print something to the console,
and if not, then we want to log something else.
In this case, we have of course 'Mark's',
and now, the escape string...
...'than, John's', and of course, escaping.
Let's copy it, and so here it's the other way around...
...'higher than Mark's'.
Let's quickly recap what we did here.
So, we're testing if the BMI of Mark is greater than John's.
This here, of course, as you already know,
will return a 'true' or 'false' value, and that is exactly
what we need in an 'if / else' statement.
In this case, it will then log to the console
'Mark's BMI is higher than John's', and if it's not,
so if this here is 'false', then of course
John's BMI must be higher than Mark's,
and so then that's exactly what we log to the console.
Now just to test it out, we say
'Mark's BMI is higher than John's'.
It's missing the 's', but that doesn't really matter,
and just to test the other scenario let's quickly increase
this mass here, and so now John's should be larger,
and indeed, now 'John's BMI is higher than Mark's'.
Putting it back, and so now again
Mark's would be higher, and indeed it is working.
Now you know how to take decisions with code.
That's awesome, but this is really just the fundamentals
of conditionals and decision taking with code,
so there's a lot more to learn.
Stay tuned for the next videos!