In this video, we're going to continue
our Kotlin tutorials and look at how to
get our code to make decisions. Now we'll
finish up on some variables and then
we'll move on to the basics of classes,
and the importance of understanding
those. So let's get into that. Alright, so
here we are where we left off at the end
of the previous tutorial. Now if you
haven't done so already, start up Android
Studio, then use the option to open an
existing Android Studio project, to open
up the Kotlin tutorial project that we
actually created quite a few videos ago.
Now what I'm going to do first now, is
delete everything in the main function,
so that we can type in some new code. Now
the opening and closing curly braces
must stay, but delete everything between
them. So I'm gonna show you what I mean.
Firstly, I'm going to close down the run
window by clicking on it down here,
bottom left-hand corner. I'm going to
select everything, essentially, from line
two right down here to line 30. Press the del
button. So you need to have that line 1
stay as it is, with the left
curly brace on the end, then the closing
right curly brace on line 3, as well. So
I'm going to talk briefly about
conditional processing. Now the best way
to show you that is to give you an
example. Now this is something that
you'll be using quite extensively in the course.
In fact, the ability of the computer to
branch to different bits of code,
depending on some condition or other, is
one of the things that make programs so
powerful. So let's see an example of that -
something you may use in a game to check
if the player can continue playing, or if
they've used up all their lives. So let's
start by declaring an integer, lives, and
give it the value 3. So we're going to
start with val lives equals 3. Now
remember that we don't have to specify
that lives as an Int. Kotlin can infer
that because we're giving lives the
value 3. Now what if you wanted the
computer to check how many lives the
player has left?
If the lives left is zero, print out a
message to say Game Over, otherwise,
print out a message to say You aren't
dead yet. So how do we go about that? Well
we'll start on the next line. Well
actually, we'll start on line 4, and we'll
check to see if the lives is less than
1. So to do that, we type if parenthesis
lives less than 1 - so the less than sign
and the number one -
right parentheses. Then we went our left
curly brace, which will automatically add
the right curly brace on the next line -
that's opening a code block - and in there
we're going to type println
parentheses Game Over
exclamation mark. And we've got the double
quote and the right parenthesis. So
we've used the if keyword, and that's
followed by a condition to test. So in
this case, the test is to check for lives
being less than 1. Now we could also
check for lives equals 0 there, but in a
fantasy world, the player could have
fallen foul of a really powerful wizard
and lost several lives in one go. So
we're making sure that the game's over,
if the number of lives goes negative as
well. So if I run this now, you can see we
get no output - it doesn't print anything.
So the code that we've put inside the if
block isn't being executed, which is
great because the number of lives is 3,
and the condition here, lives less than
1, isn't true. So all this code between
the opening curly brace and the closing
curly brace, forms a single block of code,
and anything we put in there will only
be executed if the condition's true. Now
we can also tell the computer what to do
if the condition's false, by adding an
else clause. So let's go ahead and do
that. So we're going to type this to the
right of the closing, or the right curly
brace; else. Then add another left curly
brace which has a code block, and in
there I'm going to type in println
parenthesis double quotes You're still
alive exclamation mark, and we've got
a double quote and right parenthesis. So
now when I run the code, this time we've
got a message printing out, You're still
alive, which has come from the else block.
So that's pretty powerful - we can test a
condition, and perform one set of actions
if the condition's true, and a
different set if it's false. Now if we
change our lives, up here on line two, to
zero and run it again, you can see that , 111
we've got Game over appearing - so that's
working fine. So if lives is less than 1,
this bit of code runs - the code that's in
between the left and right curly braces.
If lives isn't less than one - so if it's
one or above - then it goes to this code
in the else block, and runs that instead.
Now without this ability, programs would
be pretty basic and could just follow a
sequence of steps, but by checking
conditions, we can allow the computer to
make decisions and respond in different ways.
Now conditions like lives less than 1 -
this one here - that evaluates to a type
of value that's called a Boolean. So we
can actually store the result of that
condition in a variable, and use the
variable in the condition. So let's go
ahead and make some changes to that. So
I'm going to leave var lives equals 0,
then on, or below that, we're going to
put var isGameOver is equal to
parentheses lives less than 1, and
closing parentheses. And then we're going
to put if - instead of lives less than 1
we're going to put if - isGameOver, and leave
everything else as is. So here the
condition is evaluated, and the result is
assigned to the variable here, isGame
Over. Now in the if statement,
you saw me change the hard-coded
condition to use the value of the
variable, or actually put the variable
name in there so it will use the value
that's stored in the variable. So
different bits of code will run
depending on whether it's true or false.
So if we run it again now, it correctly
says Game over and that's because the
lives is set to 0. If I set lives to 3,
back here on line 2, and run it again, we
correctly get the message You're still alive.
Now you don't have to store the result
of a conditional expression in a
variable, but it can be useful if you
want to reuse the result, a little bit
later in the code. We can also print the
value of Boolean variables as well. So if
we do something like this on the next
line; we can type println parenthesis isGame
Over to print that out. We run that - you can see
it's printed out the value false,
and if I change lives to zero, we run it,
it now prints the value of true. So
that's the Boolean type, and it's very
useful for storing the state of things
while a program is running. Alright,
so back to our condition, and the syntax
of if statements and expressions is, they
start with the word if. That's followed
by condition in parentheses, so we've got
the if here, then a condition in
parentheses. Now a condition's an
expression that evaluates to true or
false, so it must be a Boolean expression.
So next we've got the block of code to
execute if the condition's true - that's
the code that's in the left and right
curly braces here, in this case, print
ln Game over. That's all we need,
but we can also include some code to
execute if the condition evaluates to
false. We can do that by adding the else
keyword here and adding another code
block, and then this code in here will be
executed if the condition's false. So
that's a basic if statement, but I've
thrown a few jargon words in there so I
think I'd better explain them. Firstly, a
block of code, or a code block, is
everything between the opening and
closing curly braces. Now a block of code
can contain as many lines of code as you
want, and can also contain more blocks.
Now our main function defines a block -
this is the code starting on line 1 -
which is everything from this first left
curly brace, right down to the last one.
So basically, right down there. So
everything in between those curly braces
is part of the main function. Now inside
that, we've actually got two more blocks.
Each one has only got a single line of
code but they're still code blocks - and
this is the code, of course, this first code
block here is if the condition is
evaluated as true. And the last code
block is if the condition is evaluated
as false. Now a statement - a statement's
sort of the equivalent of a sentence in
English. So on line two, we've got an
assignment statement. On the left, is a
variable declaration, val lives, and on
the right, we've got an expression. Now
this is a simple expression - just a 0
that evaluates to zero.... not surprisingly! Now
on line four, we've got a more complex
expression - lives
less than 1. Now that can evaluate to
true or false, depending on the value of
lives. So statement 4 is also a statement
that contains an expression. Now
statements don't have to include an
expression, and in fact, the next line - line
five println is Game over - is a
statement that just calls the println
function. And then on line 7
through 11, that forms our if statement.
So the whole thing - the if and the else -
all form a single statement. So in this
if statement, we've got other statements -
two calls to println - and this makes the
statement more like a paragraph rather
than a sentence. Ok, so let's look at some
more conditions because, at the
moment, we've only seen how to check if
something's less than something else.
Now I'm going to comment out all this
code so that it's available, if you want
to download my code from the resources
section of this video. So I'm going to
select all of that, right down to the
closing right curly brace but not the
very last one, making sure that the first
line also isn't commented out. And I'm
going to be my command slash, or control
slash, if you're on a Windows or Linux
machine. So I've commented out that code.
Now I'm going to also add a line of code that I
won't explain just yet, but don't worry
about it - it's just a way to get input
from the computer's console. So I'm gonna
start by typing println parentheses
How old are you, How old are you : and a
space, and close off the line with the
double quote and the right
parenthesis. Then I'm going to type val
age equals readLine parenthesis, and two
exclamation marks dot toInt then
parenthesis. Then we're going to type print
ln parenthesis double quote age is and
dollar age. So we print a message telling
the user what to do. The next line takes
input from the keyboard and converts it to
an Int - to a number, in other words. Now
there's a lot wrong with this but let's
run it first to make sure it works.
Alright, so there's our prompt and we can
type in a number. So I'm going to type in
a number like 21, and by the way, make
sure that you click into the run window
first, otherwise you'll be typing the number
into the code editor. So note how I clicked
there first before I started typing.
OK, so I'm entering the value 21, and
pressing enter. We can see age is 21 is
in output, so we're getting it printed
out okay. Now as I said, there's a lot
wrong with this. There's no error
checking, for example, so if I run the
program again, and this time type in
hello instead of a number, then the
app, the application or the program
actually crashes. And that's because
hello can't be converted to a valid
number, and it crashes with a number
format exception. Now we have to learn to
walk before we can run, so this bit of
code will let us test our program with
different numbers. So make sure you only
type in valid numbers when we run the
program. Alright, so let's see how to
test different conditions in Kotlin. So I'm
going to add some more code below the
println. So I'm going to start with
a val message : String. I'm going to
put if parenthesis age is less than 18,
and open a code block with the left
curly brace, which'll automatically add the
right one as well. Then we're gonna type,
in that code block, message equals You're
too young to vote. So that's
straightforward, and we've seen a
condition a lot similar to that,
earlier. The code's assigning the text
to the variable message, if the number
that was entered in the keyboard - on the
keyboard - was less than 18. We can also
test for age being equal to some
value as well. So a single equals symbol
was used to assign a value to a variable,
so we have to use two symbols - two equal
symbols - to test for equality. So I can do
something along these lines; else if
then parentheses age equals equals, so
two equal signs, 100, open a code
block. Then we're gonna type message
equals Congratulations. Now we can't get
to see our message yet, because Kotlin
won't compile the app if we try to print
message. And I'm just going to add the
code to print it, though anyway, then I'll
show you what I mean. So type on this line
now, println
message in parentheses, and we're getting
this error message here and if we have a
look at, see what that error message is;
Variable message must be initialized. So
we didn't assign a value to message when
we declared it on line 17, and there's a
path through the program that could
result in it not getting a value. So if
the age is less than 18, everything's
fine, and if the age is exactly 100, then
message also gets a value. But for any
other age, message will be undefined and
Kotlin doesn't allow that. So in other
words, we've accounted for people who are
under 18, and we've accounted for anyone
who's a hundred. But what about
everyone else? We can do that with a
final else statement, so we can come up
here to the end of the code block, and
do and else, open another code block,
message equals You can vote. And notice
that the error message disappears, and
that's because we've accounted for every
one, so message will always have a
value when we can run the program. Now, I'm
going to run that now, but if you do
happen to get the error message Could
not find or a load class Mainkt,
remember to come up to here to Build and
click on Rebuild Project. I'll just do
that now anyway, even though I haven't
tried to run it. And I'll try running it
now. We're getting the prompt there. I'm
clicking into the run window, and I can
enter a value, 25; age is 25, You can vote.
Now we can add as many conditions as we
need, using else followed by another if,
as you've seen me do on line 20 over
here. That's a more complex if statement
than we've had before, and it shows how
to check for equality using the two
equal signs. But, now before we look at
the other conditions we can test, let's
see what an if expression looks like. Now
we can use if as a statement, like we've
got here, or as an expression. Now if it's
an expression, we can assign it to a
variable. And in fact, Android Studio is
offering to do that for us.
So if we look at the and the if
statement there. So notice that if, here,
is underlined, so we click on that. We can
see that the tooltip's saying Assignment
can be lifted out of if, and the light
bulb will actually give us an
to do that. I'm gonna click it, over here.
We're going to choose the option here to
Lift assignment out of if. Now the rest
of evaluating the if expression is
assigned to message, as you can see there.
So the result, by the way, is the last
line in each block. If a block contains
several lines of code, which is often the
case, then the last line is the value
that's assigned. So that's a more
Kotlin-like way of writing this if, and
you'll often see this form in the code
that we'll be writing.
Now I say it's more Kotlin-like but
that really only
applies when there are two conditions,
and here we've got three; under 18,
equal to 100 and everything else. So once
you get to three or more conditions, it's
so more usual to use a when statement, or
when expression. Now we're going to be
looking at them later, but I mentioned it
now because Android Studio is suggesting
that we use one. And you can see over
here, if I come over here and hover over
that; Cascade if can be replaced with
when. But again, we'll be talking more about
when statements or when expressions later
on. But so that you've got this code
available in the resources, I'm going to
duplicate the if expression, and then
comment out the duplicate. Now Android
Studio makes it easy. You can just select all
the lines you want to duplicate, then use
Ctrl- D, or Command-D on a Mac, to
duplicate the code. So I'll show you what I
mean. I'm just going to select the code
for message, up to and including the last
brace, and I'm going to do, on my Mac, a
Command-D. Notice how it's duplicated the
code, and then I'm going to do a Command-
/ to comment out that code. And I'll just
go down to the next line and comment that
out. Okay. Now if we come over here now, select
the if and then click on the light bulb,
we can replace if with when, and that's
even more readable. So each branch now
starts with a condition, then the code to
execute comes after the branch arrow. Now
if none of the conditions match, the
else branch is used. Now using when often
results in more readable code, especially
when there are a lot of conditions that
may apply. And you're gonna be seeing
lots of examples of when, so I'm going
stop this video here. But in the next one,
we're gonna start looking at classes in
Kotlin. I'll see you in the next video.