1 00:00:04,730 --> 00:00:06,620 In this video, we're going to continue 2 00:00:06,620 --> 00:00:08,570 our Kotlin tutorials and look at how to 3 00:00:08,570 --> 00:00:11,059 get our code to make decisions. Now we'll 4 00:00:11,059 --> 00:00:12,680 finish up on some variables and then 5 00:00:12,680 --> 00:00:14,750 we'll move on to the basics of classes, 6 00:00:14,750 --> 00:00:16,309 and the importance of understanding 7 00:00:16,309 --> 00:00:19,250 those. So let's get into that. Alright, so 8 00:00:19,250 --> 00:00:20,809 here we are where we left off at the end 9 00:00:20,809 --> 00:00:23,000 of the previous tutorial. Now if you 10 00:00:23,000 --> 00:00:24,529 haven't done so already, start up Android 11 00:00:24,529 --> 00:00:26,809 Studio, then use the option to open an 12 00:00:26,809 --> 00:00:28,910 existing Android Studio project, to open 13 00:00:28,910 --> 00:00:31,250 up the Kotlin tutorial project that we 14 00:00:31,250 --> 00:00:33,110 actually created quite a few videos ago. 15 00:00:33,110 --> 00:00:35,660 Now what I'm going to do first now, is 16 00:00:35,660 --> 00:00:37,250 delete everything in the main function, 17 00:00:37,250 --> 00:00:40,430 so that we can type in some new code. Now 18 00:00:40,430 --> 00:00:41,930 the opening and closing curly braces 19 00:00:41,930 --> 00:00:44,300 must stay, but delete everything between 20 00:00:44,300 --> 00:00:46,160 them. So I'm gonna show you what I mean. 21 00:00:46,160 --> 00:00:47,510 Firstly, I'm going to close down the run 22 00:00:47,510 --> 00:00:48,770 window by clicking on it down here, 23 00:00:48,770 --> 00:00:51,200 bottom left-hand corner. I'm going to 24 00:00:51,200 --> 00:00:54,410 select everything, essentially, from line 25 00:00:54,410 --> 00:00:57,170 two right down here to line 30. Press the del 26 00:00:57,170 --> 00:00:59,329 button. So you need to have that line 1 27 00:00:59,329 --> 00:01:01,310 stay as it is, with the left 28 00:01:01,310 --> 00:01:03,230 curly brace on the end, then the closing 29 00:01:03,230 --> 00:01:06,320 right curly brace on line 3, as well. So 30 00:01:06,320 --> 00:01:07,399 I'm going to talk briefly about 31 00:01:07,399 --> 00:01:10,399 conditional processing. Now the best way 32 00:01:10,399 --> 00:01:11,960 to show you that is to give you an 33 00:01:11,960 --> 00:01:13,609 example. Now this is something that 34 00:01:13,609 --> 00:01:16,579 you'll be using quite extensively in the course. 35 00:01:16,579 --> 00:01:18,859 In fact, the ability of the computer to 36 00:01:18,859 --> 00:01:20,389 branch to different bits of code, 37 00:01:20,389 --> 00:01:22,999 depending on some condition or other, is 38 00:01:22,999 --> 00:01:24,859 one of the things that make programs so 39 00:01:24,859 --> 00:01:26,899 powerful. So let's see an example of that - 40 00:01:26,899 --> 00:01:29,299 something you may use in a game to check 41 00:01:29,299 --> 00:01:31,700 if the player can continue playing, or if 42 00:01:31,700 --> 00:01:33,979 they've used up all their lives. So let's 43 00:01:33,979 --> 00:01:36,229 start by declaring an integer, lives, and 44 00:01:36,229 --> 00:01:38,479 give it the value 3. So we're going to 45 00:01:38,479 --> 00:01:43,789 start with val lives equals 3. Now 46 00:01:43,789 --> 00:01:45,109 remember that we don't have to specify 47 00:01:45,109 --> 00:01:47,479 that lives as an Int. Kotlin can infer 48 00:01:47,479 --> 00:01:49,549 that because we're giving lives the 49 00:01:49,549 --> 00:01:52,189 value 3. Now what if you wanted the 50 00:01:52,189 --> 00:01:53,840 computer to check how many lives the 51 00:01:53,840 --> 00:01:54,740 player has left? 52 00:01:54,740 --> 00:01:57,770 If the lives left is zero, print out a 53 00:01:57,770 --> 00:01:59,479 message to say Game Over, otherwise, 54 00:01:59,479 --> 00:02:01,340 print out a message to say You aren't 55 00:02:01,340 --> 00:02:04,009 dead yet. So how do we go about that? Well 56 00:02:04,009 --> 00:02:05,929 we'll start on the next line. Well 57 00:02:05,929 --> 00:02:08,119 actually, we'll start on line 4, and we'll 58 00:02:08,119 --> 00:02:09,800 check to see if the lives is less than 59 00:02:09,800 --> 00:02:13,819 1. So to do that, we type if parenthesis 60 00:02:13,819 --> 00:02:17,659 lives less than 1 - so the less than sign 61 00:02:17,659 --> 00:02:18,410 and the number one - 62 00:02:18,410 --> 00:02:21,230 right parentheses. Then we went our left 63 00:02:21,230 --> 00:02:23,030 curly brace, which will automatically add 64 00:02:23,030 --> 00:02:24,650 the right curly brace on the next line - 65 00:02:24,650 --> 00:02:27,350 that's opening a code block - and in there 66 00:02:27,350 --> 00:02:29,620 we're going to type println 67 00:02:29,620 --> 00:02:32,890 parentheses Game Over 68 00:02:32,890 --> 00:02:35,570 exclamation mark. And we've got the double 69 00:02:35,570 --> 00:02:38,840 quote and the right parenthesis. So 70 00:02:38,840 --> 00:02:41,210 we've used the if keyword, and that's 71 00:02:41,210 --> 00:02:43,640 followed by a condition to test. So in 72 00:02:43,640 --> 00:02:46,280 this case, the test is to check for lives 73 00:02:46,280 --> 00:02:48,620 being less than 1. Now we could also 74 00:02:48,620 --> 00:02:51,530 check for lives equals 0 there, but in a 75 00:02:51,530 --> 00:02:52,760 fantasy world, the player could have 76 00:02:52,760 --> 00:02:54,590 fallen foul of a really powerful wizard 77 00:02:54,590 --> 00:02:56,870 and lost several lives in one go. So 78 00:02:56,870 --> 00:02:58,250 we're making sure that the game's over, 79 00:02:58,250 --> 00:03:00,350 if the number of lives goes negative as 80 00:03:00,350 --> 00:03:06,710 well. So if I run this now, you can see we 81 00:03:06,710 --> 00:03:08,230 get no output - it doesn't print anything. 82 00:03:08,230 --> 00:03:11,030 So the code that we've put inside the if 83 00:03:11,030 --> 00:03:13,640 block isn't being executed, which is 84 00:03:13,640 --> 00:03:16,010 great because the number of lives is 3, 85 00:03:16,010 --> 00:03:18,980 and the condition here, lives less than 86 00:03:18,980 --> 00:03:22,610 1, isn't true. So all this code between 87 00:03:22,610 --> 00:03:24,950 the opening curly brace and the closing 88 00:03:24,950 --> 00:03:28,250 curly brace, forms a single block of code, 89 00:03:28,250 --> 00:03:30,800 and anything we put in there will only 90 00:03:30,800 --> 00:03:34,190 be executed if the condition's true. Now 91 00:03:34,190 --> 00:03:35,660 we can also tell the computer what to do 92 00:03:35,660 --> 00:03:37,760 if the condition's false, by adding an 93 00:03:37,760 --> 00:03:39,410 else clause. So let's go ahead and do 94 00:03:39,410 --> 00:03:41,720 that. So we're going to type this to the 95 00:03:41,720 --> 00:03:43,610 right of the closing, or the right curly 96 00:03:43,610 --> 00:03:47,180 brace; else. Then add another left curly 97 00:03:47,180 --> 00:03:49,490 brace which has a code block, and in 98 00:03:49,490 --> 00:03:50,890 there I'm going to type in println 99 00:03:50,890 --> 00:03:55,310 parenthesis double quotes You're still 100 00:03:55,310 --> 00:03:57,740 alive exclamation mark, and we've got 101 00:03:57,740 --> 00:04:00,440 a double quote and right parenthesis. So 102 00:04:00,440 --> 00:04:05,030 now when I run the code, this time we've 103 00:04:05,030 --> 00:04:06,770 got a message printing out, You're still 104 00:04:06,770 --> 00:04:08,600 alive, which has come from the else block. 105 00:04:08,600 --> 00:04:11,450 So that's pretty powerful - we can test a 106 00:04:11,450 --> 00:04:13,820 condition, and perform one set of actions 107 00:04:13,820 --> 00:04:15,800 if the condition's true, and a 108 00:04:15,800 --> 00:04:18,500 different set if it's false. Now if we 109 00:04:18,500 --> 00:04:20,269 change our lives, up here on line two, to 110 00:04:20,269 --> 00:04:26,750 zero and run it again, you can see that , 111 111 00:04:26,750 --> 00:04:29,120 we've got Game over appearing - so that's 112 00:04:29,120 --> 00:04:32,240 working fine. So if lives is less than 1, 113 00:04:32,240 --> 00:04:34,280 this bit of code runs - the code that's in 114 00:04:34,280 --> 00:04:36,169 between the left and right curly braces. 115 00:04:36,169 --> 00:04:38,960 If lives isn't less than one - so if it's 116 00:04:38,960 --> 00:04:41,300 one or above - then it goes to this code 117 00:04:41,300 --> 00:04:44,389 in the else block, and runs that instead. 118 00:04:44,389 --> 00:04:47,300 Now without this ability, programs would 119 00:04:47,300 --> 00:04:49,190 be pretty basic and could just follow a 120 00:04:49,190 --> 00:04:51,740 sequence of steps, but by checking 121 00:04:51,740 --> 00:04:54,560 conditions, we can allow the computer to 122 00:04:54,560 --> 00:04:56,599 make decisions and respond in different ways. 123 00:04:56,599 --> 00:04:59,539 Now conditions like lives less than 1 - 124 00:04:59,539 --> 00:05:03,349 this one here - that evaluates to a type 125 00:05:03,349 --> 00:05:05,419 of value that's called a Boolean. So we 126 00:05:05,419 --> 00:05:06,770 can actually store the result of that 127 00:05:06,770 --> 00:05:09,139 condition in a variable, and use the 128 00:05:09,139 --> 00:05:11,419 variable in the condition. So let's go 129 00:05:11,419 --> 00:05:13,310 ahead and make some changes to that. So 130 00:05:13,310 --> 00:05:14,990 I'm going to leave var lives equals 0, 131 00:05:14,990 --> 00:05:17,960 then on, or below that, we're going to 132 00:05:17,960 --> 00:05:23,139 put var isGameOver is equal to 133 00:05:23,139 --> 00:05:27,139 parentheses lives less than 1, and 134 00:05:27,139 --> 00:05:29,210 closing parentheses. And then we're going 135 00:05:29,210 --> 00:05:31,789 to put if - instead of lives less than 1 136 00:05:31,789 --> 00:05:35,180 we're going to put if - isGameOver, and leave 137 00:05:35,180 --> 00:05:37,400 everything else as is. So here the 138 00:05:37,400 --> 00:05:39,740 condition is evaluated, and the result is 139 00:05:39,740 --> 00:05:42,830 assigned to the variable here, isGame 140 00:05:42,830 --> 00:05:45,080 Over. Now in the if statement, 141 00:05:45,080 --> 00:05:47,479 you saw me change the hard-coded 142 00:05:47,479 --> 00:05:49,400 condition to use the value of the 143 00:05:49,400 --> 00:05:51,530 variable, or actually put the variable 144 00:05:51,530 --> 00:05:53,780 name in there so it will use the value 145 00:05:53,780 --> 00:05:55,340 that's stored in the variable. So 146 00:05:55,340 --> 00:05:56,270 different bits of code will run 147 00:05:56,270 --> 00:05:58,310 depending on whether it's true or false. 148 00:05:58,310 --> 00:06:03,560 So if we run it again now, it correctly 149 00:06:03,560 --> 00:06:05,090 says Game over and that's because the 150 00:06:05,090 --> 00:06:08,389 lives is set to 0. If I set lives to 3, 151 00:06:08,389 --> 00:06:13,729 back here on line 2, and run it again, we 152 00:06:13,729 --> 00:06:16,400 correctly get the message You're still alive. 153 00:06:16,400 --> 00:06:18,440 Now you don't have to store the result 154 00:06:18,440 --> 00:06:19,699 of a conditional expression in a 155 00:06:19,699 --> 00:06:21,949 variable, but it can be useful if you 156 00:06:21,949 --> 00:06:24,139 want to reuse the result, a little bit 157 00:06:24,139 --> 00:06:26,569 later in the code. We can also print the 158 00:06:26,569 --> 00:06:29,030 value of Boolean variables as well. So if 159 00:06:29,030 --> 00:06:31,099 we do something like this on the next 160 00:06:31,099 --> 00:06:36,319 line; we can type println parenthesis isGame 161 00:06:36,319 --> 00:06:42,050 Over to print that out. We run that - you can see 162 00:06:42,050 --> 00:06:44,970 it's printed out the value false, 163 00:06:44,970 --> 00:06:49,910 and if I change lives to zero, we run it, 164 00:06:49,910 --> 00:06:53,250 it now prints the value of true. So 165 00:06:53,250 --> 00:06:54,960 that's the Boolean type, and it's very 166 00:06:54,960 --> 00:06:56,940 useful for storing the state of things 167 00:06:56,940 --> 00:06:59,640 while a program is running. Alright, 168 00:06:59,640 --> 00:07:01,680 so back to our condition, and the syntax 169 00:07:01,680 --> 00:07:03,990 of if statements and expressions is, they 170 00:07:03,990 --> 00:07:07,200 start with the word if. That's followed 171 00:07:07,200 --> 00:07:09,840 by condition in parentheses, so we've got 172 00:07:09,840 --> 00:07:11,700 the if here, then a condition in 173 00:07:11,700 --> 00:07:14,490 parentheses. Now a condition's an 174 00:07:14,490 --> 00:07:16,770 expression that evaluates to true or 175 00:07:16,770 --> 00:07:19,520 false, so it must be a Boolean expression. 176 00:07:19,520 --> 00:07:21,840 So next we've got the block of code to 177 00:07:21,840 --> 00:07:24,030 execute if the condition's true - that's 178 00:07:24,030 --> 00:07:26,040 the code that's in the left and right 179 00:07:26,040 --> 00:07:28,080 curly braces here, in this case, print 180 00:07:28,080 --> 00:07:31,050 ln Game over. That's all we need, 181 00:07:31,050 --> 00:07:33,000 but we can also include some code to 182 00:07:33,000 --> 00:07:34,830 execute if the condition evaluates to 183 00:07:34,830 --> 00:07:37,200 false. We can do that by adding the else 184 00:07:37,200 --> 00:07:39,090 keyword here and adding another code 185 00:07:39,090 --> 00:07:41,250 block, and then this code in here will be 186 00:07:41,250 --> 00:07:44,370 executed if the condition's false. So 187 00:07:44,370 --> 00:07:46,320 that's a basic if statement, but I've 188 00:07:46,320 --> 00:07:48,270 thrown a few jargon words in there so I 189 00:07:48,270 --> 00:07:50,280 think I'd better explain them. Firstly, a 190 00:07:50,280 --> 00:07:52,020 block of code, or a code block, is 191 00:07:52,020 --> 00:07:53,820 everything between the opening and 192 00:07:53,820 --> 00:07:56,970 closing curly braces. Now a block of code 193 00:07:56,970 --> 00:07:58,860 can contain as many lines of code as you 194 00:07:58,860 --> 00:08:01,080 want, and can also contain more blocks. 195 00:08:01,080 --> 00:08:04,020 Now our main function defines a block - 196 00:08:04,020 --> 00:08:06,169 this is the code starting on line 1 - 197 00:08:06,169 --> 00:08:09,360 which is everything from this first left 198 00:08:09,360 --> 00:08:11,580 curly brace, right down to the last one. 199 00:08:11,580 --> 00:08:13,710 So basically, right down there. So 200 00:08:13,710 --> 00:08:15,240 everything in between those curly braces 201 00:08:15,240 --> 00:08:18,180 is part of the main function. Now inside 202 00:08:18,180 --> 00:08:19,950 that, we've actually got two more blocks. 203 00:08:19,950 --> 00:08:22,169 Each one has only got a single line of 204 00:08:22,169 --> 00:08:24,330 code but they're still code blocks - and 205 00:08:24,330 --> 00:08:26,460 this is the code, of course, this first code 206 00:08:26,460 --> 00:08:28,050 block here is if the condition is 207 00:08:28,050 --> 00:08:31,050 evaluated as true. And the last code 208 00:08:31,050 --> 00:08:32,309 block is if the condition is evaluated 209 00:08:32,309 --> 00:08:36,270 as false. Now a statement - a statement's 210 00:08:36,270 --> 00:08:37,919 sort of the equivalent of a sentence in 211 00:08:37,919 --> 00:08:41,010 English. So on line two, we've got an 212 00:08:41,010 --> 00:08:43,110 assignment statement. On the left, is a 213 00:08:43,110 --> 00:08:46,350 variable declaration, val lives, and on 214 00:08:46,350 --> 00:08:48,660 the right, we've got an expression. Now 215 00:08:48,660 --> 00:08:50,460 this is a simple expression - just a 0 216 00:08:50,460 --> 00:08:54,000 that evaluates to zero.... not surprisingly! Now 217 00:08:54,000 --> 00:08:56,970 on line four, we've got a more complex 218 00:08:56,970 --> 00:08:58,319 expression - lives 219 00:08:58,319 --> 00:09:01,049 less than 1. Now that can evaluate to 220 00:09:01,049 --> 00:09:03,239 true or false, depending on the value of 221 00:09:03,239 --> 00:09:06,149 lives. So statement 4 is also a statement 222 00:09:06,149 --> 00:09:08,970 that contains an expression. Now 223 00:09:08,970 --> 00:09:10,470 statements don't have to include an 224 00:09:10,470 --> 00:09:13,019 expression, and in fact, the next line - line 225 00:09:13,019 --> 00:09:15,509 five println is Game over - is a 226 00:09:15,509 --> 00:09:17,369 statement that just calls the println 227 00:09:17,369 --> 00:09:19,979 function. And then on line 7 228 00:09:19,979 --> 00:09:22,829 through 11, that forms our if statement. 229 00:09:22,829 --> 00:09:24,569 So the whole thing - the if and the else - 230 00:09:24,569 --> 00:09:27,449 all form a single statement. So in this 231 00:09:27,449 --> 00:09:29,009 if statement, we've got other statements - 232 00:09:29,009 --> 00:09:31,439 two calls to println - and this makes the 233 00:09:31,439 --> 00:09:33,329 statement more like a paragraph rather 234 00:09:33,329 --> 00:09:36,029 than a sentence. Ok, so let's look at some 235 00:09:36,029 --> 00:09:37,529 more conditions because, at the 236 00:09:37,529 --> 00:09:39,149 moment, we've only seen how to check if 237 00:09:39,149 --> 00:09:40,649 something's less than something else. 238 00:09:40,649 --> 00:09:42,539 Now I'm going to comment out all this 239 00:09:42,539 --> 00:09:44,459 code so that it's available, if you want 240 00:09:44,459 --> 00:09:46,079 to download my code from the resources 241 00:09:46,079 --> 00:09:47,789 section of this video. So I'm going to 242 00:09:47,789 --> 00:09:50,129 select all of that, right down to the 243 00:09:50,129 --> 00:09:51,839 closing right curly brace but not the 244 00:09:51,839 --> 00:09:54,239 very last one, making sure that the first 245 00:09:54,239 --> 00:09:56,129 line also isn't commented out. And I'm 246 00:09:56,129 --> 00:09:58,589 going to be my command slash, or control 247 00:09:58,589 --> 00:10:00,929 slash, if you're on a Windows or Linux 248 00:10:00,929 --> 00:10:03,470 machine. So I've commented out that code. 249 00:10:03,470 --> 00:10:06,509 Now I'm going to also add a line of code that I 250 00:10:06,509 --> 00:10:08,429 won't explain just yet, but don't worry 251 00:10:08,429 --> 00:10:09,749 about it - it's just a way to get input 252 00:10:09,749 --> 00:10:11,609 from the computer's console. So I'm gonna 253 00:10:11,609 --> 00:10:14,989 start by typing println parentheses 254 00:10:14,989 --> 00:10:19,319 How old are you, How old are you : and a 255 00:10:19,319 --> 00:10:22,589 space, and close off the line with the 256 00:10:22,589 --> 00:10:23,819 double quote and the right 257 00:10:23,819 --> 00:10:26,399 parenthesis. Then I'm going to type val 258 00:10:26,399 --> 00:10:32,309 age equals readLine parenthesis, and two 259 00:10:32,309 --> 00:10:35,399 exclamation marks dot toInt then 260 00:10:35,399 --> 00:10:38,489 parenthesis. Then we're going to type print 261 00:10:38,489 --> 00:10:44,339 ln parenthesis double quote age is and 262 00:10:44,339 --> 00:10:49,109 dollar age. So we print a message telling 263 00:10:49,109 --> 00:10:51,299 the user what to do. The next line takes 264 00:10:51,299 --> 00:10:52,949 input from the keyboard and converts it to 265 00:10:52,949 --> 00:10:55,409 an Int - to a number, in other words. Now 266 00:10:55,409 --> 00:10:56,759 there's a lot wrong with this but let's 267 00:10:56,759 --> 00:11:02,209 run it first to make sure it works. 268 00:11:02,209 --> 00:11:04,319 Alright, so there's our prompt and we can 269 00:11:04,319 --> 00:11:05,819 type in a number. So I'm going to type in 270 00:11:05,819 --> 00:11:08,759 a number like 21, and by the way, make 271 00:11:08,759 --> 00:11:10,559 sure that you click into the run window 272 00:11:10,559 --> 00:11:12,180 first, otherwise you'll be typing the number 273 00:11:12,180 --> 00:11:14,160 into the code editor. So note how I clicked 274 00:11:14,160 --> 00:11:15,830 there first before I started typing. 275 00:11:15,830 --> 00:11:18,810 OK, so I'm entering the value 21, and 276 00:11:18,810 --> 00:11:22,710 pressing enter. We can see age is 21 is 277 00:11:22,710 --> 00:11:23,940 in output, so we're getting it printed 278 00:11:23,940 --> 00:11:26,370 out okay. Now as I said, there's a lot 279 00:11:26,370 --> 00:11:27,960 wrong with this. There's no error 280 00:11:27,960 --> 00:11:30,089 checking, for example, so if I run the 281 00:11:30,089 --> 00:11:34,529 program again, and this time type in 282 00:11:34,529 --> 00:11:38,220 hello instead of a number, then the 283 00:11:38,220 --> 00:11:39,930 app, the application or the program 284 00:11:39,930 --> 00:11:41,850 actually crashes. And that's because 285 00:11:41,850 --> 00:11:43,529 hello can't be converted to a valid 286 00:11:43,529 --> 00:11:45,690 number, and it crashes with a number 287 00:11:45,690 --> 00:11:48,330 format exception. Now we have to learn to 288 00:11:48,330 --> 00:11:50,010 walk before we can run, so this bit of 289 00:11:50,010 --> 00:11:51,810 code will let us test our program with 290 00:11:51,810 --> 00:11:53,880 different numbers. So make sure you only 291 00:11:53,880 --> 00:11:55,320 type in valid numbers when we run the 292 00:11:55,320 --> 00:11:57,390 program. Alright, so let's see how to 293 00:11:57,390 --> 00:12:01,380 test different conditions in Kotlin. So I'm 294 00:12:01,380 --> 00:12:02,670 going to add some more code below the 295 00:12:02,670 --> 00:12:05,310 println. So I'm going to start with 296 00:12:05,310 --> 00:12:11,970 a val message : String. I'm going to 297 00:12:11,970 --> 00:12:15,200 put if parenthesis age is less than 18, 298 00:12:15,200 --> 00:12:17,600 and open a code block with the left 299 00:12:17,600 --> 00:12:20,310 curly brace, which'll automatically add the 300 00:12:20,310 --> 00:12:22,110 right one as well. Then we're gonna type, 301 00:12:22,110 --> 00:12:25,310 in that code block, message equals You're 302 00:12:25,310 --> 00:12:29,279 too young to vote. So that's 303 00:12:29,279 --> 00:12:31,110 straightforward, and we've seen a 304 00:12:31,110 --> 00:12:32,310 condition a lot similar to that, 305 00:12:32,310 --> 00:12:34,860 earlier. The code's assigning the text 306 00:12:34,860 --> 00:12:37,200 to the variable message, if the number 307 00:12:37,200 --> 00:12:38,730 that was entered in the keyboard - on the 308 00:12:38,730 --> 00:12:41,850 keyboard - was less than 18. We can also 309 00:12:41,850 --> 00:12:43,589 test for age being equal to some 310 00:12:43,589 --> 00:12:46,200 value as well. So a single equals symbol 311 00:12:46,200 --> 00:12:47,900 was used to assign a value to a variable, 312 00:12:47,900 --> 00:12:50,700 so we have to use two symbols - two equal 313 00:12:50,700 --> 00:12:53,279 symbols - to test for equality. So I can do 314 00:12:53,279 --> 00:12:58,790 something along these lines; else if 315 00:12:58,790 --> 00:13:02,520 then parentheses age equals equals, so 316 00:13:02,520 --> 00:13:05,220 two equal signs, 100, open a code 317 00:13:05,220 --> 00:13:06,950 block. Then we're gonna type message 318 00:13:06,950 --> 00:13:12,959 equals Congratulations. Now we can't get 319 00:13:12,959 --> 00:13:14,550 to see our message yet, because Kotlin 320 00:13:14,550 --> 00:13:16,649 won't compile the app if we try to print 321 00:13:16,649 --> 00:13:18,060 message. And I'm just going to add the 322 00:13:18,060 --> 00:13:19,440 code to print it, though anyway, then I'll 323 00:13:19,440 --> 00:13:21,810 show you what I mean. So type on this line 324 00:13:21,810 --> 00:13:24,329 now, println 325 00:13:24,329 --> 00:13:27,519 message in parentheses, and we're getting 326 00:13:27,519 --> 00:13:29,199 this error message here and if we have a 327 00:13:29,199 --> 00:13:31,889 look at, see what that error message is; 328 00:13:31,889 --> 00:13:35,470 Variable message must be initialized. So 329 00:13:35,470 --> 00:13:37,240 we didn't assign a value to message when 330 00:13:37,240 --> 00:13:40,389 we declared it on line 17, and there's a 331 00:13:40,389 --> 00:13:42,100 path through the program that could 332 00:13:42,100 --> 00:13:44,620 result in it not getting a value. So if 333 00:13:44,620 --> 00:13:46,660 the age is less than 18, everything's 334 00:13:46,660 --> 00:13:50,230 fine, and if the age is exactly 100, then 335 00:13:50,230 --> 00:13:52,629 message also gets a value. But for any 336 00:13:52,629 --> 00:13:55,139 other age, message will be undefined and 337 00:13:55,139 --> 00:13:57,999 Kotlin doesn't allow that. So in other 338 00:13:57,999 --> 00:13:59,499 words, we've accounted for people who are 339 00:13:59,499 --> 00:14:02,740 under 18, and we've accounted for anyone 340 00:14:02,740 --> 00:14:04,809 who's a hundred. But what about 341 00:14:04,809 --> 00:14:07,209 everyone else? We can do that with a 342 00:14:07,209 --> 00:14:08,740 final else statement, so we can come up 343 00:14:08,740 --> 00:14:10,480 here to the end of the code block, and 344 00:14:10,480 --> 00:14:12,779 do and else, open another code block, 345 00:14:12,779 --> 00:14:19,629 message equals You can vote. And notice 346 00:14:19,629 --> 00:14:21,189 that the error message disappears, and 347 00:14:21,189 --> 00:14:23,050 that's because we've accounted for every 348 00:14:23,050 --> 00:14:24,699 one, so message will always have a 349 00:14:24,699 --> 00:14:27,699 value when we can run the program. Now, I'm 350 00:14:27,699 --> 00:14:28,779 going to run that now, but if you do 351 00:14:28,779 --> 00:14:30,309 happen to get the error message Could 352 00:14:30,309 --> 00:14:32,339 not find or a load class Mainkt, 353 00:14:32,339 --> 00:14:34,509 remember to come up to here to Build and 354 00:14:34,509 --> 00:14:36,309 click on Rebuild Project. I'll just do 355 00:14:36,309 --> 00:14:37,809 that now anyway, even though I haven't 356 00:14:37,809 --> 00:14:39,790 tried to run it. And I'll try running it 357 00:14:39,790 --> 00:14:43,120 now. We're getting the prompt there. I'm 358 00:14:43,120 --> 00:14:45,040 clicking into the run window, and I can 359 00:14:45,040 --> 00:14:49,629 enter a value, 25; age is 25, You can vote. 360 00:14:49,629 --> 00:14:52,360 Now we can add as many conditions as we 361 00:14:52,360 --> 00:14:54,670 need, using else followed by another if, 362 00:14:54,670 --> 00:14:57,399 as you've seen me do on line 20 over 363 00:14:57,399 --> 00:15:00,069 here. That's a more complex if statement 364 00:15:00,069 --> 00:15:01,870 than we've had before, and it shows how 365 00:15:01,870 --> 00:15:03,879 to check for equality using the two 366 00:15:03,879 --> 00:15:06,699 equal signs. But, now before we look at 367 00:15:06,699 --> 00:15:08,740 the other conditions we can test, let's 368 00:15:08,740 --> 00:15:11,019 see what an if expression looks like. Now 369 00:15:11,019 --> 00:15:12,819 we can use if as a statement, like we've 370 00:15:12,819 --> 00:15:15,429 got here, or as an expression. Now if it's 371 00:15:15,429 --> 00:15:18,009 an expression, we can assign it to a 372 00:15:18,009 --> 00:15:21,879 variable. And in fact, Android Studio is 373 00:15:21,879 --> 00:15:23,439 offering to do that for us. 374 00:15:23,439 --> 00:15:25,360 So if we look at the and the if 375 00:15:25,360 --> 00:15:27,910 statement there. So notice that if, here, 376 00:15:27,910 --> 00:15:31,929 is underlined, so we click on that. We can 377 00:15:31,929 --> 00:15:33,879 see that the tooltip's saying Assignment 378 00:15:33,879 --> 00:15:36,550 can be lifted out of if, and the light 379 00:15:36,550 --> 00:15:37,480 bulb will actually give us an 380 00:15:37,480 --> 00:15:39,880 to do that. I'm gonna click it, over here. 381 00:15:39,880 --> 00:15:42,130 We're going to choose the option here to 382 00:15:42,130 --> 00:15:46,810 Lift assignment out of if. Now the rest 383 00:15:46,810 --> 00:15:48,610 of evaluating the if expression is 384 00:15:48,610 --> 00:15:50,680 assigned to message, as you can see there. 385 00:15:50,680 --> 00:15:53,350 So the result, by the way, is the last 386 00:15:53,350 --> 00:15:56,079 line in each block. If a block contains 387 00:15:56,079 --> 00:15:58,060 several lines of code, which is often the 388 00:15:58,060 --> 00:16:00,100 case, then the last line is the value 389 00:16:00,100 --> 00:16:03,100 that's assigned. So that's a more 390 00:16:03,100 --> 00:16:05,579 Kotlin-like way of writing this if, and 391 00:16:05,579 --> 00:16:08,529 you'll often see this form in the code 392 00:16:08,529 --> 00:16:09,399 that we'll be writing. 393 00:16:09,399 --> 00:16:11,470 Now I say it's more Kotlin-like but 394 00:16:11,470 --> 00:16:12,670 that really only 395 00:16:12,670 --> 00:16:14,769 applies when there are two conditions, 396 00:16:14,769 --> 00:16:17,290 and here we've got three; under 18, 397 00:16:17,290 --> 00:16:20,290 equal to 100 and everything else. So once 398 00:16:20,290 --> 00:16:22,540 you get to three or more conditions, it's 399 00:16:22,540 --> 00:16:24,940 so more usual to use a when statement, or 400 00:16:24,940 --> 00:16:27,760 when expression. Now we're going to be 401 00:16:27,760 --> 00:16:29,290 looking at them later, but I mentioned it 402 00:16:29,290 --> 00:16:31,120 now because Android Studio is suggesting 403 00:16:31,120 --> 00:16:33,519 that we use one. And you can see over 404 00:16:33,519 --> 00:16:35,110 here, if I come over here and hover over 405 00:16:35,110 --> 00:16:37,480 that; Cascade if can be replaced with 406 00:16:37,480 --> 00:16:38,949 when. But again, we'll be talking more about 407 00:16:38,949 --> 00:16:41,440 when statements or when expressions later 408 00:16:41,440 --> 00:16:44,500 on. But so that you've got this code 409 00:16:44,500 --> 00:16:46,060 available in the resources, I'm going to 410 00:16:46,060 --> 00:16:48,069 duplicate the if expression, and then 411 00:16:48,069 --> 00:16:50,410 comment out the duplicate. Now Android 412 00:16:50,410 --> 00:16:52,240 Studio makes it easy. You can just select all 413 00:16:52,240 --> 00:16:54,490 the lines you want to duplicate, then use 414 00:16:54,490 --> 00:16:57,010 Ctrl- D, or Command-D on a Mac, to 415 00:16:57,010 --> 00:16:59,260 duplicate the code. So I'll show you what I 416 00:16:59,260 --> 00:17:00,870 mean. I'm just going to select the code 417 00:17:00,870 --> 00:17:04,390 for message, up to and including the last 418 00:17:04,390 --> 00:17:06,970 brace, and I'm going to do, on my Mac, a 419 00:17:06,970 --> 00:17:10,299 Command-D. Notice how it's duplicated the 420 00:17:10,299 --> 00:17:12,459 code, and then I'm going to do a Command- 421 00:17:12,459 --> 00:17:17,439 / to comment out that code. And I'll just 422 00:17:17,439 --> 00:17:20,020 go down to the next line and comment that 423 00:17:20,020 --> 00:17:25,119 out. Okay. Now if we come over here now, select 424 00:17:25,119 --> 00:17:27,010 the if and then click on the light bulb, 425 00:17:27,010 --> 00:17:31,150 we can replace if with when, and that's 426 00:17:31,150 --> 00:17:32,919 even more readable. So each branch now 427 00:17:32,919 --> 00:17:35,290 starts with a condition, then the code to 428 00:17:35,290 --> 00:17:38,110 execute comes after the branch arrow. Now 429 00:17:38,110 --> 00:17:39,760 if none of the conditions match, the 430 00:17:39,760 --> 00:17:43,570 else branch is used. Now using when often 431 00:17:43,570 --> 00:17:46,030 results in more readable code, especially 432 00:17:46,030 --> 00:17:47,140 when there are a lot of conditions that 433 00:17:47,140 --> 00:17:48,940 may apply. And you're gonna be seeing 434 00:17:48,940 --> 00:17:50,910 lots of examples of when, so I'm going 435 00:17:50,910 --> 00:17:52,950 stop this video here. But in the next one, 436 00:17:52,950 --> 00:17:54,690 we're gonna start looking at classes in 437 00:17:54,690 --> 00:17:58,640 Kotlin. I'll see you in the next video.