Tim: All right, so it's time to talk about
Java statements in a bit more detail,
so I've created a new projected called Statements.
So let's start off by typing in a statement in Java.
So I'm going to start with int finalScore
is equal to 50.
So this is a statement in Java.
Now we know from previous videos
that finalScore equals 50 is an expression.
The statement is the entire line including the data type,
int in this example at the start,
and the semicolon at the end.
Now it turns out there's three types of statements in Java,
so this is the first type,
and it's known as the declaration statement.
So let us swing over and take a look
at the three types of statements in Java.
So statements can be compared to sentences
in natural languages.
A statement forms a complete unit of execution,
and as I mentioned, there's three types
of statements in Java.
Firstly, we've got a declaration statement.
So the declaration part
means the statement declares something,
a variable in this case,
and we've seen numerous examples so far in the course
of a declaration statement.
We just didn't know what it was called.
So int finalScore equals 50 semicolon,
well, the data type at the front and semicolon at the end
make it a declaration statement in Java,
and obviously, finalScore equals 50 was the expression
which then ultimately has converted
into a declaration statement.
So the second type of statement is an expression statement.
So as the name suggests,
expression statements have a lot to do with expressions.
Some but not all expressions
can become expression statements in Java
just by adding a semicolon to the end of the line.
Now there's four expression groups
that can become expression statements in Java,
and let's go through each one.
First one, assignment expressions,
and we know from earlier in the course
that assignment expressions just basically
assign a value to a variable.
Got three examples there,
assigning the value five to groupAge.
The second example, we're multiplying
the current value of salary by two,
and the third example, we're adding 10
to the current value of profitMade.
So they're assignment expressions,
but adding a semicolon to these expressions
convert them into expression statements.
The second type of expression or the expression groups
that can become an expression statement
is anytime you use the ++, the increment,
or the --, decrement, operator,
and an example there, numberStudents++
or surveyResults--.
So these can be used in an expression,
but adding a semicolon at the end of these
will convert them into expression statements.
Now the third and fourth groups of expressions
that can become expression statements are method calls,
we've got an example there of System.out.println,
parentheses and Testing within double quotes,
and also the last type, object creation expressions.
Now I'm putting c and d here for completeness,
but we haven't discussed them much at all,
but we will be doing that later in the course,
and for that reason, we can't really go
into much more detail about these two groups,
but just know there are two other expression groups
that can ultimately become expression statements.
All right, so that's the second type, expression statements.
The third type is control flow statements,
the third type of statements within Java.
So statements inside your source files in Java
are generally executed from top to bottom
in the order that they appear.
Control flow statements, however,
they break up the flow of execution
by employing decision making, looping, and branching,
enabling your programme to conditionally execute
particular blocks of code,
and we've already talked briefly about conditional logic,
and you've seen the if-then statement earlier in the course.
Now control flow statements also include the if-then-else,
looping statements,
which is the for, the while, and the do while,
and then branching statements, break, continue, and return,
and most of those, we haven't talked about yet,
but we will be covering all of these topics
in future videos.
All right, so let's swing back through to IntelliJ
and have a quick look
at these different types of statements.
So looking at line seven,
this is the first type of statement.
This is the declaration statement.
Nothing complicated here.
We're declaring a variable with an optional initial value,
which we've set to 50 in this case,
so that's a declaration statement.
There's another form of a declaration statement
where we can come along and we can type int
playersHighScore semicolon.
So this is another declaration statement,
but we're just not setting playersHighScore
to a specific initial value.
So declaration statements, I'm sure you'll agree,
are pretty straightforward.
Let's now have a look at the second type of statement,
the expression statement.
Now remember that there's four groups of expressions
that can become expression statements,
and let's go through them all.
Firstly, we'll look at assignment expressions.
So I'm gonna start by typing
finalScore is equal to 60.
Now as you know from earlier in the course,
this is an assignment expression.
In this case, it assigns the literal value 60
to our variable finalScore,
and we can make this a statement by adding a semicolon
to the end of the line,
and that's now an expression statement.
However, it was a expression before I did that,
and just to prove that it was an expression,
let's assign the value of this assignment expression,
finalScore equals 60,
to a new variable and then print it out.
I'm gonna come back to the start of the line.
I'm going to type int
calculatedScore is equal to,
and I've left the finalScore equals 60
to the right-hand side of the equals sign,
but clearly, finalScore equals 60 is an expression there,
and what we've done there is
we've created a declaration statement
for int calculatedScore equals finalScore equals 60.
Now looking at that line, you might that it looks a bit odd
'cause we've got two equals signs there,
but what we're doing is we're storing the value
of the expression finalScore equals 60
into the variable calculatedScore.
So the expression finalScore equals 60,
we know from earlier in the course
that that returns the value that,
the literal value in this case, 60,
and let's confirm that by printing calculatedScore
on the next line,
just to confirm that it does actually give
what you think it should give.
So I'm just going do an output
and type Calculated Score is equal to,
and I'm going to just do a plus calculatedScore.
Let's run this,
and we should see the result 60
output in the Output pane down the bottom.
All right, you can see that Calculated Score equals 60,
so clearly, looking at line 10,
finalScore equals 60 was an expression.
It returned the result 60
because that's what was assigned to the variable finalScore,
and we confirmed that that was an expression
by printing out the value,
and as I mentioned, on line 10,
by adding the data type and the variable at the start,
we've made that a declaration statement.
So just to confirm what the expression statement looks like,
we come down here to line 13,
and I can just type in an expression statement,
finalScore equals 70.
So again, that's now an expression statement
and not an expression because you've got the semicolon
on the end of the line.
All right, so let's end the video here.
We've still got some other expression groups
to go through for our expression statement,
so we'll continue working on that in the next video.