All language subtitles for 6. [Dart] Conditionals - IFELSE

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

0 All right. 1

So for those you guys who are unfamiliar with Dart, in this lesson, I want to quickly cover Dart conditionals. 2

And more specifically, the IF and ELSE. Now an IF statement basically just checks to see if a condition 3

is true. And if the condition is true, then it's going to carry out the instructions inside a set of curly 4

braces. 5

So in this case, our IF statement says that if the track is clear, the double equals means assess to see 6

if the left hand side is equal to the right hand side, 7

but a single equals is an assignment. 8

So it means make the left hand side equal the right hand side, 9

and that's a subtle difference to be aware of when you're coding. 10

So in this case we're saying in English it means if the track is clear, then go. 11

If we go through the statement, then this is where the condition is, 12

and this is what the instruction is. 13

So when the computer evaluates the statement and in this case the track is indeed clear, then it will 14

just go. 15

But if when we evaluate this it's actually not true, 16

the track is not clear, in this case there's a giant rock there, 17

then it won't carry out the instructions inside the curly braces. 18

Instead it will skip all of this and go onto the next line of code 19

that it needs to execute. 20

So in this case, the train goes nowhere. In the last lesson, 21

we saw that in addition to just using a simple IF to trigger some statement when the conditions are met, 22

we could also use IF and ELSE. 23

So let's say that we're saying if the track is clear, then the train should go straight but else i.e. 24

if the track is not clear, then it should turn right instead. 25

In this case this is what the code would look like. the condition that we're testing for is if the track 26

is clear. 27

And in that case, it should carry out this piece of instruction, 28

everything in between this set of curly braces. So go straight. 29

But if this condition was not true then it skips ahead to the else statement and it carries out what's 30

inside this set of curly braces instead, which happens to be turn right. Now more commonly, you'll see IF 31

and ELSE statements structured more like this, where we have our condition and then we have a set of curly 32

braces and then we have all else 33

and what to do if that condition is false. And this is what we did in our last lesson as well. 34

Now if you live with a programmer then it's really dangerous and you have to be very careful how you word 35

things. 36

For example if you told your husband or wife who's a programmer, 'Please go to the supermarket and buy 37

one bottle of milk, if they have eggs buy 6.' 38

You might think what you're saying is buy one bottle of milk and if there's eggs by six eggs. 39

But what they might hear is if haveEggs = true, then buy 6 bottles of milk, 40

otherwise buy one bottle of milk. 41

So a shout out to everybody who lives with programmers. 42

Now in order to demonstrate how this code actually works, we're going to build a love calculator. 43

So it's one of those throwbacks from the 80s where you test your compatibility with whoever you fancy. 44

Now we're going to keep our love calculator really simple. 45

We're going to make a function that's called loveCalculator. 46

And it's simply just going to generate a random number between 1 and 100. 47

So we know how to do that already 48

from our previous lessons. We have to first import the Dart math library. 49

Remember it's math, and not maths. 50

And once we've imported it, then we can use the random number generator. So we can create a new variable 51

called loveScore and it's going to be equal to the random number generator .nextInt because we're 52

generating whole numbers. 53

And we're going to make it 100. 54

So they generate all the numbers from 0 to 99. And then we add 1 to change our range to 55

1 to 100. 56

So now that we can generate our random number loveScore, and I apologize to everybody who really believes 57

in love calculators. 58

But in this case it's going to be a little bit random. 59

So now that we've generated loveScore, let's print it out and let's see what we get. 60

So if we call our function up here in the main function and we call our love calculator, and then we 61

run our code, we should see the result in the console. 62

So the first time we got 29%, we got 18%, keeps on getting lower, 22%. 63

So today's not a day for love it seems it's, oh! There we go. 64

We got a 96%. We finally got a high score. So you can see there are a whole bunch of random numbers between 65

1 and 100. 66

Now if we wanted to give the user an interpretation of their love score based on this number, then 67

we could use an IF statement. So we could say that if the love score is greater then let's say 70, then 68

that means that they, you know, maybe they really like each other. Maybe they... you love each other like 69

Kanye loves Kanye. 70

And if they got a score that's lower than 70, then it will just say 71

you'd like each other. 72

All right. 73

So now, depending on our love score, we should get something different printed in our console. 74

And if we run, it we can see that the first time we got 30, so we get the text we like each... 75

you like each other. 76

Not sure how I missed so many letters. 77

And if we run it again, we get 9, 78

so you like each other. And eventually hopefully we should get... oh wow! 79

We actually got 100. 80

Jackpot! 81

So you love each other like Kanye loves Kanye. So you can see that depending on the love score that 82

we got out of the random generator, we're able to print different text into the console. 83

And this is all because of our IF statement. 84

Now what if we wanted to have more than just one bracket because at the moment we can only have a different 85

piece of interpretation for above 70 or below 70? 86

What if we wanted it to be more nuance? 87

What if we wanted 70 to 100 and 50 to 70 and 0 to 50? 88

How would we do that? 89

Well when we're using IF statements, 90

we also have a whole bunch of operators that we can use. 91

For example we already saw that we can use to double equals to check if the left hand side is equal to 92

the right hand side. 93

But we can also check if it's not equal to by using an exclamation mark, an equal sign. Or if the left 94

hand side is greater than the right hand side, lesser than, greater or equal to, or lesser or equal to. 95

