All language subtitles for 38. [DART DEEP DIVE] More on if Statements

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: 1 00:00:02,400 --> 00:00:09,480 So in the last lecture, I introduced if statements. You create if statements by writing if and then between 2 00:00:09,630 --> 00:00:12,420 parentheses, your condition. 3 00:00:12,510 --> 00:00:18,390 Now whenever this condition here is met, you make it into this body of the if statement which is found 4 00:00:18,390 --> 00:00:24,690 between these curly braces and just as after all such body statements with curly braces, you don't have 5 00:00:24,690 --> 00:00:26,220 a semicolon there. 6 00:00:26,220 --> 00:00:29,440 Now what goes between these parentheses though? 7 00:00:29,460 --> 00:00:37,080 Now as I said, a condition but to be very precise, what goes between there is actually an expression 8 00:00:37,290 --> 00:00:40,200 that resolves to true or false 9 00:00:40,200 --> 00:00:45,100 and that's a specific type in Dart and this type exists in many programming languages, 10 00:00:45,120 --> 00:00:46,930 it's called a boolean. 11 00:00:46,950 --> 00:00:50,390 So here for example, we have a new variable, 12 00:00:50,610 --> 00:00:57,870 let's say we're tracking the logged in status of our user and that could be set to true or to false 13 00:00:57,870 --> 00:01:02,790 and that would be a boolean. A boolean is a value which is either true or false, 14 00:01:02,790 --> 00:01:06,480 there is no other possible value. So true or false 15 00:01:06,510 --> 00:01:13,590 and of course this is a very handy type of value, a very handy data type because often in programming, 16 00:01:13,590 --> 00:01:21,300 you have these two options and you especially need booleans in conjunction with if statements because 17 00:01:21,300 --> 00:01:28,710 here, you in the end want to find out if some condition is true, then do this or if some condition is 18 00:01:28,710 --> 00:01:29,980 false, then don't do that 19 00:01:30,030 --> 00:01:34,980 but maybe that's also something you have on an if condition, maybe you then do something else. 20 00:01:34,980 --> 00:01:40,720 So that would be your else block here which you can also specify on an if condition to have an alternative 21 00:01:40,860 --> 00:01:46,070 if this is not met. You don't need to have an else block though, it's totally fine if you just have the if 22 00:01:46,080 --> 00:01:49,310 block, then you might execute this code or do nothing 23 00:01:49,320 --> 00:01:55,390 if this condition is not met or if you have an else block, then you have an alternative code to run. 24 00:01:55,390 --> 00:01:59,490 So both is possible and booleans are the key to that. 25 00:01:59,490 --> 00:02:05,910 Now the condition here typically is one of two things, either you're referring to a boolean here, 26 00:02:05,910 --> 00:02:11,430 so to a variable that holds a boolean and then somewhere in your code, you probably have a way of setting 27 00:02:11,430 --> 00:02:16,800 this dynamically because of course, this code snippet here right now doesn't make a lot of sense because 28 00:02:17,070 --> 00:02:22,440 isLoggedIn is hardcoded to be false, hence you can never make it into this block, you would always 29 00:02:22,440 --> 00:02:28,680 go into the else block. But if isLoggedIn is controlled somewhere else in your code, if it is set dynamically 30 00:02:28,680 --> 00:02:34,410 based on some user input or some other action that is occurring, then this here is a typical check. 31 00:02:34,410 --> 00:02:38,460 Now what I'm doing here by the way is a shortcut to equals true, 32 00:02:38,580 --> 00:02:43,360 I'm comparing if isLoggedIn, so the value is stored in this variable, is true. 33 00:02:43,440 --> 00:02:45,630 That's the default check that is performed 34 00:02:45,630 --> 00:02:47,730 if you omit the double equals signs. 35 00:02:47,730 --> 00:02:54,300 So this here, if isLoggedIn, which can be read as if true, is equal to this check, 36 00:02:54,300 --> 00:03:00,150 so it's redundant to check for equality to true because that's the default if you just refer to a variable 37 00:03:00,510 --> 00:03:02,250 or to some expression. 38 00:03:02,250 --> 00:03:07,980 Now as I mentioned, this is one of two common scenarios, that you have some variable or maybe also some 39 00:03:07,980 --> 00:03:14,760 function call where you call some other function which returns true or false, so that you check something 40 00:03:14,760 --> 00:03:20,320 that yields true or false here and that something is a variable or a function call. 41 00:03:20,440 --> 00:03:26,730 The other common scenario is that you write a condition here where you directly compare two or more 42 00:03:26,730 --> 00:03:27,850 values. 43 00:03:27,990 --> 00:03:36,840 Let's say we have the username and that's Max and I have my age, 30 and I want to make sure that we execute 44 00:03:36,840 --> 00:03:37,710 some code here, 45 00:03:40,900 --> 00:03:43,780 logged in, when the username is equal to Max. 46 00:03:43,780 --> 00:03:49,240 Let's also add a password to make this a bit more realistic, tester. 47 00:03:49,300 --> 00:03:55,900 So here when my username and my password are correct, I want to print this, otherwise I want to take my user 48 00:03:55,900 --> 00:04:00,610 or the code execution into the else block and print failed. 49 00:04:00,610 --> 00:04:06,490 Now in such a case, we want to check the values of username and password and we therefore have no single 50 00:04:06,490 --> 00:04:11,320 variable that is true or false but we want to check if username has a certain value and password has 51 00:04:11,320 --> 00:04:16,720 a certain value. In such a scenario, you can check if username is equal to Max 52 00:04:16,990 --> 00:04:22,060 and and that's something new which you haven't seen before, the && sign here means that 53 00:04:22,060 --> 00:04:25,170 you also need some other condition to be true 54 00:04:25,180 --> 00:04:29,440 and here you check if password is equal to tester for example. 55 00:04:29,440 --> 00:04:34,180 So in this scenario, what I'm doing is I'm checking two condition and both have to be true to make it 56 00:04:34,180 --> 00:04:39,310 into this if block, if at least one of them is false, even if the other one is true, you make it into 57 00:04:39,310 --> 00:04:40,810 the else block here. 58 00:04:40,810 --> 00:04:46,840 So now if we run this code, we therefore see logged in because username is Max, I'm setting it here and password 59 00:04:46,870 --> 00:04:48,280 is tester, I'm setting it here. 60 00:04:48,280 --> 00:04:53,710 Please be aware that double equals sign has a totally different meaning than a single equals sign. You 61 00:04:53,710 --> 00:04:59,170 use a single equals sign here to store a value in the variable, you use a double equals sign here to 62 00:04:59,170 --> 00:05:05,820 compare a variable's value with another value and that is something totally different. Such a comparison, 63 00:05:05,910 --> 00:05:11,610 where you use such a boolean comparison operator as it's called, always yields a boolean. 64 00:05:11,760 --> 00:05:17,430 So this here yields true or false and that's where we're back to this boolean value up there 65 00:05:17,430 --> 00:05:25,650 and of course, you could also use some helper variable, like hasCorrectCredentials or whatever you want 66 00:05:25,650 --> 00:05:26,670 to use here 67 00:05:26,670 --> 00:05:31,830 where are you putting this check in, like this, 68 00:05:31,830 --> 00:05:37,440 this is a valid code and then down there, you only use that variable in the if condition to have a leaner 69 00:05:37,440 --> 00:05:38,420 if condition. 70 00:05:38,430 --> 00:05:39,720 This is totally valid, 71 00:05:39,720 --> 00:05:46,320 now I'm basically using this boolean operation here with my comparison and my concatenation of conditions 72 00:05:46,530 --> 00:05:52,040 to derive a boolean value which is true if both conditions are met or false if at least one fails and 73 00:05:52,040 --> 00:05:57,780 therefore I store true or false in hasCorrectCredentials and then I'm using hasCorrectCredentials 74 00:05:57,780 --> 00:05:59,770 down there, which is true or false. 75 00:05:59,880 --> 00:06:04,560 So that's all possible but I'd say the more common case is that you don't use such helper variables 76 00:06:04,680 --> 00:06:09,930 but that you put your condition right into your if statement here. 77 00:06:09,960 --> 00:06:15,990 Now sometimes you also have alternative conditions, here both have to be met but let's say for whatever 78 00:06:15,990 --> 00:06:17,890 reason, we also want to log the user in 79 00:06:17,970 --> 00:06:24,940 if he's older than 20. So we can also add an or statement with two pipe symbols here. 80 00:06:24,960 --> 00:06:27,430 So here, we can add or and check 81 00:06:27,440 --> 00:06:34,140 or age > 20 and that's another boolean comparison operator here. Besides a double equal sign, 82 00:06:34,140 --> 00:06:41,010 you also have greater than, greater than or equal, smaller than or smaller than or equal and also not 83 00:06:41,040 --> 00:06:41,430 equal 84 00:06:41,430 --> 00:06:43,520 with the exclamation mark in front of the equals sign. 85 00:06:43,680 --> 00:06:45,730 Please note that this is only one equal sign in here. 86 00:06:46,050 --> 00:06:47,850 So these are alternative operators 87 00:06:47,860 --> 00:06:52,950 and here I am using the greater than operator so that this part here returns true if age is greater 88 00:06:53,010 --> 00:06:55,410 than 20, which it here of course is. 89 00:06:55,530 --> 00:06:58,720 Now how can we read this overall expression though? 90 00:06:58,740 --> 00:07:03,770 Well let's see what happens if I now change my password, so that it's wrong, 91 00:07:03,780 --> 00:07:10,140 so that this first part here shouldn't be met. If I now run this, we're still logged in, 92 00:07:10,140 --> 00:07:11,970 so how can this be read? 93 00:07:12,150 --> 00:07:19,230 Well this reads as please log me in if this is true and this is true or if this is true 94 00:07:19,230 --> 00:07:24,090 and well, this here is not true and therefore, this combination here is not true 95 00:07:24,270 --> 00:07:30,870 but this here, this is true and since it's an or, this overrules the prior false which this returns because 96 00:07:30,870 --> 00:07:36,600 we have an alternative condition that could be met and only if this condition and this condition would 97 00:07:36,600 --> 00:07:42,480 be false, we would not make it in here because then neither of our two or conditions would be true. And 98 00:07:42,480 --> 00:07:47,910 of course, it's always a great idea that you simply play around with DartPad here and with if statements 99 00:07:48,090 --> 00:07:53,370 to find out how you can configure that and what you can do there and how the different operators behave. 100 00:07:54,450 --> 00:07:58,740 Sometimes you also want to combine conditions differently than they're parsed by default. 101 00:07:58,800 --> 00:08:04,200 So instead of maybe saying this and this should be true, you might also want to say well this should 102 00:08:04,200 --> 00:08:08,220 always be true and then this here should also yield true, 103 00:08:08,220 --> 00:08:11,910 so you want to combine this check, so you want to say yes you're logged in 104 00:08:11,910 --> 00:08:19,080 if the username is Max and then if either the password is tester or the age is greater than 20. Right 105 00:08:19,110 --> 00:08:24,430 now this reads as you're logged in if this is true and this is true or if this is true. 106 00:08:24,570 --> 00:08:29,400 So if you would want to combine password check and age check together, you could also use parentheses in 107 00:08:29,400 --> 00:08:29,600 there, 108 00:08:29,610 --> 00:08:34,800 just like in mathematical equations and now you're saying these two conditions here in the end belong 109 00:08:34,800 --> 00:08:35,460 together, 110 00:08:35,460 --> 00:08:40,410 so now the username always has to be Max and then you're either having the right password or the right 111 00:08:40,440 --> 00:08:41,230 age. 112 00:08:41,250 --> 00:08:44,820 So at the moment, that means we're logged in because the username is Max 113 00:08:44,820 --> 00:08:52,090 but now if I change Max up here to Maximilian, we would actually see that when I run this again, 114 00:08:52,120 --> 00:08:55,530 we're failing because this always needs to be true, 115 00:08:55,750 --> 00:08:59,750 I'm then combining it with the check of the latter condition here. 116 00:08:59,830 --> 00:09:04,990 Now if I would for example remove these parentheses which I just added, you'll see that now, I would 117 00:09:04,990 --> 00:09:07,700 actually be logged in and that's making a difference, 118 00:09:07,780 --> 00:09:12,800 I would be logged in now because I'm checking is this true and this true or is this true 119 00:09:12,880 --> 00:09:14,920 and neither of these first two checks is true 120 00:09:14,950 --> 00:09:17,780 but this here is and therefore that's why we're now logged in. 121 00:09:17,830 --> 00:09:21,010 So using these parentheses correctly is very important 122 00:09:21,010 --> 00:09:25,450 and of course, it's also important that you use these comparison operators correctly, 123 00:09:25,480 --> 00:09:30,460 also be aware of the not equal operator which is also super important and therefore you have a lot of 124 00:09:30,460 --> 00:09:38,200 power to control when your code executes. Now sometimes, you also have a more complex if check where you 125 00:09:38,200 --> 00:09:44,560 don't just have an if and an else statement but you know if this here fails, I might have some alternative 126 00:09:44,560 --> 00:09:51,100 code to execute but that code also depends on some condition and then you can add an else if statement, 127 00:09:52,820 --> 00:09:56,480 you can then also still have a normal else statement if you want to. 128 00:09:56,480 --> 00:10:01,730 Now you would say well if this here is not met, if this first condition is not met, then we make it to 129 00:10:01,730 --> 00:10:07,960 the next block in line and that is an else if statement and there, I can check yet another condition 130 00:10:08,210 --> 00:10:11,930 and only if this condition is then also not met, we make it into this else block. 131 00:10:12,080 --> 00:10:15,670 If this condition here is met, we don't make it into this else block 132 00:10:15,780 --> 00:10:19,200 and here we could check if isLoggedIn is true 133 00:10:19,370 --> 00:10:25,800 and now if I also add this extra parentheses pair up there again, so that the first check will fail, 134 00:10:25,910 --> 00:10:32,950 now we should make it into the else if block and then we could print overruled here to see that this executed 135 00:10:33,410 --> 00:10:38,510 but when I hit run here, you indeed see failed because of course isLoggedIn is false. 136 00:10:38,510 --> 00:10:43,370 As soon as we set this to true however or where we derive this dynamically and it yields true, then 137 00:10:43,370 --> 00:10:49,280 we see overruled because now we're running the else if block because the first condition was not met. 138 00:10:49,280 --> 00:10:50,550 Therefore we got into here, 139 00:10:50,660 --> 00:10:52,580 we checked this and this is true 140 00:10:52,610 --> 00:10:54,160 and therefore now, we're running this, 141 00:10:54,170 --> 00:10:56,950 we're then not making it into the else block too. 142 00:10:57,170 --> 00:11:03,020 These are if statements and they're an important construct in basically any programming language you 143 00:11:03,020 --> 00:11:08,900 can learn and therefore also in Dart. We'll use them a lot throughout the course and I hope that this lecture 144 00:11:08,900 --> 00:11:12,890 here was helpful in understanding how they work and how you may play around with them. 16460

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