1 00:00:00,430 --> 00:00:03,540 In our last video, we learned how to add, subtract, multiply, and 2 00:00:03,540 --> 00:00:05,320 divide in objective C. 3 00:00:05,320 --> 00:00:06,330 As you can imagine, 4 00:00:06,330 --> 00:00:10,210 those arithmetic operators play a role in nearly any piece of software. 5 00:00:10,210 --> 00:00:13,170 Just as essential, however, are the comparison operators. 6 00:00:13,170 --> 00:00:18,030 Which as the name suggests, allow you to compare two values, and get a result. 7 00:00:18,030 --> 00:00:21,540 However, unlike the numerical results you would get by doing arithmetic, 8 00:00:21,540 --> 00:00:25,190 a comparison either returns a true or false. 9 00:00:25,190 --> 00:00:26,750 Yes or no. 10 00:00:26,750 --> 00:00:30,550 You're probably already family with some comparisons like greater than or less. 11 00:00:30,550 --> 00:00:37,810 But let's spend a few minutes exploring the other common comparison operations. 12 00:00:38,840 --> 00:00:42,500 The first four will probably be quite familiar to you from childhood math class, 13 00:00:42,500 --> 00:00:44,600 and the last two may be new to you. 14 00:00:44,600 --> 00:00:46,340 I'm back in my math ops project, 15 00:00:46,340 --> 00:00:50,330 by the way, which is what we use to practice our arithmetical operations. 16 00:00:50,330 --> 00:00:53,350 I'll just comment out the old code so it doesn't get in the way. 17 00:00:56,000 --> 00:01:01,020 And then I'll give us some space down here, and I'll also 18 00:01:01,020 --> 00:01:05,580 paste in the operations will be working on just do we have them for reference. 19 00:01:05,580 --> 00:01:07,320 But I'll put those in the comment too. 20 00:01:07,320 --> 00:01:09,440 But don't worry, we'll go through them one by one. 21 00:01:09,440 --> 00:01:13,160 Earlier, I mentioned that comparisons don't return numerical results but 22 00:01:13,160 --> 00:01:15,160 rather a yes or no. 23 00:01:15,160 --> 00:01:19,300 Which means, we're gonna need a particular type of variable to handle the result. 24 00:01:19,300 --> 00:01:23,260 And I hope that before I even finish that sentence, you were shouting bool 25 00:01:23,260 --> 00:01:25,990 at your screen, since that's exactly what we're gonna need. 26 00:01:25,990 --> 00:01:32,030 So, let's create a bool and we're gonna call it compareResult. 27 00:01:36,190 --> 00:01:38,253 Now let's try some comparisons. 28 00:01:38,253 --> 00:01:43,096 First we'll try greater than, for 29 00:01:43,096 --> 00:01:50,046 which we can simply type compareResult = 5>3. 30 00:01:50,046 --> 00:01:56,100 To translate that from computer to human, we're saying is five greater than three? 31 00:01:56,100 --> 00:01:59,990 If true, set compare result to yes. 32 00:01:59,990 --> 00:02:01,920 If not set compare result to no. 33 00:02:03,190 --> 00:02:09,173 We can put a break point right here, Command + R, to run our code. 34 00:02:09,173 --> 00:02:13,539 And, if we mouse over, we see compare 35 00:02:13,539 --> 00:02:18,047 result is true, 5 is greater than 3. 36 00:02:18,047 --> 00:02:20,080 Now let's try flipping our symbol. 37 00:02:21,290 --> 00:02:23,160 I also actually like some space in here, so 38 00:02:23,160 --> 00:02:27,650 I'm gonna put a space, and then that, I find that easier to read. 39 00:02:27,650 --> 00:02:30,297 Let's run our code again, command + R. 40 00:02:30,297 --> 00:02:36,462 We mouse over compare result false, 5 is definitely not less than 3. 41 00:02:36,462 --> 00:02:40,571 We can take that result a step further and change our expression to a less than or 42 00:02:40,571 --> 00:02:41,780 equal to. 43 00:02:41,780 --> 00:02:44,200 Simply by adding an equals sign. 44 00:02:44,200 --> 00:02:45,700 This can look a little funny. 45 00:02:45,700 --> 00:02:47,040 But it actually makes sense. 46 00:02:47,040 --> 00:02:50,140 You just read it less than or equal to. 47 00:02:50,140 --> 00:02:50,890 We'll run that again. 48 00:02:52,770 --> 00:02:55,600 Mouse over compareResult, false. 49 00:02:55,600 --> 00:02:58,790 Five is definitely not less than or equal to three. 50 00:03:00,040 --> 00:03:01,296 If we flip our operator. 51 00:03:03,848 --> 00:03:11,420 To greater than or equal to three and run it again, we see we get true. 52 00:03:11,420 --> 00:03:14,920 Five most definitely is greater than three. 53 00:03:14,920 --> 00:03:19,337 Now just for kicks, let's change our three to a five and run it again. 54 00:03:22,255 --> 00:03:26,130 Indeed we got true because 5 = 5. 55 00:03:26,130 --> 00:03:33,463 Just to be sure, let's change this 5 to a 50, and we'll switch the sign. 56 00:03:36,850 --> 00:03:37,860 False. 57 00:03:37,860 --> 00:03:42,220 50 is neither less than nor equal to 5. 58 00:03:42,220 --> 00:03:46,120 Okay, we got four down, and two to go. 59 00:03:46,120 --> 00:03:50,860 Next, we have the double equal sign, which may look a little confusing at first, 60 00:03:50,860 --> 00:03:52,180 sorta like a typo. 61 00:03:52,180 --> 00:03:54,610 But in actuality, it's pretty simple. 62 00:03:54,610 --> 00:04:00,530 So to do that, let's erase what we have here and we'll say 5 == 5. 63 00:04:00,530 --> 00:04:04,980 Remember, the double equal sign is comparing two numbers, and 64 00:04:04,980 --> 00:04:07,440 then determining if they're equal or not. 65 00:04:07,440 --> 00:04:10,510 Remember, a single equals sign assigns a value. 66 00:04:10,510 --> 00:04:12,520 It's called the assignment operator. 67 00:04:12,520 --> 00:04:17,900 A double equals sign, like we have here, compares two values for equality. 68 00:04:17,900 --> 00:04:23,860 Let's run this. 69 00:04:23,860 --> 00:04:25,712 True, 5 does equal 5. 70 00:04:25,712 --> 00:04:27,629 Now let's change it a bit. 71 00:04:33,212 --> 00:04:37,836 False, 5 does not equal 5.75. 72 00:04:37,836 --> 00:04:41,400 Okay, last up the not equal operator. 73 00:04:41,400 --> 00:04:45,550 As you might guess, this decides if two values are not equal and if so, 74 00:04:45,550 --> 00:04:46,940 returns yes. 75 00:04:46,940 --> 00:04:50,660 This is written simply as an exclamation point followed by an equal sign. 76 00:04:52,350 --> 00:04:57,870 If we run that, we'll see that we get true, 77 00:04:57,870 --> 00:05:02,050 5 is not equal to 5.75. 78 00:05:02,050 --> 00:05:08,580 In English, we could read this whole line as, is 5 not equal to 5.75? 79 00:05:08,580 --> 00:05:12,010 If so, return true. 80 00:05:12,010 --> 00:05:13,470 If not, return false. 81 00:05:13,470 --> 00:05:18,060 In our case, it's gonna return true, because these two numbers are not equal. 82 00:05:18,060 --> 00:05:23,790 By the way, that exclamation mark is often referred to as a bang in programming. 83 00:05:23,790 --> 00:05:26,230 That's this exclamation mark right here. 84 00:05:26,230 --> 00:05:28,520 Though unlike clever jargon, like camelcase or 85 00:05:28,520 --> 00:05:32,620 breadcrumb, the slang wasn't created or popularized by programmers. 86 00:05:32,620 --> 00:05:37,180 In actuality, it was popularized by the female secretaries of the 1950s, 87 00:05:37,180 --> 00:05:40,040 who often used the term bang when typing in shorthand. 88 00:05:41,050 --> 00:05:43,610 Hopefully much of what we just learned seemed familiar or 89 00:05:43,610 --> 00:05:45,120 at least intuitive to you. 90 00:05:45,120 --> 00:05:46,780 But if not, don't despair. 91 00:05:46,780 --> 00:05:50,640 Using comparison operators like less than or equal to is something you'll get 92 00:05:50,640 --> 00:05:54,260 plenty of practice with as we work through more and more code examples. 93 00:05:54,260 --> 00:05:57,670 That said, if you feel like you need a good cheat sheet on hand, 94 00:05:57,670 --> 00:05:59,100 I've linked one in the teacher's notes.