1 00:00:00,000 --> 00:00:00,440 2 00:00:00,440 --> 00:00:03,440 >> SPEAKER 1: The last time we analyzed integers, recall that we used some 3 00:00:03,440 --> 00:00:07,810 Boolean expressions to check if a user's input n was between numbers 4 00:00:07,810 --> 00:00:11,580 like 0 and 3, 4 and 7, and 8 and 10. 5 00:00:11,580 --> 00:00:14,990 Well, we did that using if's and else if's, but it turns out you can 6 00:00:14,990 --> 00:00:17,990 implement that same logic using a different programming construct 7 00:00:17,990 --> 00:00:21,400 altogether while still achieving precisely the same result. 8 00:00:21,400 --> 00:00:25,000 In fact, we can introduce something called a switch that allows us to 9 00:00:25,000 --> 00:00:28,660 switch our behavior depending on the value of some variable. 10 00:00:28,660 --> 00:00:29,840 >> Let's give this a try. 11 00:00:29,840 --> 00:00:35,730 To do this, I'm first going to include the cs50 library by way of cs50.h. 12 00:00:35,730 --> 00:00:39,960 I'm also going to include the standard library by way of standard I/O.h. 13 00:00:39,960 --> 00:00:44,540 And I'm going to declare main in the usual way, int main void. 14 00:00:44,540 --> 00:00:45,650 Open curly brace. 15 00:00:45,650 --> 00:00:47,100 Close curly brace. 16 00:00:47,100 --> 00:00:49,720 And now I'm going to ask the user for an integer. 17 00:00:49,720 --> 00:00:56,990 >> Printf, give me an int between 1 and 10. 18 00:00:56,990 --> 00:00:59,310 And now I'm going to get that int using the cs50 19 00:00:59,310 --> 00:01:01,590 libraries function, GetInt. 20 00:01:01,590 --> 00:01:06,140 Int, let's call it n, equals GetInt. 21 00:01:06,140 --> 00:01:08,500 And now I'm going to do a bit of analysis on that integer. 22 00:01:08,500 --> 00:01:13,120 Somewhat arbitrarily, but with this new construct known as a switch. 23 00:01:13,120 --> 00:01:16,530 Switch on the value of n as follows. 24 00:01:16,530 --> 00:01:23,240 >> In the case that n equals 1, or in the case that n equals 2, or in the case 25 00:01:23,240 --> 00:01:28,150 that n equals 3, go ahead and execute this line of code. 26 00:01:28,150 --> 00:01:34,480 Printf you picked a small int, break. 27 00:01:34,480 --> 00:01:37,610 Now, I need to implement the equivalent of an ELT simple by 28 00:01:37,610 --> 00:01:40,990 enumerating some additional cases. 29 00:01:40,990 --> 00:01:47,180 >> In the case that n equals 4, or in the case that n equals 5, or in the case 30 00:01:47,180 --> 00:01:53,830 that n equals 6, or in the case that n equals 7, go ahead and print out you 31 00:01:53,830 --> 00:01:58,000 picked a medium Int. 32 00:01:58,000 --> 00:01:59,490 Break. 33 00:01:59,490 --> 00:02:02,300 Now in the case the user picked a large number, let's 34 00:02:02,300 --> 00:02:04,940 detect that as follows. 35 00:02:04,940 --> 00:02:08,900 >> In the case that the user picked eight or in the case that the user pick 36 00:02:08,900 --> 00:02:14,110 nine, or in the case that the user pick 10, go ahead and print out you 37 00:02:14,110 --> 00:02:17,830 picked a large Int. 38 00:02:17,830 --> 00:02:19,180 Break. 39 00:02:19,180 --> 00:02:23,970 ELT, if a user did not pick a number that falls into any of these 10 cases, 40 00:02:23,970 --> 00:02:25,520 let's have some default behavior. 41 00:02:25,520 --> 00:02:27,870 Which in this case will be as follows. 42 00:02:27,870 --> 00:02:32,860 >> Default, Printf, you picked an invalid Int. 43 00:02:32,860 --> 00:02:36,100 44 00:02:36,100 --> 00:02:38,220 Break. 45 00:02:38,220 --> 00:02:42,870 Now, if I save this file compile it with make switch. 46 00:02:42,870 --> 00:02:44,870 Run it with .slash switch. 47 00:02:44,870 --> 00:02:46,770 Let's do a couple of sanity checks. 48 00:02:46,770 --> 00:02:48,440 I'll pick an Int of 1. 49 00:02:48,440 --> 00:02:50,110 And ID picked a small int. 50 00:02:50,110 --> 00:02:52,240 >> Let's now do .slash switch. 51 00:02:52,240 --> 00:02:54,514 And type in say, negative 1. 52 00:02:54,514 --> 00:02:56,900 And ID picked an invalid int. 53 00:02:56,900 --> 00:02:59,560 At the end of the day, the switch construct does not enable you to do 54 00:02:59,560 --> 00:03:03,320 anything that you couldn't already do with the more familiar if, else if, 55 00:03:03,320 --> 00:03:04,530 else construct. 56 00:03:04,530 --> 00:03:07,470 But if you have a finite list of values that you're checking for, a 57 00:03:07,470 --> 00:03:09,820 switch statement may very well make your code more 58 00:03:09,820 --> 00:03:11,310 explicit, or more readable. 59 00:03:11,310 --> 00:03:12,910