All right I want to talk about a naming convention relating to boolean variables.
Now I mentioned this in an earlier video but when a boolean variable is declared good naming convention
is to stop the variable naming which is another rule of thumb is to have the portion of the boolean
variable name following is state how the variable will be used.
Also choose the name to be positive.
So consider this boolean is valid equals true.
Well that's preferred over boolean is invalid equals false.
Now the reason for this is you want to strive to make your code as readable as possible so continuing
with is valid versus is invalid.
If you saw the code you're reviewing not is invalid and that's the use of the exclamation which is then
not operator.
Well that requires the reader to stop and decipher the true intention Where is valid is a lot clearer.
All right.
So back to the code.
Now I'm gonna change the initial value of score on line six to be 800 to do that first and then on line
8.
What we're going to do.
I'm going to add three new variables so let's go ahead and do that.
Start with Berlin is came either is equal to true mixed line int level completed is equal to 5 semicolon
2 in the line and finally int bonus is equal to 100 semicolon like so what I'm going to do is comment
out this code on line twelve through 18 so let's go ahead and do that I'm gonna hold down shift to select
roll and on a Windows machine control forward slash will come into that and it's command slash on a
Mac to do the same right.
So what we want to do now and I'll put this below this code of committed out for now just basically
starting on line 19 if you want to do a test here.
So I'm gonna put if parentheses is game over is equal to true.
So we're using to where equal signs for the logical equal.
I'm gonna open a code block we're going to top and I'll just make this up a little bit so we can see
case you've got subtitles on it.
Final score and trouble talking today equals score plus and in parentheses level completed multiplied
by bonus then on the next line we're going to output the system the laptop print line and then double
quotes in parentheses there your final score was a space after before the double quote plus final score.
So I want to present your final score was followed by the calculated final score that we defined on
line 20.
So let's actually run that
we can say that the final score is thirteen hundred was output.
And that's because the final score equals the score which was if you scroll up and have a look it's
just off screen at the moment.
Final score was eight hundred plus level completed which is five there in terms our bonus which is one
hundred thirty five times one hundred is five hundred.
Therefore it's eight hundred plus five hundred and the final score is showing down the output pane is
thirteen hundred.
So I can say this code block but being back on screen again with the code in there was executed because
the variable is game over is a course set to true and we did that on line 8.
As you can see but notice how we created a variable in final score in the code block and the thing to
remember about code blocks is as you can see here they're able to access variables that were created
outside of the code block so you can see that we're accessing score here the variable score on line
20.
And that was defined up in the code there.
On line 6.
So basically we're using a number of variables there.
We've got score level completed and bonus all variables that we've created outside of the code block
but final score is different in that it was created inside our code block.
Now the reason bringing that up is because there's something we can't do we can't come down here at
sort of the code block going to top some code here and try and top it saved.
Final score is equal to final score.
Semicolon and nice I've got an error there.
However it cannot resolve symbol final score.
Now the reason for that is because once execution within a code block is finished any variable is created
inside that code.
Look at deleted but the reverse isn't true.
So while you can access variables created outside of the code block from inside the code block and you
saw that we were doing that.
What you can't do is access variables created in the code block from outside of the code block.
As you can see in this scenario we're trying to do on line 23 right so let's go ahead and remove line
23 then I think I talked about this briefly before but there is a shortcut to using the two equal signs.
The logical operator to determine for equality if you're using a billion variable instead of if it's
game over is equally true.
We can actually just remove the equal equal true so we can go ahead and delete that
and make it just is game over.
So if parentheses is game over is a shortcut and essentially executor same as typing if parentheses
is game either equally true.
So keep that in mind as it's a good way to abbreviate your code.
You'll see commonly used in Java code is just a shortcut to test to see whether a boolean variable in
this case is game over has the value true.
Now if you wanted to test for false in other words is game over equally false.
The shortcut to that is just to use an exclamation mark at the start of the variable.
So come up here we put an exclamation mark which is not so.
If not is game over is testing to see whether is game over is equal to False let's let's have a brief
discussion about scope so scope refers to when a variable can be accessed.
We actually scope is another subject which requires a video in its own right and we'll be getting to
that later in the course.
But for now that keep in mind that job connects has variables from outer code blocks which are still
executing.
Java can not access variables inside code blocks which have finished executing and Java cannot access
variables inside code blocks which have not started executing so scope really deals with accessibility
of variables in certain situations or in certain places in the code for example in a code block.
The bottom line for now is it in a code block.
If you create a variable you can't access that variable you've created outside of that code block and
we saw an error earlier in the video when we tried to do that but of course you can.
As you saw still access variables that were created previously outside of the code block and prior to
going into the code block whilst you're in the code block itself and again you saw us doing that in
fact the code still on screen there are lines 20 and 21.
We're using score level completed and bonus 3 variables we created outside of the code block.
All right with that said well in the video here and what we're going to do in the next video.
I've got a challenge for you so I'll see you in the next video.