And then we can combine these conditions together, in an IF statement, using these comparatives. 96

So for example we can have two && for AND, and two || for OR, and a single exclamation mark 97

for NOT. So let me show you how this works. If we have are IF statement for if loveScore is greater than 98

70, then do this. We can also have another IF statement down here. Let's say in this one we want to check 99

if the love score is greater than say 50 and the love score is less than 70, then we print 'You like each 100

other'. But if the love score is less than 50 then we print you don't like each other. All right. 101

So now we get an error over here that says too many positional arguments. 102

One expected, but it found five. 103

What is this all about? 104

Well remember what we said about the single quotes. 105

It thinks that this is what we want to print and it doesn't know why there's all of this code at the 106

end. 107

So whenever we're using single quotes inside our strings, we always have to use the backslash to escape 108

it. 109

Tell it, 'Don't look at this as code. 110

This is just something that we use in English grammar and not a part of the code.' 111

Now let's run our code and you can see that if we get 71, which is greater than 70, then we get 112

you love each other like Kanye loves Kanye. 113

But if we get a different number, say 8 which is less than 50, then it tells us you don't like each 114

other. 115

So now, we're able to use these operators and combine different conditions to check for multiple things. 116

So we can check if love score is greater than 50, 117

and the love score is less than 70. Or we could check if the love score is greater than 50 or the love 118

score is less than 70. Although it won't be very helpful in our case because that means this is going 119

to be true when this is also going to be true. 120

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 121

than 50. 122

So this will also trigger. 123

Now while this works, while we can use these ways of combining different things to make our statements 124

more specific, there's a much better way of doing this. 125

We can start to build up a ladder of IF and ELSE statements. 126

So this is how it would work. 127

We can check to see if track one, this one, is clear. 128

Then we're going to get our train to go on to track one. 129

This is our first choice. But if that was false, 130

so if there was a rock there, then we can go to the next statement. And we're saying that ELSEIF track 131

2 is clear, then we should go down track2. But what if track2 also has a rock on it? 132

I don't know who's maintaining these railways by the way why there are rocks everywhere. 133

If track2 is also blocked then we can have another 134

Else If statement to check if track3 is clear, then that's going to be the one we'll take. 135

Now our code looks a bit like this. 136

So we can say if the love score is greater than 70, then do A. 137

But if that's not true, then we get on to our second choice. 138

So else if love scores greater than 30, then do B. 139

And finally if that's also false, then do C. 140

So if we get a Love Score of 50, then it's obviously not greater than 70, 141

so it skips A. But then it checks the next statement. 142

It checks to see if the love score is greater than 30, which it is. 143

So then it will go ahead and do B. 144

However if the love score was 75, then the first statement is already true. 145

So in this case it will simply go and do A. 146

And notice that if this was not an ELSE IF, but it was just an if, then this would also be true, 147

right? 148

75 is also greater than 30. 149

But this doesn't happen because this is an ELSE IF. So when we have an IF statement followed by an ELSE IF 150

followed by other ELSE IFs or ELSE, it means that once it finds one of the conditions to be true then 151

it will skip all of the other ones. 152

But if all of these were IF statements then it will check them one by one. 153

So if we had if love score was greater than 70 and if love score was greater than 30, then it will go 154

and evaluate the first condition. 155

So it's greater than 70. 156

It'll do A. 157

And then it sees that oh this is the end of the IF statement, and we've got another IF statement. 158

Well this one's also true. 159

75 is also greater than 30. 160

So I'm gonna go and do B instead. 161

Now there's cases where you might want your code to work like this but there's also cases where you 162

might want to use an ELSE IF instead. 163

So heading back to our code, we can actually change it so we don't even need to compare and have more 164

than one condition. Instead of checking to see if the love score is greater than 50 and less than 70, 165

I can change this to and ELSE IF instead. And I can simply write 166

else if the love score is greater than 50. and the final one doesn't even need an if, it could simply just 167

be an else. 168

So now what happens is that when I click run and we generate another new random number and we get a 169

score of 35, the first thing that happens is it checks to see if the love score is greater than 170

70. 35 is not greater than 70, so it skips this and goes to the next one. 171

Now it checks to see if 35 is greater than 50. which is also not true. 172

So it skips this block. 173

And finally because this is an else block, when everything above is not true, then it will simply carry 174

out the instruction inside here, which is to print 'You don't like each other.' 175

Now if we try again and this time we get 94, 176

then once it checks the first IF statement, it's already true and it carries out this function, then it 177

skips all of the other ones. 178

It doesn't also go and check to see if it's greater than 50. 179

But say if we had changed this to another if, then if I click to run and we get a large number like seventy 180

five, then this is true, 181

so it does this. But this is also true, 182

so it does that. 183

So this is the difference between an IF and ELSE IF. And you can actually build up your ELSE IF Statements 184

to have lots of them. 185

So you can have another one for example, if love score is greater than, I don't know, 30. Maybe you want a different 186

message for 30 to 50 187

in this case. And you have a different message for 50 to 70, 188

and here you have a different message for 70 to 100. 189

So have a play around with this code to fully appreciate the difference between a whole bunch of IF 190

statements and also a ladder of ELSE IF statements and how these types of conditional statements work 191

in Dart. Once you're done, we're going to head back to making our Quizzler app. 192

So I'll see you on the next lesson.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.