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

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,330 --> 00:00:00,600 All right. 1 2 00:00:00,660 --> 00:00:06,630 So for those you guys who are unfamiliar with Dart, in this lesson, I want to quickly cover Dart conditionals. 2 3 00:00:07,110 --> 00:00:15,330 And more specifically, the IF and ELSE. Now an IF statement basically just checks to see if a condition 3 4 00:00:15,450 --> 00:00:24,060 is true. And if the condition is true, then it's going to carry out the instructions inside a set of curly 4 5 00:00:24,060 --> 00:00:24,960 braces. 5 6 00:00:24,960 --> 00:00:33,630 So in this case, our IF statement says that if the track is clear, the double equals means assess to see 6 7 00:00:33,630 --> 00:00:36,810 if the left hand side is equal to the right hand side, 7 8 00:00:36,900 --> 00:00:39,270 but a single equals is an assignment. 8 9 00:00:39,270 --> 00:00:43,470 So it means make the left hand side equal the right hand side, 9 10 00:00:43,500 --> 00:00:47,070 and that's a subtle difference to be aware of when you're coding. 10 11 00:00:47,520 --> 00:00:53,760 So in this case we're saying in English it means if the track is clear, then go. 11 12 00:00:53,760 --> 00:00:58,010 If we go through the statement, then this is where the condition is, 12 13 00:00:58,080 --> 00:01:02,570 and this is what the instruction is. 13 14 00:01:02,580 --> 00:01:09,030 So when the computer evaluates the statement and in this case the track is indeed clear, then it will 14 15 00:01:09,090 --> 00:01:10,090 just go. 15 16 00:01:10,800 --> 00:01:14,520 But if when we evaluate this it's actually not true, 16 17 00:01:14,520 --> 00:01:18,560 the track is not clear, in this case there's a giant rock there, 17 18 00:01:18,690 --> 00:01:23,780 then it won't carry out the instructions inside the curly braces. 18 19 00:01:23,790 --> 00:01:27,860 Instead it will skip all of this and go onto the next line of code 19 20 00:01:27,860 --> 00:01:29,750 that it needs to execute. 20 21 00:01:29,760 --> 00:01:33,660 So in this case, the train goes nowhere. In the last lesson, 21 22 00:01:33,670 --> 00:01:40,350 we saw that in addition to just using a simple IF to trigger some statement when the conditions are met, 22 23 00:01:40,360 --> 00:01:42,220 we could also use IF and ELSE. 23 24 00:01:42,430 --> 00:01:51,010 So let's say that we're saying if the track is clear, then the train should go straight but else i.e. 24 25 00:01:51,010 --> 00:01:56,800 if the track is not clear, then it should turn right instead. 25 26 00:01:56,830 --> 00:02:02,770 In this case this is what the code would look like. the condition that we're testing for is if the track 26 27 00:02:02,770 --> 00:02:03,650 is clear. 27 28 00:02:03,940 --> 00:02:07,480 And in that case, it should carry out this piece of instruction, 28 29 00:02:07,600 --> 00:02:12,580 everything in between this set of curly braces. So go straight. 29 30 00:02:12,580 --> 00:02:21,040 But if this condition was not true then it skips ahead to the else statement and it carries out what's 30 31 00:02:21,130 --> 00:02:28,870 inside this set of curly braces instead, which happens to be turn right. Now more commonly, you'll see IF 31 32 00:02:28,870 --> 00:02:36,730 and ELSE statements structured more like this, where we have our condition and then we have a set of curly 32 33 00:02:36,730 --> 00:02:39,010 braces and then we have all else 33 34 00:02:39,130 --> 00:02:45,720 and what to do if that condition is false. And this is what we did in our last lesson as well. 34 35 00:02:45,730 --> 00:02:50,890 Now if you live with a programmer then it's really dangerous and you have to be very careful how you word 35 36 00:02:50,890 --> 00:02:51,620 things. 36 37 00:02:51,640 --> 00:02:56,500 For example if you told your husband or wife who's a programmer, 'Please go to the supermarket and buy 37 38 00:02:56,530 --> 00:03:00,210 one bottle of milk, if they have eggs buy 6.' 38 39 00:03:00,250 --> 00:03:06,230 You might think what you're saying is buy one bottle of milk and if there's eggs by six eggs. 39 40 00:03:06,400 --> 00:03:13,000 But what they might hear is if haveEggs = true, then buy 6 bottles of milk, 40 41 00:03:13,060 --> 00:03:15,840 otherwise buy one bottle of milk. 41 42 00:03:15,940 --> 00:03:19,710 So a shout out to everybody who lives with programmers. 42 43 00:03:19,870 --> 00:03:25,550 Now in order to demonstrate how this code actually works, we're going to build a love calculator. 43 44 00:03:25,570 --> 00:03:32,710 So it's one of those throwbacks from the 80s where you test your compatibility with whoever you fancy. 44 45 00:03:32,710 --> 00:03:36,060 Now we're going to keep our love calculator really simple. 45 46 00:03:36,070 --> 00:03:40,620 We're going to make a function that's called loveCalculator. 46 47 00:03:40,900 --> 00:03:47,310 And it's simply just going to generate a random number between 1 and 100. 47 48 00:03:47,320 --> 00:03:49,110 So we know how to do that already 48 49 00:03:49,150 --> 00:03:55,050 from our previous lessons. We have to first import the Dart math library. 49 50 00:03:55,090 --> 00:03:57,660 Remember it's math, and not maths. 50 51 00:03:57,700 --> 00:04:05,320 And once we've imported it, then we can use the random number generator. So we can create a new variable 51 52 00:04:05,320 --> 00:04:13,360 called loveScore and it's going to be equal to the random number generator .nextInt because we're 52 53 00:04:13,360 --> 00:04:14,670 generating whole numbers. 53 54 00:04:14,890 --> 00:04:17,420 And we're going to make it 100. 54 55 00:04:17,440 --> 00:04:25,090 So they generate all the numbers from 0 to 99. And then we add 1 to change our range to 55 56 00:04:25,180 --> 00:04:27,070 1 to 100. 56 57 00:04:27,070 --> 00:04:34,210 So now that we can generate our random number loveScore, and I apologize to everybody who really believes 57 58 00:04:34,210 --> 00:04:35,740 in love calculators. 58 59 00:04:35,740 --> 00:04:38,950 But in this case it's going to be a little bit random. 59 60 00:04:39,100 --> 00:04:45,520 So now that we've generated loveScore, let's print it out and let's see what we get. 60 61 00:04:45,520 --> 00:04:54,310 So if we call our function up here in the main function and we call our love calculator, and then we 61 62 00:04:54,310 --> 00:04:56,800 run our code, we should see the result in the console. 62 63 00:04:57,190 --> 00:05:03,210 So the first time we got 29%, we got 18%, keeps on getting lower, 22%. 63 64 00:05:03,220 --> 00:05:06,930 So today's not a day for love it seems it's, oh! There we go. 64 65 00:05:06,940 --> 00:05:12,130 We got a 96%. We finally got a high score. So you can see there are a whole bunch of random numbers between 65 66 00:05:12,130 --> 00:05:13,420 1 and 100. 66 67 00:05:13,900 --> 00:05:22,330 Now if we wanted to give the user an interpretation of their love score based on this number, then 67 68 00:05:22,420 --> 00:05:33,280 we could use an IF statement. So we could say that if the love score is greater then let's say 70, then 68 69 00:05:33,310 --> 00:05:43,060 that means that they, you know, maybe they really like each other. Maybe they... you love each other like 69 70 00:05:43,120 --> 00:05:46,890 Kanye loves Kanye. 70 71 00:05:48,790 --> 00:05:54,820 And if they got a score that's lower than 70, then it will just say 71 72 00:05:58,050 --> 00:05:59,100 you'd like each other. 72 73 00:06:03,990 --> 00:06:04,240 All right. 73 74 00:06:04,330 --> 00:06:11,230 So now, depending on our love score, we should get something different printed in our console. 74 75 00:06:11,230 --> 00:06:19,750 And if we run, it we can see that the first time we got 30, so we get the text we like each... 75 76 00:06:19,750 --> 00:06:22,730 you like each other. 76 77 00:06:22,840 --> 00:06:24,830 Not sure how I missed so many letters. 77 78 00:06:25,150 --> 00:06:28,470 And if we run it again, we get 9, 78 79 00:06:28,480 --> 00:06:33,520 so you like each other. And eventually hopefully we should get... oh wow! 79 80 00:06:33,520 --> 00:06:34,630 We actually got 100. 80 81 00:06:34,630 --> 00:06:35,510 Jackpot! 81 82 00:06:35,510 --> 00:06:40,450 So you love each other like Kanye loves Kanye. So you can see that depending on the love score that 82 83 00:06:40,450 --> 00:06:46,200 we got out of the random generator, we're able to print different text into the console. 83 84 00:06:46,300 --> 00:06:49,930 And this is all because of our IF statement. 84 85 00:06:49,930 --> 00:06:55,990 Now what if we wanted to have more than just one bracket because at the moment we can only have a different 85 86 00:06:56,140 --> 00:07:00,730 piece of interpretation for above 70 or below 70? 86 87 00:07:00,730 --> 00:07:02,580 What if we wanted it to be more nuance? 87 88 00:07:02,590 --> 00:07:08,410 What if we wanted 70 to 100 and 50 to 70 and 0 to 50? 88 89 00:07:08,440 --> 00:07:10,110 How would we do that? 89 90 00:07:10,120 --> 00:07:12,280 Well when we're using IF statements, 90 91 00:07:12,280 --> 00:07:15,650 we also have a whole bunch of operators that we can use. 91 92 00:07:15,700 --> 00:07:21,880 For example we already saw that we can use to double equals to check if the left hand side is equal to 92 93 00:07:21,940 --> 00:07:23,220 the right hand side. 93 94 00:07:23,380 --> 00:07:30,160 But we can also check if it's not equal to by using an exclamation mark, an equal sign. Or if the left 94 95 00:07:30,160 --> 00:07:37,670 hand side is greater than the right hand side, lesser than, greater or equal to, or lesser or equal to. 95 96 00:07:37,870 --> 00:07:44,320 And then we can combine these conditions together, in an IF statement, using these comparatives. 96 97 00:07:44,320 --> 00:07:54,700 So for example we can have two && for AND, and two || for OR, and a single exclamation mark 97 98 00:07:54,760 --> 00:08:02,440 for NOT. So let me show you how this works. If we have are IF statement for if loveScore is greater than 98 99 00:08:02,440 --> 00:08:10,180 70, then do this. We can also have another IF statement down here. Let's say in this one we want to check 99 100 00:08:10,570 --> 00:08:26,020 if the love score is greater than say 50 and the love score is less than 70, then we print 'You like each 100 101 00:08:26,020 --> 00:08:41,750 other'. But if the love score is less than 50 then we print you don't like each other. All right. 101 102 00:08:41,780 --> 00:08:47,030 So now we get an error over here that says too many positional arguments. 102 103 00:08:47,030 --> 00:08:49,350 One expected, but it found five. 103 104 00:08:49,370 --> 00:08:50,810 What is this all about? 104 105 00:08:50,810 --> 00:08:53,570 Well remember what we said about the single quotes. 105 106 00:08:53,600 --> 00:08:59,300 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 107 00:08:59,300 --> 00:08:59,900 end. 107 108 00:08:59,990 --> 00:09:06,290 So whenever we're using single quotes inside our strings, we always have to use the backslash to escape 108 109 00:09:06,290 --> 00:09:06,590 it. 109 110 00:09:06,590 --> 00:09:09,140 Tell it, 'Don't look at this as code. 110 111 00:09:09,140 --> 00:09:13,620 This is just something that we use in English grammar and not a part of the code.' 111 112 00:09:13,640 --> 00:09:21,080 Now let's run our code and you can see that if we get 71, which is greater than 70, then we get 112 113 00:09:21,080 --> 00:09:23,070 you love each other like Kanye loves Kanye. 113 114 00:09:23,390 --> 00:09:29,540 But if we get a different number, say 8 which is less than 50, then it tells us you don't like each 114 115 00:09:29,540 --> 00:09:30,480 other. 115 116 00:09:30,500 --> 00:09:39,270 So now, we're able to use these operators and combine different conditions to check for multiple things. 116 117 00:09:39,410 --> 00:09:42,530 So we can check if love score is greater than 50, 117 118 00:09:42,530 --> 00:09:48,800 and the love score is less than 70. Or we could check if the love score is greater than 50 or the love 118 119 00:09:48,800 --> 00:09:54,050 score is less than 70. Although it won't be very helpful in our case because that means this is going 119 120 00:09:54,050 --> 00:09:56,270 to be true when this is also going to be true. 120 121 00:09:56,630 --> 00:10:05,570 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 122 00:10:05,570 --> 00:10:06,250 than 50. 122 123 00:10:06,260 --> 00:10:09,080 So this will also trigger. 123 124 00:10:09,080 --> 00:10:16,000 Now while this works, while we can use these ways of combining different things to make our statements 124 125 00:10:16,100 --> 00:10:19,900 more specific, there's a much better way of doing this. 125 126 00:10:20,060 --> 00:10:24,470 We can start to build up a ladder of IF and ELSE statements. 126 127 00:10:24,470 --> 00:10:26,120 So this is how it would work. 127 128 00:10:26,150 --> 00:10:30,780 We can check to see if track one, this one, is clear. 128 129 00:10:31,010 --> 00:10:33,880 Then we're going to get our train to go on to track one. 129 130 00:10:33,890 --> 00:10:38,450 This is our first choice. But if that was false, 130 131 00:10:38,480 --> 00:10:45,020 so if there was a rock there, then we can go to the next statement. And we're saying that ELSEIF track 131 132 00:10:45,050 --> 00:10:52,880 2 is clear, then we should go down track2. But what if track2 also has a rock on it? 132 133 00:10:52,920 --> 00:10:56,940 I don't know who's maintaining these railways by the way why there are rocks everywhere. 133 134 00:10:57,070 --> 00:11:00,540 If track2 is also blocked then we can have another 134 135 00:11:00,540 --> 00:11:06,720 Else If statement to check if track3 is clear, then that's going to be the one we'll take. 135 136 00:11:06,720 --> 00:11:10,600 Now our code looks a bit like this. 136 137 00:11:10,740 --> 00:11:15,960 So we can say if the love score is greater than 70, then do A. 137 138 00:11:16,260 --> 00:11:21,600 But if that's not true, then we get on to our second choice. 138 139 00:11:21,600 --> 00:11:26,220 So else if love scores greater than 30, then do B. 139 140 00:11:26,220 --> 00:11:30,140 And finally if that's also false, then do C. 140 141 00:11:30,150 --> 00:11:35,000 So if we get a Love Score of 50, then it's obviously not greater than 70, 141 142 00:11:35,220 --> 00:11:40,830 so it skips A. But then it checks the next statement. 142 143 00:11:40,830 --> 00:11:45,090 It checks to see if the love score is greater than 30, which it is. 143 144 00:11:45,090 --> 00:11:48,740 So then it will go ahead and do B. 144 145 00:11:48,810 --> 00:11:55,200 However if the love score was 75, then the first statement is already true. 145 146 00:11:55,200 --> 00:11:59,080 So in this case it will simply go and do A. 146 147 00:11:59,190 --> 00:12:06,950 And notice that if this was not an ELSE IF, but it was just an if, then this would also be true, 147 148 00:12:06,960 --> 00:12:07,230 right? 148 149 00:12:07,230 --> 00:12:10,340 75 is also greater than 30. 149 150 00:12:10,350 --> 00:12:18,120 But this doesn't happen because this is an ELSE IF. So when we have an IF statement followed by an ELSE IF 150 151 00:12:18,210 --> 00:12:26,550 followed by other ELSE IFs or ELSE, it means that once it finds one of the conditions to be true then 151 152 00:12:26,550 --> 00:12:28,920 it will skip all of the other ones. 152 153 00:12:29,640 --> 00:12:35,130 But if all of these were IF statements then it will check them one by one. 153 154 00:12:35,130 --> 00:12:42,540 So if we had if love score was greater than 70 and if love score was greater than 30, then it will go 154 155 00:12:42,660 --> 00:12:45,060 and evaluate the first condition. 155 156 00:12:45,060 --> 00:12:46,600 So it's greater than 70. 156 157 00:12:46,710 --> 00:12:48,060 It'll do A. 157 158 00:12:48,060 --> 00:12:53,010 And then it sees that oh this is the end of the IF statement, and we've got another IF statement. 158 159 00:12:53,010 --> 00:12:54,480 Well this one's also true. 159 160 00:12:54,480 --> 00:12:56,630 75 is also greater than 30. 160 161 00:12:56,670 --> 00:12:58,970 So I'm gonna go and do B instead. 161 162 00:12:59,010 --> 00:13:04,530 Now there's cases where you might want your code to work like this but there's also cases where you 162 163 00:13:04,530 --> 00:13:07,020 might want to use an ELSE IF instead. 163 164 00:13:08,070 --> 00:13:15,510 So heading back to our code, we can actually change it so we don't even need to compare and have more 164 165 00:13:15,510 --> 00:13:23,130 than one condition. Instead of checking to see if the love score is greater than 50 and less than 70, 165 166 00:13:23,220 --> 00:13:28,770 I can change this to and ELSE IF instead. And I can simply write 166 167 00:13:29,010 --> 00:13:37,470 else if the love score is greater than 50. and the final one doesn't even need an if, it could simply just 167 168 00:13:37,470 --> 00:13:38,900 be an else. 168 169 00:13:39,420 --> 00:13:45,870 So now what happens is that when I click run and we generate another new random number and we get a 169 170 00:13:45,870 --> 00:13:51,990 score of 35, the first thing that happens is it checks to see if the love score is greater than 170 171 00:13:51,990 --> 00:13:58,330 70. 35 is not greater than 70, so it skips this and goes to the next one. 171 172 00:13:58,470 --> 00:14:03,750 Now it checks to see if 35 is greater than 50. which is also not true. 172 173 00:14:03,750 --> 00:14:05,060 So it skips this block. 173 174 00:14:05,250 --> 00:14:12,480 And finally because this is an else block, when everything above is not true, then it will simply carry 174 175 00:14:12,480 --> 00:14:17,220 out the instruction inside here, which is to print 'You don't like each other.' 175 176 00:14:17,220 --> 00:14:22,390 Now if we try again and this time we get 94, 176 177 00:14:22,500 --> 00:14:29,490 then once it checks the first IF statement, it's already true and it carries out this function, then it 177 178 00:14:29,490 --> 00:14:32,200 skips all of the other ones. 178 179 00:14:32,310 --> 00:14:35,420 It doesn't also go and check to see if it's greater than 50. 179 180 00:14:36,030 --> 00:14:45,390 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 181 00:14:45,390 --> 00:14:47,850 five, then this is true, 181 182 00:14:47,850 --> 00:14:50,320 so it does this. But this is also true, 182 183 00:14:50,340 --> 00:14:51,090 so it does that. 183 184 00:14:51,540 --> 00:14:58,320 So this is the difference between an IF and ELSE IF. And you can actually build up your ELSE IF Statements 184 185 00:14:58,320 --> 00:14:59,520 to have lots of them. 185 186 00:14:59,550 --> 00:15:07,110 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 187 00:15:07,110 --> 00:15:09,600 message for 30 to 50 187 188 00:15:09,600 --> 00:15:13,530 in this case. And you have a different message for 50 to 70, 188 189 00:15:13,530 --> 00:15:17,020 and here you have a different message for 70 to 100. 189 190 00:15:17,130 --> 00:15:22,580 So have a play around with this code to fully appreciate the difference between a whole bunch of IF 190 191 00:15:22,590 --> 00:15:29,100 statements and also a ladder of ELSE IF statements and how these types of conditional statements work 191 192 00:15:29,190 --> 00:15:34,570 in Dart. Once you're done, we're going to head back to making our Quizzler app. 192 193 00:15:34,670 --> 00:15:36,260 So I'll see you on the next lesson. 20782

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