Tim: All right so I'm gonna continue
exploring some more operators.
But to do that I need to talk firstly
about the if-then statement.
So the if-then statement is the most basic
of all the control flow statements available in Java.
And it tells your programme to execute a certain
section of the code
only if a particular test
which is an expression evaluates to true.
Now this is known as conditional logic.
So conditional logic uses specific statements in Java
to allow us to check a condition
and to execute certain code
based on whether that condition
the expression is true.
So let's see how this works in practise.
Right so I'm gonna start by continuing on
down here on line 47.
I'm going to type Boolean, isAlieN,
equals false.
Then on the next line I'm gonna type if
in parentheses, isAlien
equals equals,
note the two equals signs there,
press enter then after the right parentheses,
noting that IntelliJ indents the code
and we'll print out the message
it is not an alien.
And put an exclamation mark there as well.
So notice a few things here
firstly the if statement does not have a semicolon
on the line where you'd normally expect it to
we normally on each line
so far in the course we've putting a semicolon.
The semicolon doesn't get added until the following line
which in this case is a system.out.println.
So you don't put a semicolon after the parentheses.
So what's happening here is the statement's
actually spanning two lines
which is perfectly valid to do in Java
and we'll talk more about whitespace
which is the concept of using multiple lines
and any number of spaces, later in the course.
But check out the operators that we've used
in these three lines of code.
So you can see that the one line
is a single equals
and that's on line 47.
And on the second line it's using two equals
put together.
So the first one, the equals on line 47
well that's an assignment operator
and we've seen that quite a few times.
It assigns the value of an expression
to the variable on the left of the equals operator.
So isAlien is that variable in this case
it's been set to false
which is the value of our expression
on the right hand side of the equals sign.
But the second line's a bit more interesting
that's got two equals signs
so it does something completely different.
What it's doing,
it's testing to see whether the particular value
is false.
So is in this line here we're saying
if the contents of the variable, isAlien, is false.
So the two equals operator
it asks if the operands
are identical or equal to each other.
So here the operands as you can see
the left hand operator isAlien
the right hand one is false.
So are they the same?
In other words, does isAlien equal
or have the value false?
So in this case yes it does
because we know that isAlien was defined as false
on line 47.
So therefore the expression isAlien
is false is true.
And that can be a bit confusing to grasp
but again we're checking if two operands
equal each other
and if that is the case
a true is returned, the expression returns the value true,
otherwise returns false to say that they aren't equal.
So the if keyword, getting back to our if-then statement,
well that takes what's in parentheses
and if and only if the result of that expression is true
will the next line be executed.
So a better way of reading this is
do the contents of what's in the parentheses
evaluate to true?
So let's try it.
So if we run this we should see
this'll come back and say it's not an alien on the screen.
So we'll just run it.
You can see it is not an alien.
Now if you scroll down to see our code again
I'm gonna just hover over there
note also that IntelliJ is telling us
there's a way to abbreviate
isAlien equals equals false by using the
exclamation isAlien
but we'll talk more about that later in the course.
For now what we're doing is still valid as well.
But IntelliJ's always there trying to give you ways to
do things in a more efficient manner.
Right so the other things I wanna show you though
is what happens if I add a semicolon
on the if line after the right parentheses.
Notice when I do that everything's still valid
we have got a little potential warning over here
but there's no errors.
Now if we run this
all right so you can see what's happened there
we've still got the same output
so at first glance you would assume that nothing's changed
and the program's still working as it should.
However if I change this
so if we instead of testing for isAlien equals false
let's change this to test for true,
we need to know if that's obviously not going to be true
that's going to be false
because we know isAlien is set to false
and we're comparing true so that should give us a false.
So what that should mean is that this line
won't be executed.
I'll run this
we still get the output.
So that's the confusing part about
putting a semicolon where there shouldn't be one.
After the if or the right of the parentheses.
By doing, putting the semicolon there
you're closing off that line of code
and completing it
and that means that this line 49 then
is basically a line that's gonna be executed no matter what.
So in other words it's no longer using
any conditional logic
it's just going to be executed no matter what.
So that's just the syntax of Java to allow you
to get away with that
but basically we've created an if-then statement
which doesn't do anything there
and that's what's confusing about adding the semicolon.
And that's the reason why you don't wanna put
a semicolon there
so I'll put that back
and again we'll still get the same output
but we know that now
we're not seeing anything with it set to true,
which is correct now
because it's now correctly evaluating this
and only outputting line 49
in the case of isAlien being set to true
and we know that's not the case
'cause on line 47.
So I'll put this back to false again.
Run it again
and now we're getting the output
because of course isAlien is set to false.
Now I wanted to show you this particular method
of having an if-then statement as you can see there
without using a code block
but you can see that here you can
run into difficulties with that
because it's not always immediately clear
looking at the code
as to what's going on
and this is one of the disadvantages
of using an if without using a code block.
What I will show you here is
if I actually add a second line here
so say we wanted to display two lines
in the case of isAlien being set to false
we're gonna do system.out.println
as we normally do
and I'm going to put and I am scared of aliens.
Let's just assume we wanted to do that.
At first glance we'll think that it'll work
we'll run it.
We're still getting the two lines,
but let's change this back to true again.
Run the code.
Confusingly we only get and I am scared of aliens showing
and not the other line
and we know that the first line
it is not an alien shouldn't be printed
because we're comparing isAlien
which has been set to false on line 47
with the literal Boolean value true on line 48
so it shouldn't work
but why has this extra line worked?
Why have we got the output for that?
Well the reason is we haven't used a code block
in our if-then statement if we don't use a code block
then only one line of code can be added
to this if-then statement.
So what we can do and what you should do
a hundred per cent of the time
is actually add a code block.
So instead of using the if statement as you can see here
we should be using instead a code block.
So a code block allows more than one statement
to be executed.
A block of code, that's why it's called a code block.
So the format here is to add the left and right parentheses
as you can see in the example
and we can put the statements we want executed
if that expression is true
in between left and right curly braces.
So let's swing back to the code
and introduce a code block before we end the video.
So I'm gonna come back here and do that
I'm going to add a left curly brace there
and on the end of the line
I'm gonna add a right curly brace to close it off.
And you can see that IntelliJ's shown you
that in blue now to indicate that that's a block.
Now that I've done that,
first thing we'll try running it with
isAlien set to true.
We should technically get no output at all.
That's now correct, we're not getting any output
because isAlien is set to true
now if I change this back to false
we should now see both lines of output.
And there it is we can see both lines of output.
So using code blocks makes the code easy to understand
but also enables us to have more than one statement execute
if in this case
isAlien was set to false and we're checking it for false
and you can see we've got the two lines of output.
So in general
I would always suggest using an if,
when you're using an if-then statement
to always use a code block
even if you are only executing one line
because if you come back to this code in the future
and decide to add another line
you may not realise you've introduced a bug
as you saw what happened to me
when I didn't use a code block.
So bottom line, always use a code block.
All right so let's move on now,
in the next video we're gonna switch
to another primitive type to continue our exploration
of the if-then statement.
So I'll see you in the next video.