Tim: All right, so moving on
in our discussion of expression statements,
we ended the last video talking about
the first expression group
that could be converted to an expression statement.
The second type that can be converted
is the use of the increment, ++,
or decrement, --, operators.
So let's have a look at some code to understand that.
So I'm going to start by typing int
playerLives is equal to three,
and we're going to use a decrement operator
to subtract a score from the player.
So let's assume we've got a game.
We wanna deduct a life from the player.
We could use this.
We could type playersLives-- semicolon.
So what we've done there is
we've created an expression statement,
and it deductions one from the current value
of playersLives.
Now we can also use that as an expression.
Clearly here, because we've got a semicolon
on the end of the line,
we're using it as an expression statement,
but let's comment that out for now
and use it in an expression.
So what I'm going to do instead is type
System.out.println,
then put double quotes Lives remaining equals.
Then outside of the double quotes,
I'm going to put plus and our variable playersLives,
and I'm gonna add the decrement operator, --,
to the end of that line,
and here, what we're doing is we're using playersLives--
as an expression.
So it's not playersLives-- on a line by itself anymore
as we've got commented out on line 17.
It's now part of line 18,
and it's used as an expression.
So looking at this code,
you'd probably think that if we run this,
we should see the value two being outputted on the screen.
Let's actually run this and see what happens.
Now weirdly, you can see the output shows
Lives remaining equals three,
even though we can clearly see on line 15
that we've defined the playersLives equals three,
and on line 18, we've used the decrement operator,
the --, to deduct one from playersLives,
so in fact, why is lives remaining showing as three?
Well, this is a good time to talk more about
the increment and decrement operators.
So when we add a -- or a ++ to a variable,
it decrements or increments the value by one,
and we have seen that earlier in the course,
but we can dictate when the decrement or increment happens,
either before or after the expression is evaluated,
and this is referred to as prefix and postfix.
So let's review the code below.
So we've got int playerLives equals three.
System.out.println.
Then we've got Lives equal plus playerLives--.
Now that currently outputs the value of three.
We saw that IntelliJ a moment ago.
Now the reason that returns the value three
is because the variable playersLives was read
and the value obtained, three in this case,
before the decrement was executed or processed.
So the decrement was considered, it was postfix.
In other words, it was done or completed
after the value was returned
to the System.out.println process
and outputted on the screen.
Now we can confirm that or we could confirm that
by adding another line of code directly below
which is just, as you can see, a System.out.println.
Basically, what we've got,
we just removed the decrement operator, the --.
Now if we put that in IntelliJ and run that,
the output of that will then be two.
Now the reason it'll show two on the second line
is because playersLives or playerLives
was decremented by one on the first line.
So that's postfix.
Let's now look at prefix.
So looking at the code again,
it's pretty well identical to what you saw
in the previous slide,
but in this case, we put the -- to the left-hand side
of the playerLives variable.
Now what will happen in this case
is we'll immediately get the outputted value two,
and the reason that we get two on that line
is that the value two is returned
because playerLives was read and decremented by one
and only at that point
was the result of the operation returned.
So the decrement was prefix,
done before the value was outputted,
in other words, or sent back
to the System.out.println process
for outputting on the screen.
So let's go back to IntelliJ and type in some code
just to test how this works.
All right, so firstly,
here we are with the code we finished on
previously with our playerLives--,
and we've still got the output
at the bottom left of the screen
showing that lives remaining equals three.
So just to verify that playerLives
is now set to a value of two,
on the next line, I'm going to type System.out.println
and Lives is now plus playerLives.
Now I'm not adding the decrement operator.
I'm just purely outputting what the current value
of playerLives is,
so we should see two lines of output now.
Let's run this and see what happens,
and you can see that the first line,
or first line that talks about lives,
Lives remaining equals three,
which is occurring on line 18, returns three,
but on the next line, you can see that in fact,
what did happen, playerLives was decremented
or reduced by one.
One was taken from the current value
and showing Lives is now two.
So we can see that playerLives was actually decremented
on line 18,
but that happened after the output.
That's because the value was done postfix,
in other words, after the value was evaluated.
All right, so let's now see how the prefix actually works.
So we know at this point playerScore
is currently set to two.
We can see that from our output
down at the bottom of the screen.
So now we're gonna use the prefix operator
and see what happens.
So I'm just gonna System.out.println,
And subtracting one from lives equals,
plus, this time, I'm gonna put a space
and then --playerLives.
So we're using the prefix version,
which, in this case, is the decrement operator
to the left of the variable playerLives,
and now, if we run that,
we can see And subtracting one from lives equals one,
so clearly, because we've used the prefix,
in other words, we've used it to the left-hand side
of the playerLives variable,
what's happened is the value has been updated
before the line was outputted.
You can see the difference.
We've got postfix, which doesn't really get,
doesn't get updated until afterwards,
but prefix gets updated before the output.
All right, so that's the decrement operator.
Now the ++, so increment operator,
can be used in an identical fashion
with the only difference being that
it's going to increment instead of decrement.
So let's print out some code just to confirm it.
So we know at the moment,
so what I'll do is I'll just take a copy of that line,
and we know that playerScore is currently set to one now,
and we can see that in the output,
the last line of output down the bottom of the screen.
So let's type some code.
We're going to put System.out.println
Lives remaining equals, plus,
and we're gonna do playersLives++,
and we know from how the decrement operator worked before
that we should see,
should be the value one there because we're using postfix
which doesn't update until the next line,
and then, we'll just confirm that by putting
Lives is now plus playerLives,
and that should be giving,
should give us a value of two,
and we'll continue on with this,
and this time, we'll use the prefix version,
so we'll use it on the left-hand side of the variable.
And adding one to lives equals plus.
So outside of the double quotes, we'll put our plus,
space ++playerLives.
You can get into difficulty with Java
if you try and not put a space between the plus
and the other plus.
It's gonna get itself confused,
so make sure you've got a space there
'cause we know that we're actually adding
the output playerLives,
but we're also using the increment operator as well.
All right, so, and we know the output for that.
Because we're using the prefix version,
the output should be three.
All right, so let's run this now and confirm that it works.
So looking at the code on line 25,
Lives remaining equals one,
we expected it to show as one,
and that's because the value was calculated postfix
after the output,
so playersLives is actually set to the value two.
It just wasn't output and was still showed as the value one,
and we saw that on line 25,
and on line 26, Lives is now two is correct
because we know that the value playerLives
was actually updated on the previous line,
and thirdly, because we're using the prefix version
of playerLives increment,
in other words, the ++ to the left-hand side
of the variable,
we know that straightaway, we gain the value of three,
and obviously, I've added a plus there
where I shouldn't have added
which makes things a little bit confusing,
so let me just run that again,
and we're now seeing correctly
that adding one to lives equals three
because we're using the prefix.
So that's pre and post increment and decrement now,
so hopefully, that makes sense as to what I've done there.
So how does this relate to expression statements?
Well, these increment and decrement operators
that we've used here,
we've used them as expressions,
but what we can do is we can do the same thing
as what we've done there
but put them on a line by themselves
to make them an expression statement,
and we saw that briefly earlier in the course.
So what I can do is change the code.
So let's go ahead and do that.
At the moment, we know that,
actually, I'll just take a copy of this line again,
save a bit of typing.
So we know the playerScore is currently
set to three at this point.
We saw that in the output window.
So we can type
Lives remaining equals
plus playerLives.
This is gonna be the long version,
and what I'll do is I'll,
we'll do playersLives++.
We'll output it.
Then we'll do a playerLives--,
and we'll output it.
So the output for this one,
the output should be four,
and output should be three
'cause you can see, we're using the increment operator
on line 30 and the decrement on line 32,
and we're using, and that's using the postfix version,
the ones on the right-hand side,
and if we do the same thing,
I'm gonna take a copy of that
whole entire block of four lines.
Copy and paste,
and this time, I'm going to change that.
Instead of --, instead of ++ on the right-hand side,
we're gonna put it on the left,
so we're using the prefix version,
and the same for the decrement,
and we'll run this,
and we should see that we get the same results.
Lives equals four, lives equals three,
lives equals four, lives remaining equals three,
and that's as we would expect
from the code on lines 30 through 37.
So again, this is now an expression statement,
the code on lines 30, 32, 34, and 36,
because we've added a semicolon to the end
of what was an expression.
So we've got two examples of it being used as an expression
on line 25 and also on line 27.
So again, semicolon to the end of that
makes it an expression statement.
All right, so what about the two other expression groups
that can become expression statements?
So going back to the slide,
method calls and object creation expressions,
we're going to discuss later in the course
because we need to cover other material first
for that to make sense.
So we'll leave that for now,
and that brings us on
to the third type of expression statement,
and that's the control flow statement.
Now as a recap, control flow statements are as follows.
So based on what we've learnt so far,
we can really only talk about
the if-then control flow statement.
The rest we'll introduce and discuss later in detail,
so let's swing over now to IntelliJ
and have another quick look at that.
So we're going to type a quick example
of a control flow statement
that we've covered so far in the course.
Now we have explored the if-then in detail,
so I'm just going go through this quickly at this point.
We still have the else component of the if-then-else
to discuss in detail as well as
those other control flow statements.
Again, we'll be doing that a little bit later,
but in terms of the code,
let's come down here,
and we're going to type if, parentheses,
calculatedScore, if that's greater than 59,
have that in parentheses,
then we'll introduce a code block
with a left and right curly braces,
and we're going to do a printout,
and we'll put I calculate your score
as being more than 59!
And looking up at our code,
calculatedScore, if you recall,
on line 10, we've defined it,
and that was equal to the value
of the expression finalScore equals 60,
so clearly, that should be true in the scenario on line 39.
So if we run this,
we get the output,
I calculate your score as being more than 59!
All right, so that's a control flow statement,
the code on line 39, so why?
Well, because depending on the expression in parentheses,
we're altering the standard flow of our programme,
which is done usually from top to bottom.
So with the rest of this code,
going to be going right up to where we first started typing,
back on line seven,
each line was executed in order
until we get down to line 39 with a control flow statement.
Now this code will only be executed, the code on line 40,
if the expression calculatedScore is greater than 59.
So that's a control flow statement,
enabling us to alter the flow of a typical programme
which is usually top to bottom.
All right, so that's the control flow statement.
Let's end the video here, and in the next one,
we're going to discuss what's space and indentation.
See you in the next video.