In this video we start with a recap of code blocks and a recap of what we've done with if then we further
our understanding of conditional logic when we add an else component to the if then statement or code
block recap.
So code block allows more than one statement to be executed.
In other words a block of code you can use a code block wherever a single statement is allowed.
The format of a code block is an open curly brace the left hand curly brace followed by one or more
statements followed by closing right.
Curly brace.
There are places in Java where code blocks are mandatory as we saw when declaring the main method.
Likewise there are places where code blocks are optional within an if then statement you have a code
block.
Although optional should always be used as follows.
So we've got.
If there is an expression in parentheses and then a code block and with your actual code join executed
in between the Left the Right curly braces in between the code block right.
So what I want to do now is just recap the use of the if then statement to recall the various ways that
you can use it.
What we can to do is create some coded of go through it as you can see I've created a new project and
of called it if keyword and code blocks match let's go ahead and type some code in the main method.
OK so I'm going to start by talking int score is equal to 5000.
Then if on line 8 parentheses score is equal to five thousand.
Obviously the two equal signs that I'm on the next I'm going to type system to that top printed in and
in parentheses and double quotes.
You've got the high school code to the end of the line and then on the line after that I'm gonna do
our system without top printing in parentheses and double quotes.
This was executed alright so I can say that I have declared a variable scores in it with an initial
value of 5000 and used a generic form of ethane to print.
You got the high score.
If the test score equals 5000 passes no notice on line 6 for use.
The assignment operator equals to assign the value of 5000 to score on line 8.
However we use the logical operator that's the two equal signs to test.
If the score is equal to 5000 throughout the lesson from that
so since the test score equal to 4000 passes the output contains the line.
You got the high score followed by the line.
This was executed.
Now if we do not use a code block you can only get one statement to be executed after the test.
So let's test that by changing the test on line 8 to check if scores equal to 4000.
So I'll go ahead and do that.
Times the 4000 or 4000 will run that again.
This time the output only contains this was executed.
Now if we add a code block after the test and include both system for that to print land you got the
high score and the following system that out print land.
This was executed.
We'll get a different result so let's do that.
I gotta put that in parentheses and put another parentheses at the end of the line and utilize our format
that for us we run the code again.
This time as you can see in the run parent down the bottom neither line has appeared in the airport.
Now making another change if I go back to last night and tests I will change the tests to score equals
4000.
Again it was 4000 run that
we've now got the output you got the high score and this was executed.
So as you can see a code block can be used to execute more than one statement.
This is especially useful when you need to execute multiple statements which Java calls for a single
statement.
My personal recommendation is to use a code block even if you've only got the one line.
And that's because I believe it actually makes the code clearer and easier to understand.
Using a code block with a single line also helps prevent future bugs in your code since it makes it
very clear if additional lines of new code are all part and parcel of the same if keyword statement
or write something I now want to show you another part of the statement.
So let's start by rearranging the code just a bit and adding an x component to our if statement then
stop by on line 8.
Changing the equals a logical operator to less than so it scores less than 5000.
I'm going to change the text here which is you got the high school.
We're going to change that output to your score was less than 5000
then what I'm gonna do is come down to the end of that line put a rock Kelly Price to close up the if
statement at an LS then let Kelly Price and you can see that we've now added the x component of the
if statement and I'll change this was executed to by some other text got here but I can be literally
anything we want.
So by adding the US component we can handle the case with score is greater than or equal to 5000 and
in that scenario we can displace the text got here in its place.
Let's just run up to see what happens here.
And you could see the code printed got here.
Now the reason that we've got that output and we didn't see the other airport is that the first if test
was checked to see save scores less than 5000.
In our case we've assigned score to be equal to 5000 a line six so that is false and therefore your
scores less than 4000 was an output.
Basically since SQL was set to 5000 the code block contains the line system the letter printed in your
score was less than 5000 wasn't executed.
So what happens then is the computer goes to the next part here which is the else statement and because
we've got no other condition there it says okay.
Note that all other circumstances.
I'm going to just print got here and that's why it printed got here.
Now if we change line 8 to less than or equal to 5000 we'll get a different result.
So change less than to less than equal and will change the text as well.
You score was less than or equal to
so if you run that
you can see that we've now got the output.
Your score was less than or equal to 4000.
And now the system the got here.
Text basically the code on line eleven wasn't executed and that's because the first condition was fulfilled
in other words was true because score is less than or equal to 5000 and the else part wasn't executed.
So you can say that the if is used to from that scenario but with that said we can expand it even further.
What we can do is change the condition to use a logical expression in the else part.
So what I'm gonna do is change it so that testing of scores is greater than five thousand and scores
greater than 1000 by using two ampersand is the logical and then will output something in that scenario.
So I'm gonna go ahead and do that so I'm gonna put up here on line 8.
I'm gonna add another parentheses left parentheses so if then the first parentheses will be score less
than 4000 that's change it back to less than 4000 and add a right parentheses there.
There is a logical end and then another parentheses score is greater than Forbes greater than 1000.
They will get to closing parentheses that will change the output now.
So your score was less than 4000 but would change it.
Now it's but it's greater than 1000.
What I'm gonna do is come down here.
We're gonna add a right to Kelly Price and put it.
Else if school is less than equal to 1000 we're going to add Al code block left to right Kelly prices
and in there I'm going to put the airport score was less than or equal to 1000 then we got a final else
which we've left as is system that appropriately got here.
So I can say what I've done there.
Firstly on line I'd have put a test to check that the score was less than five thousand and it's greater
than 1000.
If that's the case we're gonna print out the output.
Your score was less than 5000 but greater than 1000.
Then the second component is the LC of scores less than equal to 1000.
And you can see the text there your scores less than or equal to 1000 which is correct I with that said
let's try and see what happens for different values of score on line six at the moment we've got in
score was part thousand.
Let's change that to be four thousand.
Now if we run that
score was less than five thousand but greater than 1000.
Let's go ahead to make another change and we'll change the score now to be eight hundred or on that
your score was less than or equal to 1000 is their output.
We'll try one more let's make score equal to six thousand and see what happens run that
and we've got got here being output.
And the reason we got that in this scenario is both the comparison tests have failed and that's a result
neither.
Your score was less than 5000 mark greater than 1000 was output.
Or your scores less than 1000.
And again that's because both of those expressions returned false.
Instead you've got got here which is the sort of catch all condition where the others have returned
false.
So let's end the video here in the next one we want to talk briefly about some naming conventions relating
to boolean variables.
See you in the next video.