1 00:00:00,000 --> 00:00:05,140 2 00:00:05,140 --> 00:00:05,640 All right. 3 00:00:05,640 --> 00:00:08,330 So now let's talk about something really useful in programming-- 4 00:00:08,330 --> 00:00:09,914 conditional statements. 5 00:00:09,914 --> 00:00:11,830 So conditional expressions allow your programs 6 00:00:11,830 --> 00:00:14,538 to make decisions and take different forks in the road, something 7 00:00:14,538 --> 00:00:17,670 I alluded to little earlier, depending on the values of variables, 8 00:00:17,670 --> 00:00:20,990 or based on what the user inputs at the programmer, at the command line, 9 00:00:20,990 --> 00:00:23,130 or if you have a prompt or something like that. 10 00:00:23,130 --> 00:00:26,690 C has a couple of different ways to express conditional expressions, which 11 00:00:26,690 --> 00:00:29,911 we also sometimes will call a conditional branch in your programs. 12 00:00:29,911 --> 00:00:32,910 And some of these are going to look pretty familiar to you from scratch, 13 00:00:32,910 --> 00:00:35,190 so we'll even pull them up side by side, just 14 00:00:35,190 --> 00:00:38,170 you can make that analogy in your head. 15 00:00:38,170 --> 00:00:41,250 >> So, if-- if is a pretty simple conditional. 16 00:00:41,250 --> 00:00:43,560 If you recall from scratch on the right here 17 00:00:43,560 --> 00:00:50,000 you could fill in that is hexagon with a blue expression if mouse down 18 00:00:50,000 --> 00:00:53,010 or if x is less than 10, or something like that. 19 00:00:53,010 --> 00:00:57,390 And then, if x was less than 10, or if the mouse was in fact, down, 20 00:00:57,390 --> 00:01:01,140 all of the code inside of the puzzle piece would execute. 21 00:01:01,140 --> 00:01:03,720 All the things that fit inside that C shape. 22 00:01:03,720 --> 00:01:07,200 >> Similarly, do we have if on the left there. 23 00:01:07,200 --> 00:01:09,210 If Boolean expression, which I'm just using 24 00:01:09,210 --> 00:01:13,010 as a substitute for one of the Boolean expressions we previously discussed, 25 00:01:13,010 --> 00:01:15,240 open curly brace, close curly brace. 26 00:01:15,240 --> 00:01:17,720 So think of open curly brace and closed curly brace 27 00:01:17,720 --> 00:01:22,720 as sort of analogous to the sandwich effect of the if block on the right 28 00:01:22,720 --> 00:01:24,070 from scratch. 29 00:01:24,070 --> 00:01:28,680 >> If the Boolean expression in the if statement is true, 30 00:01:28,680 --> 00:01:30,900 then all the lines of code between the curly braces 31 00:01:30,900 --> 00:01:33,870 will execute in order from top to bottom. 32 00:01:33,870 --> 00:01:35,700 If the Boolean expression is false, we'll 33 00:01:35,700 --> 00:01:38,610 skip over everything in between the curly braces, because we only 34 00:01:38,610 --> 00:01:44,570 want to go down that fork in the road if the Boolean expression is true. 35 00:01:44,570 --> 00:01:48,540 >> We can take this one step further with if else. 36 00:01:48,540 --> 00:01:50,820 So this Scratch block is pretty similar to the one 37 00:01:50,820 --> 00:01:55,884 we saw just a second ago, except it takes two different paths based 38 00:01:55,884 --> 00:01:56,550 on what happens. 39 00:01:56,550 --> 00:02:00,420 So if the mouse was down, or if x was less than 10, 40 00:02:00,420 --> 00:02:04,780 we'll do everything that's in between that first fork, that first C. , 41 00:02:04,780 --> 00:02:08,430 >> Otherwise, if the mouse is up, or x is not less than 10, 42 00:02:08,430 --> 00:02:10,460 we will do everything in the second set. 43 00:02:10,460 --> 00:02:15,010 And that's analogous to what you see here for C. If Boolean expression, 44 00:02:15,010 --> 00:02:17,910 do the stuff between the first set of curly braces. 45 00:02:17,910 --> 00:02:20,550 Else, do the stuff between the second set of curly braces. 46 00:02:20,550 --> 00:02:22,080 So if the Boolean expression is true, we'll 47 00:02:22,080 --> 00:02:23,580 do whatever's between the first set. 48 00:02:23,580 --> 00:02:27,480 If the Boolean expression is false, that would trigger the else, 49 00:02:27,480 --> 00:02:30,100 and we would do whatever's in the second set of curly braces. 50 00:02:30,100 --> 00:02:34,190 Again, top to bottom, all lines in between the braces. 51 00:02:34,190 --> 00:02:38,130 >> In C, it's possible to create an if-else if-else chain. 52 00:02:38,130 --> 00:02:42,000 In fact you can have if-else if-else if-else, if, and so on and so on and so 53 00:02:42,000 --> 00:02:42,720 on. 54 00:02:42,720 --> 00:02:44,660 In Scratch, this required nesting the blocks. 55 00:02:44,660 --> 00:02:48,280 You add an if-else, and you had to put another one inside of the else, 56 00:02:48,280 --> 00:02:51,110 and so on, and it got kind of nested and complicated. 57 00:02:51,110 --> 00:02:52,450 But C, we don't have to do that. 58 00:02:52,450 --> 00:02:55,300 We can actually just have it be a chain like this. 59 00:02:55,300 --> 00:02:58,350 Again, as you might expect, all of these branches are mutually exclusive. 60 00:02:58,350 --> 00:03:00,750 You can only ever go down one of the branch. 61 00:03:00,750 --> 00:03:02,270 If this is true. 62 00:03:02,270 --> 00:03:03,930 Otherwise, if this is true. 63 00:03:03,930 --> 00:03:05,700 Otherwise, if this is true. 64 00:03:05,700 --> 00:03:07,120 Otherwise, do this. 65 00:03:07,120 --> 00:03:11,010 So all four of the branches in this example are mutually exclusive. 66 00:03:11,010 --> 00:03:14,900 It's an if-else if-else chain. 67 00:03:14,900 --> 00:03:17,580 >> It is possible though, and sometimes very useful, 68 00:03:17,580 --> 00:03:20,950 to create a chain of not mutually exclusive branches. 69 00:03:20,950 --> 00:03:24,600 In this example, only the third and fourth branches are mutually exclusive. 70 00:03:24,600 --> 00:03:27,450 It could be that you could satisfy the first condition, 71 00:03:27,450 --> 00:03:29,396 and you could satisfy the second condition, 72 00:03:29,396 --> 00:03:31,770 and you could satisfy the third condition-- in which case 73 00:03:31,770 --> 00:03:35,270 you would go down the first branch, then you go down a second branch, 74 00:03:35,270 --> 00:03:37,000 then you would go down the third branch. 75 00:03:37,000 --> 00:03:40,450 Or perhaps you satisfy the first condition, and the second condition, 76 00:03:40,450 --> 00:03:42,770 but you don't satisfy the third condition. 77 00:03:42,770 --> 00:03:46,230 In this case you go down the first branch and the second branch, 78 00:03:46,230 --> 00:03:48,040 and then the fourth branch, 79 00:03:48,040 --> 00:03:51,392 >> The reason for this is that the else will only bind to the nearest if. 80 00:03:51,392 --> 00:03:53,100 So even though there's an else here, that 81 00:03:53,100 --> 00:03:56,490 doesn't necessarily create a mutually exclusive chain of everything. 82 00:03:56,490 --> 00:04:00,890 It's only the expression there with Boolean 83 00:04:00,890 --> 00:04:05,040 expression 3-- that's the mutually exclusive with the else. 84 00:04:05,040 --> 00:04:07,580 So it is possible, and sometimes quite useful, 85 00:04:07,580 --> 00:04:11,772 as I said, to create a chain of not mutually exclusive branches. 86 00:04:11,772 --> 00:04:14,230 Let's take a look at a different kind of conditional, which 87 00:04:14,230 --> 00:04:17,392 you have not seen before in Scratch. 88 00:04:17,392 --> 00:04:19,369 There's something called the switch statement. 89 00:04:19,369 --> 00:04:21,410 The switch statement is kind of neat because it's 90 00:04:21,410 --> 00:04:25,930 a conditional statement that allows you to specify distinct cases, 91 00:04:25,930 --> 00:04:28,926 instead of relying on Boolean expressions to make decisions for you. 92 00:04:28,926 --> 00:04:31,050 So for example, let's say that I have this program, 93 00:04:31,050 --> 00:04:34,110 and I'm asking the user to provide input to me. 94 00:04:34,110 --> 00:04:37,170 So I say, int x = Get Int(), and if you're not familiar yet, 95 00:04:37,170 --> 00:04:40,190 get int is a function that is also included in the CS50 library, 96 00:04:40,190 --> 00:04:44,610 so if you #include CS50.H you'll have access to Get Int() and all of its 97 00:04:44,610 --> 00:04:46,840 cousins-- GetFloat, GetString, and so on. 98 00:04:46,840 --> 00:04:52,590 Basically one Get function for every data type that we've already discussed. 99 00:04:52,590 --> 00:04:53,970 >> So Int x equals GetInt. 100 00:04:53,970 --> 00:04:56,390 Basically what's happening is I'm at the terminal. 101 00:04:56,390 --> 00:04:58,790 I'm asking the user to type in a number. 102 00:04:58,790 --> 00:05:02,300 >> And here I'm switching what I'm doing, depending 103 00:05:02,300 --> 00:05:05,060 on what the user typed at the prompt. 104 00:05:05,060 --> 00:05:09,147 So if they typed one, I print out one. 105 00:05:09,147 --> 00:05:09,855 And then I break. 106 00:05:09,855 --> 00:05:12,590 107 00:05:12,590 --> 00:05:15,510 If they type two, I print out two. 108 00:05:15,510 --> 00:05:16,690 And then I break. 109 00:05:16,690 --> 00:05:19,060 It's important to break between each case 110 00:05:19,060 --> 00:05:20,890 because otherwise you will fall through. 111 00:05:20,890 --> 00:05:23,380 So if I didn't have any breaks there, and the user 112 00:05:23,380 --> 00:05:31,380 typed one, what would happen is it would print one, two, three, sorry. 113 00:05:31,380 --> 00:05:33,099 That's kind of strange behavior, right? 114 00:05:33,099 --> 00:05:33,890 You might think so. 115 00:05:33,890 --> 00:05:36,480 But there are actually some cases where this could be a pretty useful thing. 116 00:05:36,480 --> 00:05:39,730 So here's another example of a switch statement where I omit the breaks. 117 00:05:39,730 --> 00:05:42,030 But I do it on purpose. 118 00:05:42,030 --> 00:05:43,030 >> So what happens here? 119 00:05:43,030 --> 00:05:43,821 Think for a second. 120 00:05:43,821 --> 00:05:45,960 You may even want to pause the video. 121 00:05:45,960 --> 00:05:48,230 >> What happens here if the user types four? 122 00:05:48,230 --> 00:05:51,190 123 00:05:51,190 --> 00:05:53,860 So I've asked the user for input. 124 00:05:53,860 --> 00:05:56,560 And they provide the value 4. 125 00:05:56,560 --> 00:05:59,545 What gets printed when I do that? 126 00:05:59,545 --> 00:06:02,170 On the previous slide, there were breaks between all the cases. 127 00:06:02,170 --> 00:06:04,750 And so it would just print four and then stop. 128 00:06:04,750 --> 00:06:06,610 But in this case, it won't. 129 00:06:06,610 --> 00:06:10,700 What will happen is you will fall through each case. 130 00:06:10,700 --> 00:06:14,890 >> So in this case I've organized my cases in such a way that if the user types 4, 131 00:06:14,890 --> 00:06:20,070 I will print four, three, two, one, blast off. 132 00:06:20,070 --> 00:06:22,780 And if they typed 5, I would start at five and do the same thing. 133 00:06:22,780 --> 00:06:26,410 If they typed 1, I would just do one, blast off. 134 00:06:26,410 --> 00:06:28,715 >> So in this case, I'm using a switch kind of cleverly so 135 00:06:28,715 --> 00:06:30,804 that I do intend to fall through all the cases. 136 00:06:30,804 --> 00:06:33,720 But generally you're probably gonna want to break between all of them, 137 00:06:33,720 --> 00:06:36,090 unless you have a situation like this one where you're 138 00:06:36,090 --> 00:06:40,081 kind of leveraging the fact that you'll fall through the cases without a break. 139 00:06:40,081 --> 00:06:42,830 So that's the second of the major types of conditional statements. 140 00:06:42,830 --> 00:06:47,139 The last of which is ?: So I have two snippets of C code here. 141 00:06:47,139 --> 00:06:48,680 One on the left and one on the right. 142 00:06:48,680 --> 00:06:52,330 The one on the left should probably be pretty familiar to you. 143 00:06:52,330 --> 00:06:55,110 >> I have Int x. 144 00:06:55,110 --> 00:06:57,167 And I probably should have asked the user 145 00:06:57,167 --> 00:07:00,250 for-- this should probably be Int x equals GetInt, or something like that. 146 00:07:00,250 --> 00:07:03,030 147 00:07:03,030 --> 00:07:05,240 And then I'm making a decision. 148 00:07:05,240 --> 00:07:11,700 If some Boolean expression is true, assign x the value 5. 149 00:07:11,700 --> 00:07:13,590 Otherwise, assign x the value 6. 150 00:07:13,590 --> 00:07:16,548 >> That on the left should probably be pretty familiar from our discussion 151 00:07:16,548 --> 00:07:18,160 of If Else just a moment ago. 152 00:07:18,160 --> 00:07:20,535 Would you be surprised to know that the line on the right 153 00:07:20,535 --> 00:07:22,310 does the exact same thing? 154 00:07:22,310 --> 00:07:26,140 >> So this is called ?: or sometimes called the ternary operator. 155 00:07:26,140 --> 00:07:27,450 And it's pretty cool. 156 00:07:27,450 --> 00:07:29,110 It's usually used as a cute trick. 157 00:07:29,110 --> 00:07:35,777 >> But what it allows you to do is to simulate an If Else with really small, 158 00:07:35,777 --> 00:07:37,610 really trivially short conditional branches. 159 00:07:37,610 --> 00:07:41,470 You generally wouldn't use ?: if you had six lines of code between each set 160 00:07:41,470 --> 00:07:42,569 of curly braces. 161 00:07:42,569 --> 00:07:44,360 But if you're just making a quick decision, 162 00:07:44,360 --> 00:07:47,520 if you're going to do one thing or the other and it's very simple, 163 00:07:47,520 --> 00:07:52,240 this might be an example of how to do it with ?: the ternary operator. 164 00:07:52,240 --> 00:07:56,940 So Int x equals expression ? 165 00:07:56,940 --> 00:07:59,470 The thing after the question mark is what x's value 166 00:07:59,470 --> 00:08:02,690 will be if expression is true. 167 00:08:02,690 --> 00:08:05,330 >> The thing after the colon is what x's value 168 00:08:05,330 --> 00:08:07,990 would be if the expression was false. 169 00:08:07,990 --> 00:08:11,510 So I'm asking myself, is the expression true? 170 00:08:11,510 --> 00:08:13,870 If it is, assign x the value 5. 171 00:08:13,870 --> 00:08:16,619 If it's not, assign x the value 6. 172 00:08:16,619 --> 00:08:17,410 Again, like I said. 173 00:08:17,410 --> 00:08:18,670 This is usually just a cute trick. 174 00:08:18,670 --> 00:08:20,430 And sometimes if you become really comfortable with it, 175 00:08:20,430 --> 00:08:22,820 you'll do this because it looks kind of cool in your programs. 176 00:08:22,820 --> 00:08:25,710 Generally I'm presenting it to you now so you're familiar with it 177 00:08:25,710 --> 00:08:26,990 if you see it. 178 00:08:26,990 --> 00:08:30,080 But certainly know you don't have to write it in any of your code. 179 00:08:30,080 --> 00:08:33,246 But it is something to be familiar with, because you'll definitely encounter 180 00:08:33,246 --> 00:08:36,130 snippets of code here and there where this ?: syntax, 181 00:08:36,130 --> 00:08:39,120 AKA the ternary operator, is used. 182 00:08:39,120 --> 00:08:40,960 >> So quick summary on what conditionals are, 183 00:08:40,960 --> 00:08:44,210 and what the options are available to you in C. You have If and if-else, 184 00:08:44,210 --> 00:08:46,860 and if else if, et cetera. 185 00:08:46,860 --> 00:08:50,880 You can use Boolean expressions for those to make decisions. 186 00:08:50,880 --> 00:08:53,720 >> With switch statements you use discrete cases to make decisions. 187 00:08:53,720 --> 00:08:57,540 You would specifically say, if it's one, or if it's two, or if it's three, 188 00:08:57,540 --> 00:09:00,870 I'll do this thing, or this thing, or this thing. 189 00:09:00,870 --> 00:09:04,660 And ?: can to be used to replace very simple if-else branches, 190 00:09:04,660 --> 00:09:08,490 or if-else chains to make your code look a little fancy. 191 00:09:08,490 --> 00:09:09,250 >> I'm Doug Lloyd. 192 00:09:09,250 --> 00:09:11,410 And this is CS50. 193 00:09:11,410 --> 00:09:12,959