0:00:01.250,0:00:03.667 Here's where we left off[br]with our coin-flipping game. 0:00:03.656,0:00:06.596 We started out by generating[br]a random number between 0 and 1, 0:00:06.596,0:00:08.876 and then rounding that number[br]to the nearest integer. 0:00:08.877,0:00:12.213 So this gave us a number[br]that was either 0 or 1. 0:00:12.214,0:00:15.908 And then we illustrated[br]our super-realistic coin with this ellipse 0:00:15.909,0:00:18.908 that was either purple or yellow,[br]depending on what the number was. 0:00:18.909,0:00:20.986 And here's the code where we did that. 0:00:20.987,0:00:24.158 So we said if integer is equal to 0[br]set the fill code to be purple, 0:00:24.159,0:00:25.818 otherwise, set it to yellow. 0:00:25.819,0:00:27.916 And then we draw the coin down here. 0:00:27.917,0:00:30.924 But now I've decided[br]that actually my coin has 3 sides. 0:00:30.925,0:00:32.454 Yep, a 3-sided coin. 0:00:32.455,0:00:34.189 So let's add that third side. 0:00:34.190,0:00:37.108 I'm going to start by making[br]a number between 0 and 2, 0:00:37.109,0:00:41.532 so after we round that, it will give me[br]an integer that's either 0, 1, or 2. 0:00:41.533,0:00:45.608 But if we look at our drawing code,[br]we're only accounting for 2 cases here. 0:00:45.609,0:00:48.814 So if integer is 0, make it purple,[br]otherwise, make it yellow. 0:00:48.815,0:00:51.884 But now, otherwise can mean[br]either 1 or 2. 0:00:51.885,0:00:54.930 But wait, what if I did the same thing[br]that we did before, 0:00:54.931,0:00:56.674 but inside this else block? 0:00:56.675,0:01:00.748 So I'm just going to say:[br]if integer is equal to 1, 0:01:00.749,0:01:04.933 then set the fill color to be yellow,[br]like it was before, 0:01:05.918,0:01:09.877 else, we will set the fill color [br]to be red. 0:01:09.853,0:01:11.402 Red's pretty good. 0:01:11.403,0:01:12.577 So what this means is, 0:01:12.578,0:01:15.477 if integer is equal to 0,[br]set the color to be purple, 0:01:15.478,0:01:18.983 otherwise, if it's 1,[br]we will make it yellow; 0:01:18.984,0:01:20.330 otherwise, that is, 0:01:20.331,0:01:24.610 if it wasn't 0, it wasn't 1,[br]it must be 2, we'll make the color red. 0:01:24.626,0:01:28.585 I press restart a bunch of times.[br]It works! Woohoo! 0:01:28.566,0:01:30.865 So then if I wanted[br]to add more sides to my coin, 0:01:30.866,0:01:35.676 I can go up here, make it 0 to 3,[br]and then go deeper into this else block 0:01:35.677,0:01:37.986 and add more if-else's[br]and so on and so forth, 0:01:37.987,0:01:40.157 until I have a bazillion nested blocks. 0:01:40.158,0:01:44.703 And only then will I realize this code[br]is disgusting! I mean gross! 0:01:44.704,0:01:48.760 Code is supposed to be easy to read[br]and pretty, not as ugly as possible. 0:01:48.761,0:01:50.729 So here's what we're going to do: 0:01:50.730,0:01:52.904 Whenever you have[br]just a single if statement 0:01:52.905,0:01:55.674 or an if-else statement[br]inside an else block, 0:01:55.675,0:01:58.752 so that means we're not doing[br]anything else outside of these blocks, 0:01:58.753,0:02:00.545 we're not setting the stroke color, 0:02:00.546,0:02:04.765 we don't have anymore[br]if statements, nothing. 0:02:04.766,0:02:08.443 All we have is that one if statement,[br]and maybe it comes with an else block. 0:02:08.443,0:02:12.755 Then we can actually combine[br]this condition with the line before 0:02:12.756,0:02:17.072 and say, else if integer is equal to 1,[br]then set the fill color to yellow. 0:02:17.073,0:02:19.948 And then this last else block[br]isn't nested anywhere, 0:02:19.949,0:02:22.318 just comes at the very end all by itself. 0:02:22.319,0:02:24.586 Great! So now what this means is 0:02:24.587,0:02:27.326 if integer is equal to 0,[br]set the color to purple, 0:02:27.327,0:02:30.234 otherwise, if integer is equal to 1,[br]set it to yellow, 0:02:30.235,0:02:34.483 otherwise, so if both of these were false,[br]then set it to red. 0:02:34.484,0:02:37.737 I press restart a bunch of times,[br]you can see it still works. 0:02:37.738,0:02:41.823 Great! And the cool thing about this is[br]we can have as many else-if's as we want, 0:02:41.824,0:02:44.702 which makes it really easy for me[br]to add more sides to my coin. 0:02:44.703,0:02:48.008 So let's do that now: I'm going to make[br]a number between 0 and 3, 0:02:48.000,0:02:51.751 and then just add one more else block 0:02:51.760,0:02:55.914 that says else if integer is equal to 2 0:02:55.915,0:02:59.657 we'll set the fill color to... well,[br]it's red before so we can keep it red. 0:02:59.658,0:03:03.287 And then this last else block will be[br]for when an integer is equal to 3, 0:03:03.288,0:03:07.529 it will make it blue. Great! 0:03:08.479,0:03:09.585 All right. 0:03:09.586,0:03:13.115 So in order to do this, you always have[br]to start with an if statement, 0:03:13.116,0:03:16.909 and then you can have[br]as many else-if's as you want, 0:03:16.910,0:03:18.697 and then this last guy is optional. 0:03:18.698,0:03:19.927 We can actually do without it, 0:03:19.928,0:03:22.249 and then it's possible[br]that all of these will be false, 0:03:22.250,0:03:24.161 so none of these blocks gets executed. 0:03:24.162,0:03:27.203 But as long as we have that there, 0:03:27.204,0:03:30.483 then exactly one of these blocks[br]will be run. 0:03:30.484,0:03:34.016 Cool? Now this is a case[br]where it doesn't really matter 0:03:34.017,0:03:37.016 if you say if else-if else-if else, 0:03:37.017,0:03:41.928 or just use plain old if statements[br]like we had in the beginning, 0:03:41.929,0:03:45.538 so if integer is equal to 3. 0:03:45.539,0:03:47.727 And that's because it's never possible 0:03:47.728,0:03:51.197 for integer to equal 0,[br]and then also 1, 2, or 3. 0:03:51.198,0:03:54.222 Great. So these conditions[br]are all mutually exclusive. 0:03:54.223,0:03:56.608 Only one of these blocks[br]will ever get run anyway. 0:03:56.609,0:03:58.277 But that's not always the case. 0:03:58.278,0:04:01.635 What if we try to make a game[br]like this without rounding to an integer? 0:04:01.636,0:04:04.484 So I'm going to get rid of this part[br]where we round it, 0:04:04.485,0:04:06.639 and the other part[br]where we draw it to the screen. 0:04:06.640,0:04:09.283 And let's make up[br]some new rules to my game. 0:04:09.284,0:04:12.636 So I'm still generating[br]a number between 0 and 3, 0:04:12.637,0:04:15.315 put some more tick marks[br]on our number line. 0:04:16.815,0:04:22.778 So let's say that if the number falls[br]between 0 and 1, we will make it purple. 0:04:23.378,0:04:26.642 Oh, that doesn't look like a 'p' at all.[br]You get the idea. OK. 0:04:26.643,0:04:30.503 And then if it's between 1 and 2,[br]we can make it yellow. 0:04:30.504,0:04:35.532 And if it's between 2 and 3,[br]we will make it red. 0:04:35.533,0:04:39.290 Great. So let's see how we can do that[br]with if and else-if's. 0:04:39.291,0:04:43.881 So I can start out by saying[br]if number is less than 1, 0:04:43.882,0:04:46.015 so if it's less than 1, 0:04:46.016,0:04:50.116 and I know it's between 0 and 3,[br]then it must be in this range, 0:04:50.956,0:04:53.126 then I'll set the fill color to be purple. 0:04:56.066,0:05:00.182 Otherwise, if the number is less than 2, 0:05:00.844,0:05:05.324 I can set the fill color[br]to be yellow, 255... 0:05:06.702,0:05:12.183 And otherwise,[br]I'll set the fill to be red. 0:05:13.263,0:05:16.255 Great! And it works just as intended. 0:05:16.256,0:05:19.686 So if the number was less than 1,[br]then again, it's in this range. 0:05:19.687,0:05:23.602 Otherwise it's greater than or equal to 1,[br]but it's also less than 2, 0:05:23.603,0:05:26.731 so that puts it in this range,[br]so we're going to set the color to yellow. 0:05:26.732,0:05:29.859 Otherwise, it must be greater[br]than or equal to 2, 0:05:29.860,0:05:31.899 so it's going to fall in that range. 0:05:31.900,0:05:33.276 And here's a case 0:05:33.277,0:05:36.276 where we couldn't just say[br]if number is less than 2, 0:05:36.277,0:05:39.649 and if number is less than 3, 0:05:39.650,0:05:42.931 because if number is less than 1,[br]then it's definitely is less than 2 0:05:42.932,0:05:44.895 and it's also going to be less than 3. 0:05:44.896,0:05:46.780 So our coin[br]is always going to end up red. 0:05:46.781,0:05:50.781 And that's why in this case,[br]it's really helpful to have else-if.