All right.
So for those you guys who are unfamiliar with Dart, in this lesson, I want to quickly cover Dart conditionals.
And more specifically, the IF and ELSE. Now an IF statement basically just checks to see if a condition
is true. And if the condition is true, then it's going to carry out the instructions inside a set of curly
braces.
So in this case, our IF statement says that if the track is clear, the double equals means assess to see
if the left hand side is equal to the right hand side,
but a single equals is an assignment.
So it means make the left hand side equal the right hand side,
and that's a subtle difference to be aware of when you're coding.
So in this case we're saying in English it means if the track is clear, then go.
If we go through the statement, then this is where the condition is,
and this is what the instruction is.
So when the computer evaluates the statement and in this case the track is indeed clear, then it will
just go.
But if when we evaluate this it's actually not true,
the track is not clear, in this case there's a giant rock there,
then it won't carry out the instructions inside the curly braces.
Instead it will skip all of this and go onto the next line of code
that it needs to execute.
So in this case, the train goes nowhere. In the last lesson,
we saw that in addition to just using a simple IF to trigger some statement when the conditions are met,
we could also use IF and ELSE.
So let's say that we're saying if the track is clear, then the train should go straight but else i.e.
if the track is not clear, then it should turn right instead.
In this case this is what the code would look like. the condition that we're testing for is if the track
is clear.
And in that case, it should carry out this piece of instruction,
everything in between this set of curly braces. So go straight.
But if this condition was not true then it skips ahead to the else statement and it carries out what's
inside this set of curly braces instead, which happens to be turn right. Now more commonly, you'll see IF
and ELSE statements structured more like this, where we have our condition and then we have a set of curly
braces and then we have all else
and what to do if that condition is false. And this is what we did in our last lesson as well.
Now if you live with a programmer then it's really dangerous and you have to be very careful how you word
things.
For example if you told your husband or wife who's a programmer, 'Please go to the supermarket and buy
one bottle of milk, if they have eggs buy 6.'
You might think what you're saying is buy one bottle of milk and if there's eggs by six eggs.
But what they might hear is if haveEggs = true, then buy 6 bottles of milk,
otherwise buy one bottle of milk.
So a shout out to everybody who lives with programmers.
Now in order to demonstrate how this code actually works, we're going to build a love calculator.
So it's one of those throwbacks from the 80s where you test your compatibility with whoever you fancy.
Now we're going to keep our love calculator really simple.
We're going to make a function that's called loveCalculator.
And it's simply just going to generate a random number between 1 and 100.
So we know how to do that already
from our previous lessons. We have to first import the Dart math library.
Remember it's math, and not maths.
And once we've imported it, then we can use the random number generator. So we can create a new variable
called loveScore and it's going to be equal to the random number generator .nextInt because we're
generating whole numbers.
And we're going to make it 100.
So they generate all the numbers from 0 to 99. And then we add 1 to change our range to
1 to 100.
So now that we can generate our random number loveScore, and I apologize to everybody who really believes
in love calculators.
But in this case it's going to be a little bit random.
So now that we've generated loveScore, let's print it out and let's see what we get.
So if we call our function up here in the main function and we call our love calculator, and then we
run our code, we should see the result in the console.
So the first time we got 29%, we got 18%, keeps on getting lower, 22%.
So today's not a day for love it seems it's, oh! There we go.
We got a 96%. We finally got a high score. So you can see there are a whole bunch of random numbers between
1 and 100.
Now if we wanted to give the user an interpretation of their love score based on this number, then
we could use an IF statement. So we could say that if the love score is greater then let's say 70, then
that means that they, you know, maybe they really like each other. Maybe they... you love each other like
Kanye loves Kanye.
And if they got a score that's lower than 70, then it will just say
you'd like each other.
All right.
So now, depending on our love score, we should get something different printed in our console.
And if we run, it we can see that the first time we got 30, so we get the text we like each...
you like each other.
Not sure how I missed so many letters.
And if we run it again, we get 9,
so you like each other. And eventually hopefully we should get... oh wow!
We actually got 100.
Jackpot!
So you love each other like Kanye loves Kanye. So you can see that depending on the love score that
we got out of the random generator, we're able to print different text into the console.
And this is all because of our IF statement.
Now what if we wanted to have more than just one bracket because at the moment we can only have a different
piece of interpretation for above 70 or below 70?
What if we wanted it to be more nuance?
What if we wanted 70 to 100 and 50 to 70 and 0 to 50?
How would we do that?
Well when we're using IF statements,
we also have a whole bunch of operators that we can use.
For example we already saw that we can use to double equals to check if the left hand side is equal to
the right hand side.
But we can also check if it's not equal to by using an exclamation mark, an equal sign. Or if the left
hand side is greater than the right hand side, lesser than, greater or equal to, or lesser or equal to.
And then we can combine these conditions together, in an IF statement, using these comparatives.
So for example we can have two && for AND, and two || for OR, and a single exclamation mark
for NOT. So let me show you how this works. If we have are IF statement for if loveScore is greater than
70, then do this. We can also have another IF statement down here. Let's say in this one we want to check
if the love score is greater than say 50 and the love score is less than 70, then we print 'You like each
other'. But if the love score is less than 50 then we print you don't like each other. All right.
So now we get an error over here that says too many positional arguments.
One expected, but it found five.
What is this all about?
Well remember what we said about the single quotes.
It thinks that this is what we want to print and it doesn't know why there's all of this code at the
end.
So whenever we're using single quotes inside our strings, we always have to use the backslash to escape
it.
Tell it, 'Don't look at this as code.
This is just something that we use in English grammar and not a part of the code.'
Now let's run our code and you can see that if we get 71, which is greater than 70, then we get
you love each other like Kanye loves Kanye.
But if we get a different number, say 8 which is less than 50, then it tells us you don't like each
other.
So now, we're able to use these operators and combine different conditions to check for multiple things.
So we can check if love score is greater than 50,
and the love score is less than 70. Or we could check if the love score is greater than 50 or the love
score is less than 70. Although it won't be very helpful in our case because that means this is going
to be true when this is also going to be true.
So when it's 22, because it's less than 70 and this is an OR, then this will trigger and 22 is also less
than 50.
So this will also trigger.
Now while this works, while we can use these ways of combining different things to make our statements
more specific, there's a much better way of doing this.
We can start to build up a ladder of IF and ELSE statements.
So this is how it would work.
We can check to see if track one, this one, is clear.
Then we're going to get our train to go on to track one.
This is our first choice. But if that was false,
so if there was a rock there, then we can go to the next statement. And we're saying that ELSEIF track
2 is clear, then we should go down track2. But what if track2 also has a rock on it?
I don't know who's maintaining these railways by the way why there are rocks everywhere.
If track2 is also blocked then we can have another
Else If statement to check if track3 is clear, then that's going to be the one we'll take.
Now our code looks a bit like this.
So we can say if the love score is greater than 70, then do A.
But if that's not true, then we get on to our second choice.
So else if love scores greater than 30, then do B.
And finally if that's also false, then do C.
So if we get a Love Score of 50, then it's obviously not greater than 70,
so it skips A. But then it checks the next statement.
It checks to see if the love score is greater than 30, which it is.
So then it will go ahead and do B.
However if the love score was 75, then the first statement is already true.
So in this case it will simply go and do A.
And notice that if this was not an ELSE IF, but it was just an if, then this would also be true,
right?
75 is also greater than 30.
But this doesn't happen because this is an ELSE IF. So when we have an IF statement followed by an ELSE IF
followed by other ELSE IFs or ELSE, it means that once it finds one of the conditions to be true then
it will skip all of the other ones.
But if all of these were IF statements then it will check them one by one.
So if we had if love score was greater than 70 and if love score was greater than 30, then it will go
and evaluate the first condition.
So it's greater than 70.
It'll do A.
And then it sees that oh this is the end of the IF statement, and we've got another IF statement.
Well this one's also true.
75 is also greater than 30.
So I'm gonna go and do B instead.
Now there's cases where you might want your code to work like this but there's also cases where you
might want to use an ELSE IF instead.
So heading back to our code, we can actually change it so we don't even need to compare and have more
than one condition. Instead of checking to see if the love score is greater than 50 and less than 70,
I can change this to and ELSE IF instead. And I can simply write
else if the love score is greater than 50. and the final one doesn't even need an if, it could simply just
be an else.
So now what happens is that when I click run and we generate another new random number and we get a
score of 35, the first thing that happens is it checks to see if the love score is greater than
70. 35 is not greater than 70, so it skips this and goes to the next one.
Now it checks to see if 35 is greater than 50. which is also not true.
So it skips this block.
And finally because this is an else block, when everything above is not true, then it will simply carry
out the instruction inside here, which is to print 'You don't like each other.'
Now if we try again and this time we get 94,
then once it checks the first IF statement, it's already true and it carries out this function, then it
skips all of the other ones.
It doesn't also go and check to see if it's greater than 50.
But say if we had changed this to another if, then if I click to run and we get a large number like seventy
five, then this is true,
so it does this. But this is also true,
so it does that.
So this is the difference between an IF and ELSE IF. And you can actually build up your ELSE IF Statements
to have lots of them.
So you can have another one for example, if love score is greater than, I don't know, 30. Maybe you want a different
message for 30 to 50
in this case. And you have a different message for 50 to 70,
and here you have a different message for 70 to 100.
So have a play around with this code to fully appreciate the difference between a whole bunch of IF
statements and also a ladder of ELSE IF statements and how these types of conditional statements work
in Dart. Once you're done, we're going to head back to making our Quizzler app.
So I'll see you on the next lesson.