Instructor: All right, so let's switch now
to another primitive type to continue our
exploration of the if then statement.
I'm going to continue on outside
of the code block introduced in the last video
and I'm going to type int topScore is equal to 100
and on the next line I'm going to type
if parentheses topScore is equal to 100
and we can see we're using the two equals signs there
because it's not an assignment,
we're checking to see whether
they are the same, they equal the same.
Then we're gonna add our code block as I talked about
and I'm gonna print our message you got the high score.
So you can see that that equal to operator,
which is the correct name for the operator on line 54,
that's comparing topScore to the literal value 100.
So it should show our output
that we've defined in our code block on line 55
because we've defined topScore as 100.
So if we run this we should see the output.
All right, so we can see we've got you got the high score,
so that's working correctly,
but we can also do something like this,
we can use the not equal to operator,
which is one that we haven't looked at yet,
and that's the exclamation mark and the equals.
So I'm gonna come back there and change that to
exclamation mark, which is not, and the equals.
So we're asking the question with our operator,
is topScore not equal to 100?
So that's the expression.
So in that case clearly if we had previously had
topScore equal 100 work,
topScore not equal to 100 shouldn't work.
So let's run that and this time
we shouldn't see you got the high score,
and no, that wasn't displayed, so that would be correct,
and the reason is topScore does equal 100
so therefore the expression
topScore not equal to 100 was false,
and therefore the line on 55 wasn't executed.
Now we can also use another operator,
the greater than operator.
Let's replace that with greater than.
So that's now checking to see or it's asking the question,
does topScore, or is topScore greater than the value 100?
And we know the answer's no to that
but let's run it anyway to confirm.
Then we haven't got the you got the high score message
which is correct
because we've used the greater than operator
and obviously topScore is equal to 100,
so is topScore greater than 100 is false
because of course topScore is 100.
But we can define that further, instead of using
greater than, we can use greater than or equal to,
and we can do that with the greater than
and equals sign together.
Now we're testing whether topScore
is greater than or equal to 100.
So it's one operator to cover both those scenarios,
and if we run that this time we should see the output,
and we're seeing the output because of course
topScore is equal to 100,
and the greater than equal operator checks
to see whether the value is 100 or greater in this case,
or whatever is on the right hand side, or the right operand.
And we can contrast that and do a similar thing,
we've also got a less than operator.
We'll just run through these pretty quickly.
Less than operator won't come out
because topScore isn't less than 100, it is equal to 100,
but we can use less than equal.
Similar thing to greater than equal.
This time we're testing to see
whether topScore is 100 or less.
We run that and we should see the output,
and again we're seeing that because
topScore is equal to 100.
Now just to confirm that this is working,
so if this topScore less than 100,
we come back and change that again.
Let's make the topScore value to be 80.
Just wanted to show you that it does actually work.
So now we're testing to see whether
topScore's less than 100.
Now that we've defined it as 80 on line 53
of course that should be true, the expression on line 54,
so when we run this,
this time we have got the you got the high score message
because topScore is less than 100,
because we defined it to be 80 on line 53.
All right, so let's introduce another variable.
I want to show you a slightly more complex example,
so I'm going to type int secondTopScore is equal to 60.
Now you can get a bit more complex,
we can type something like if
then our parentheses and we can put topScore,
if that's greater than secondTopScore,
two ampersand characters which stands for and,
topScore less than 100
and I'm going to add a code block there
and I'm gonna print out a message,
greater than second top score
and less than 100.
So looking at this code,
this is actually performing two tests now.
So it's saying if the topScore
is greater than the secondTopScore,
and we're also saying, but,
also we want to check whether the topScore is less than 100.
So the two ampersands put together like that
are is what's called a logical and operator.
So it ensures that both operands are true,
or tests to see whether both operands
to the left of the double ampersand and to the right,
both of those have to be set to true, have a value of true.
And there's also a single ampersand you can use
which is called a bitwise and, which works in a similar way
but you shouldn't really use that here,
you need to use the both ampersands.
We'll talk about bitwise operators later
which are something that you use far less regularly
than you would use the logical and operator
that we're using here.
So in this case we should find the message will be printed
and the reason is topScore firstly
is greater than secondTopScore,
and secondTopScore is 60,
but we've assigned the test, the two ampersands,
if this topScore is less than 100,
and of course topScore is set to 80.
So if we run that
greater than second top score and less than 100
is correctly output.
And just to confirm that this is working let's make
the secondTopScore, make that value 81
which now changes the scenario
because now suddenly secondTopScore
is greater than topScore.
So let's run that
and you can see we haven't got the message
showing that it only needs one side,
for the left operand or right operand to be false,
and then the entire condition
or in this case the code in the code block
won't be executed.
All right, so that's a more advanced if then statement.
Let's finish the video here and then in the next one
we'll continue our exploration of operators still further
and we'll also be looking at something
called operator precedence.
So see you in the next video.