1 00:00:00,000 --> 00:00:00,380 2 00:00:00,380 --> 00:00:04,050 >> SPEAKER 1: Let's write a program that prompts the user for int, an integer, 3 00:00:04,050 --> 00:00:06,520 and then does a bit of analysis on it, telling them whether 4 00:00:06,520 --> 00:00:08,109 it's positive or negative. 5 00:00:08,109 --> 00:00:11,880 To do this, let's plan on using the getint function in the CS50 Library, 6 00:00:11,880 --> 00:00:14,870 for which I'm going to need to include cs50.h. 7 00:00:14,870 --> 00:00:17,520 I'm going to anticipate wanting to print some things to 8 00:00:17,520 --> 00:00:18,310 the screen as well. 9 00:00:18,310 --> 00:00:22,240 So I'm also going to include standardio.h. 10 00:00:22,240 --> 00:00:24,170 >> And I'm now going to declare main as usual. 11 00:00:24,170 --> 00:00:29,030 int mainvoid, open curly brace, and preemptively close curly brace. 12 00:00:29,030 --> 00:00:31,790 I'm going to now prompt the user for that int. 13 00:00:31,790 --> 00:00:35,395 Printf, please me an in. 14 00:00:35,395 --> 00:00:38,260 And I'm now going to get that int from the user. 15 00:00:38,260 --> 00:00:42,650 Int, let's call it n, equals getint. 16 00:00:42,650 --> 00:00:45,480 >> In other words, on the right hand side of this expression, I'm going to call 17 00:00:45,480 --> 00:00:49,150 the CS50 function called getint, which is going to do exactly that. 18 00:00:49,150 --> 00:00:51,890 It's then going to return that value from the right hand side of this 19 00:00:51,890 --> 00:00:55,390 expression to the left hand side of this expression, ultimately storing 20 00:00:55,390 --> 00:00:57,960 that value in a variable called n. 21 00:00:57,960 --> 00:01:00,130 Let's now do a bit of analysis. 22 00:01:00,130 --> 00:01:06,000 >> For this, I'm going to employ a condition, or a branch, with if n is, 23 00:01:06,000 --> 00:01:09,860 say, greater than 0, then I'm going to do the following. 24 00:01:09,860 --> 00:01:13,330 Open curly brace and preemptively close curly brace. 25 00:01:13,330 --> 00:01:21,020 I'm going to then print out "you picked a positive integer." /n for 26 00:01:21,020 --> 00:01:24,490 formatting, close quote, closed parenthesis, semicolon. 27 00:01:24,490 --> 00:01:26,810 >> Else, I'm going to want to print something a little different. 28 00:01:26,810 --> 00:01:34,750 So else, open curly brace close curly brace, printf, "you picked a negative 29 00:01:34,750 --> 00:01:39,580 integer." All right, let's save and compile this program. 30 00:01:39,580 --> 00:01:42,720 Make condition zero Enter. 31 00:01:42,720 --> 00:01:47,380 I'm back at my blinking prompt dot slash, condition, zero, Enter. 32 00:01:47,380 --> 00:01:49,730 >> And let's do the simplest of sanity checks first. 33 00:01:49,730 --> 00:01:53,310 One as my int, and I indeed picked a positive integer. 34 00:01:53,310 --> 00:01:57,600 Let's run this program again with condition, zero, Enter, "please give 35 00:01:57,600 --> 00:02:00,210 me an int." Let's try two. 36 00:02:00,210 --> 00:02:02,240 I indeed picked a positive integer. 37 00:02:02,240 --> 00:02:03,620 >> Let's go negative this time. 38 00:02:03,620 --> 00:02:05,890 Dot slash, condition, zero. 39 00:02:05,890 --> 00:02:09,380 Negative 1 and I picked a negative integer. 40 00:02:09,380 --> 00:02:10,590 But I'm not done yet. 41 00:02:10,590 --> 00:02:13,000 Let's try another corner case, if you will. 42 00:02:13,000 --> 00:02:13,830 Let's try zero. 43 00:02:13,830 --> 00:02:18,560 >> Dot slash, condition, zero, Enter, and zero. 44 00:02:18,560 --> 00:02:21,550 And oh boy, I picked a negative integer. 45 00:02:21,550 --> 00:02:25,860 But I'm pretty sure zero is defined as neither positive nor negative. 46 00:02:25,860 --> 00:02:27,240 So I'm going to have to fix this. 47 00:02:27,240 --> 00:02:28,490