1 00:00:00,000 --> 00:00:00,285 2 00:00:00,285 --> 00:00:03,030 >> DAVID J. MALAN: Let's start writing some more complex conditions. 3 00:00:03,030 --> 00:00:06,530 In particular, let's write a program that prompts the user for an integer, 4 00:00:06,530 --> 00:00:09,460 say, between 1 and 10, and then does a bit of analysis. 5 00:00:09,460 --> 00:00:14,530 But this time reporting whether that number is small or medium or large in 6 00:00:14,530 --> 00:00:17,480 size, drawing those distinctions fairly arbitrarily. 7 00:00:17,480 --> 00:00:21,040 >> To do this, I'm going to rely on get int, that function from the CS50 8 00:00:21,040 --> 00:00:22,860 library that does exactly that. 9 00:00:22,860 --> 00:00:24,350 And I'm also going to leverage print f. 10 00:00:24,350 --> 00:00:28,530 So I'm going to get started by including cs50.h as 11 00:00:28,530 --> 00:00:31,880 well as standard io.h. 12 00:00:31,880 --> 00:00:36,460 >> And going to then declare main in the usual way, int main void, open curly 13 00:00:36,460 --> 00:00:38,440 brace, close curly brace. 14 00:00:38,440 --> 00:00:41,240 And I'm then going to prompt the user for an integer. 15 00:00:41,240 --> 00:00:48,340 Print f, please give me an int between 1 and 10. 16 00:00:48,340 --> 00:00:52,640 >> Now let's get that int by declaring a variable called, say, n and assigning 17 00:00:52,640 --> 00:00:55,520 it the return value of get int. 18 00:00:55,520 --> 00:00:57,300 Let's now do a bit of analysis. 19 00:00:57,300 --> 00:01:04,500 >> If n is greater than or equal to 0 and n is less than or equal to, say, 3, 20 00:01:04,500 --> 00:01:12,560 then we're going to go ahead and print out you picked a small int. 21 00:01:12,560 --> 00:01:15,730 >> Else, if the user picks, say, a medium sized value, let's 22 00:01:15,730 --> 00:01:16,960 check for that as follows. 23 00:01:16,960 --> 00:01:23,360 Else if n is greater than or equal to, say, 4 and n is less than or equal to, 24 00:01:23,360 --> 00:01:31,850 say, 7, then I'm going to print out you picked a medium int. 25 00:01:31,850 --> 00:01:36,490 >> Finally, I'm going to assume that if the value is between 8 and 10, they 26 00:01:36,490 --> 00:01:37,550 picked a large int. 27 00:01:37,550 --> 00:01:44,140 So to express that, I'll type, else if n is greater than or equal to 8 and n 28 00:01:44,140 --> 00:01:53,590 is less than or equal to 10, go ahead and print you picked a large int. 29 00:01:53,590 --> 00:01:55,720 >> Else, there's a fourth condition here. 30 00:01:55,720 --> 00:01:59,520 If the user didn't cooperate and instead typed a value that's less than 31 00:01:59,520 --> 00:02:04,430 0 or greater than 10, I want to simply reprimand them. 32 00:02:04,430 --> 00:02:12,490 Else print out, you picked an invalid int. 33 00:02:12,490 --> 00:02:13,950 >> Let's save the file. 34 00:02:13,950 --> 00:02:17,580 Compile it with make non-switch. 35 00:02:17,580 --> 00:02:21,650 Back at my prompt, I'm going to run it with dot slash non-switch. 36 00:02:21,650 --> 00:02:23,320 And let's try few values. 37 00:02:23,320 --> 00:02:26,110 >> First, let's be uncooperative and type in negative 1. 38 00:02:26,110 --> 00:02:30,970 Fortunately, that was detected by our final branch in that condition. 39 00:02:30,970 --> 00:02:36,530 Let's try again with dot slash non-switch, this time giving it 1. 40 00:02:36,530 --> 00:02:37,900 I indeed picked a small int. 41 00:02:37,900 --> 00:02:43,250 >> Let's do it again with dot slash non-switch, this time picking, say, 5. 42 00:02:43,250 --> 00:02:44,590 And that's a medium int. 43 00:02:44,590 --> 00:02:48,200 Let's now do again dot slash non-switch. 44 00:02:48,200 --> 00:02:52,650 And give it a value of 10, which is indeed a large int. 45 00:02:52,650 --> 00:02:55,310 >> Now it's worth noting that this program could have been implemented in 46 00:02:55,310 --> 00:02:56,840 any number of ways. 47 00:02:56,840 --> 00:03:00,160 First of all, it was completely arbitrary that I drew the lines that I 48 00:03:00,160 --> 00:03:03,290 did among small, medium, and large ints. 49 00:03:03,290 --> 00:03:05,230 We could have drawn those boundaries anywhere. 50 00:03:05,230 --> 00:03:08,930 >> But more interestingly, I didn't have to express myself with all of these 51 00:03:08,930 --> 00:03:13,440 greater than or equal to or less than or equal to signs. 52 00:03:13,440 --> 00:03:18,880 I could have, for instance, rewritten if n is greater than or equal to 4 and 53 00:03:18,880 --> 00:03:24,760 n is less than or equal to 7, as instead if n is greater than 3 and n 54 00:03:24,760 --> 00:03:29,130 is less than 8, then print out you picked a medium int. 55 00:03:29,130 --> 00:03:33,360 >> After all, if the user's input, by nature of get int, is an integer, we 56 00:03:33,360 --> 00:03:37,580 can either test if that value is greater than 3 or greater than or 57 00:03:37,580 --> 00:03:38,740 equal to 4. 58 00:03:38,740 --> 00:03:43,130 And we could also check if that value less than 8 or less 59 00:03:43,130 --> 00:03:44,590 than or equal to 7. 60 00:03:44,590 --> 00:03:45,860