1 00:00:00,000 --> 00:00:04,487 [MUSIC] 2 00:00:04,487 --> 00:00:05,454 Hopefully, by now, 3 00:00:05,454 --> 00:00:09,331 you're feeling somewhat comfortable with arithmetic operations like addition and 4 00:00:09,331 --> 00:00:13,700 division, as well as the comparison operators like greater than and equal to. 5 00:00:13,700 --> 00:00:16,440 Don't worry, we'll be getting lots more practice with those. 6 00:00:16,440 --> 00:00:18,740 But in this lesson, I'd like to teach a new and 7 00:00:18,740 --> 00:00:22,780 relatively simple flavor of operator called the logical operator. 8 00:00:22,780 --> 00:00:25,500 Which should prove quite useful when used together with 9 00:00:25,500 --> 00:00:27,550 the operators you already know. 10 00:00:27,550 --> 00:00:32,310 Namely, the logical operators we'll learn now are and, or, and not. 11 00:00:33,830 --> 00:00:36,970 Before we jump into Xcode and start learning the syntax, 12 00:00:36,970 --> 00:00:40,710 let's talk through a couple examples of where these operators come in handy. 13 00:00:40,710 --> 00:00:42,340 Let's start with or and and. 14 00:00:44,030 --> 00:00:47,370 Or, as the name suggests, is most often used for 15 00:00:47,370 --> 00:00:51,760 checking if this condition or that condition are true. 16 00:00:51,760 --> 00:00:56,020 As long as at least one of them are true, the condition is satisfied. 17 00:00:56,020 --> 00:01:00,420 So yes, if both are true, the or will still return true. 18 00:01:01,800 --> 00:01:03,065 And, as the name suggests, 19 00:01:03,065 --> 00:01:07,810 checks if this condition and that condition are both true. 20 00:01:07,810 --> 00:01:11,380 They have to both be true to meet our and condition. 21 00:01:12,880 --> 00:01:15,590 For example, let's say we're writing code to give 22 00:01:15,590 --> 00:01:18,450 online clothing shoppers certain free gifts. 23 00:01:18,450 --> 00:01:23,430 If they buy a featured shirt or spend over \$100, they get a \$10 coupon for 24 00:01:23,430 --> 00:01:24,960 their next purchase. 25 00:01:24,960 --> 00:01:28,830 Just satisfying one of the conditions, buying a shirt or 26 00:01:28,830 --> 00:01:33,150 spending over \$100, will get them that \$10 off coupon. 27 00:01:33,150 --> 00:01:36,130 However, if they buy a featured shirt and 28 00:01:36,130 --> 00:01:41,090 spend over \$100, then they get a \$25 coupon for their next purchase. 29 00:01:41,090 --> 00:01:44,480 Being able to model that type of logic is critical to programming. 30 00:01:45,590 --> 00:01:47,220 The third operator I mentioned, 31 00:01:47,220 --> 00:01:51,210 the not operator, you actually already met in a previous video. 32 00:01:51,210 --> 00:01:55,940 Remember back when we learned how to check if a value was not equal to another value 33 00:01:55,940 --> 00:01:59,270 by using an exclamation mark followed by an equal sign? 34 00:01:59,270 --> 00:02:04,370 That exclamation mark, or bang, as it's often called, is the not operator. 35 00:02:04,370 --> 00:02:07,920 Not, comes in handy all the time when it's faster or 36 00:02:07,920 --> 00:02:12,970 more intuitive to check for the absence of something or the negative of a condition. 37 00:02:12,970 --> 00:02:17,180 For instance, let's say that our software needs to first log a user in, 38 00:02:17,180 --> 00:02:19,950 then decide which of many screens to launch. 39 00:02:19,950 --> 00:02:24,010 Depending on what type of user they are and what their particular settings are. 40 00:02:24,010 --> 00:02:28,670 Well somewhere in that code, we might want to express this familiar idea. 41 00:02:28,670 --> 00:02:32,970 If the user is not logged in yet, don't bother with the launch screen. 42 00:02:32,970 --> 00:02:34,650 Just present the login screen again. 43 00:02:35,950 --> 00:02:40,030 To illustrate and or not, let's return to the example of our small movie 44 00:02:40,030 --> 00:02:42,650 theater that shows just one movie at a time. 45 00:02:42,650 --> 00:02:46,670 Much of the code we created in our old Xcode project, boxOffice, 46 00:02:46,670 --> 00:02:48,360 could definitely be refactored. 47 00:02:48,360 --> 00:02:50,540 But we'll let you keep that one for posterity. 48 00:02:50,540 --> 00:02:54,950 Instead, we'll create a new project, OS X Command Line Tool. 49 00:02:57,150 --> 00:02:58,757 And we'll name it boxOfficePlus. 50 00:03:10,796 --> 00:03:13,958 Just as before, will hop into our main.m file. 51 00:03:13,958 --> 00:03:18,290 We're gonna delete that, and we'll give ourself some space. 52 00:03:19,490 --> 00:03:21,188 Now the movie theater we're modeling, 53 00:03:21,188 --> 00:03:24,280 is just a small theater showing one movie at a time. 54 00:03:24,280 --> 00:03:26,820 But the owners have changed their business rules a bit. 55 00:03:26,820 --> 00:03:29,970 Let's see if our new logical operators, and, or, and 56 00:03:29,970 --> 00:03:32,750 not, can help us mirror their business. 57 00:03:32,750 --> 00:03:36,670 By the way, the syntax for our logical operators are short and sweet. 58 00:03:36,670 --> 00:03:39,390 So let's just list them out now, in a comment of course. 59 00:03:40,460 --> 00:03:46,910 First we have the AND operator, which is simply specified by two ampersands. 60 00:03:48,230 --> 00:03:54,150 Next, we have our OR operator, which you specify with pipes. 61 00:03:55,550 --> 00:04:00,310 By the way, on most keyboards you're gonna find the pipe by pressing shift and 62 00:04:00,310 --> 00:04:01,600 then your backslash. 63 00:04:02,750 --> 00:04:06,840 Lastly, we have the NOT operator, 64 00:04:06,840 --> 00:04:11,900 which is often called bang, and that's specified with an exclamation point. 65 00:04:13,410 --> 00:04:14,500 All right. 66 00:04:14,500 --> 00:04:17,370 Now let's talk about the business rules for our movie theater. 67 00:04:17,370 --> 00:04:22,070 The theater offers several different prices of tickets, based on a few factors. 68 00:04:22,070 --> 00:04:24,252 I'll paste them into a comment and we can talk through each one. 69 00:04:28,244 --> 00:04:32,990 Is the customer either under 13 years old, or 65 or over, 70 00:04:32,990 --> 00:04:35,950 if so, they get an "ageDiscount". 71 00:04:35,950 --> 00:04:39,250 Is the movie a matinee as in, is it the first showing of the day? 72 00:04:40,350 --> 00:04:43,190 Is the customer also a theatre employee? 73 00:04:43,190 --> 00:04:46,380 Those are questions we'll need to answer in order to figure out what price 74 00:04:46,380 --> 00:04:48,430 to charge the customer. 75 00:04:48,430 --> 00:04:51,130 Now if you already think that these conditions 76 00:04:51,130 --> 00:04:55,070 would be represented effectively with Boolean variables, then right on. 77 00:04:55,070 --> 00:04:56,920 Let's take a second to create those Booleans. 78 00:04:58,480 --> 00:05:01,282 We'll want one for ageDiscount. 79 00:05:04,887 --> 00:05:10,066 Want one for isMatinee and 80 00:05:10,066 --> 00:05:16,620 lastly one for isEmployee. 81 00:05:16,620 --> 00:05:20,020 Now rather than typing all this out, I'll paste it in a comment here and 82 00:05:20,020 --> 00:05:21,820 we can go through each one. 83 00:05:21,820 --> 00:05:24,880 First we see that full price tickets are \$10. 84 00:05:24,880 --> 00:05:30,628 For an employee with an age discount AND attending a matinee, the price is 6.50. 85 00:05:30,628 --> 00:05:34,950 For a non-employee with an age discount OR attending a matinee, but 86 00:05:34,950 --> 00:05:38,110 not both, the price would be \$8. 87 00:05:38,110 --> 00:05:43,290 For an employee attending a non-matinee, the price is 4.50. 88 00:05:43,290 --> 00:05:47,560 And for an employee attending a matinee, they get to go see the movie for free. 89 00:05:47,560 --> 00:05:50,870 Okay, now what other variables will we need? 90 00:05:50,870 --> 00:05:55,850 Well, we'll want one as an int and that's gonna the customer's age. 91 00:05:55,850 --> 00:05:59,480 And then we'll also want a float where we can store the final ticket price. 92 00:05:59,480 --> 00:06:00,320 So let's create those now. 93 00:06:01,560 --> 00:06:03,390 int Customerage. 94 00:06:05,730 --> 00:06:07,200 And made a mistake here, right? 95 00:06:07,200 --> 00:06:09,970 We started our variable with an uppercase letter, but 96 00:06:09,970 --> 00:06:12,190 a variable should be a lowercase letter. 97 00:06:12,190 --> 00:06:15,600 Or rather, an uppercase letter would specify a class, and 98 00:06:15,600 --> 00:06:18,090 here we're just creating a variable. 99 00:06:18,090 --> 00:06:23,672 And then we're gonna make float customerPrice. 100 00:06:23,672 --> 00:06:25,840 Okay. 101 00:06:25,840 --> 00:06:30,520 So it seems we have the variables we need, so let's proceed with the logic. 102 00:06:30,520 --> 00:06:35,430 First, let's figure out if our customer will qualify for the age discount. 103 00:06:35,430 --> 00:06:38,130 For this, we'll want to create an if statement. 104 00:06:38,130 --> 00:06:43,280 Specifically, if the customer's age is less than 13 or greater than or 105 00:06:43,280 --> 00:06:46,060 equal to 65, then they would qualify. 106 00:06:47,130 --> 00:06:49,420 Take a second, jot it down, or 107 00:06:49,420 --> 00:06:52,830 just imagine it in your head, what that conditional statement would look like. 108 00:06:54,530 --> 00:06:57,620 Okay, hopefully you came up with something. 109 00:06:57,620 --> 00:07:03,060 The way we would write this is, if, and we're going to create our parentheses and 110 00:07:03,060 --> 00:07:05,000 we'll just create our curly bases first here. 111 00:07:06,260 --> 00:07:11,650 Now, inside our parentheses we're gonna say we need one condition and 112 00:07:11,650 --> 00:07:16,170 that's gonna be customerAge is less than 13. 113 00:07:16,170 --> 00:07:20,994 We can close that first parenthetical statement, 114 00:07:20,994 --> 00:07:25,030 and then we're gonna use the pipes to say or 115 00:07:25,030 --> 00:07:30,639 then another parenthetical statement, and inside that, 116 00:07:30,639 --> 00:07:35,940 we're gonna say, customerAge has gotta be >= 65. 117 00:07:35,940 --> 00:07:40,830 Inside our curly braces, we're gonna say if this or 118 00:07:40,830 --> 00:07:44,857 this is met, then we're gonna give them the ageDiscount. 119 00:07:47,370 --> 00:07:49,130 Let's look back over that. 120 00:07:49,130 --> 00:07:53,490 You see, up here, we have two parenthetical statements, that are joined 121 00:07:53,490 --> 00:07:58,730 with an or, and they're contained within one larger parenthetical statement. 122 00:07:58,730 --> 00:08:01,350 Just like when you perform mathematical operations, 123 00:08:01,350 --> 00:08:04,610 we begin with the inner parenthesis. 124 00:08:04,610 --> 00:08:10,570 If one or the other, or both, return true, then the entire parenthetical 125 00:08:10,570 --> 00:08:15,630 statement will return true and will proceed to these curly braces. 126 00:08:16,730 --> 00:08:21,520 By the way, we would say that these two statements are nested inside the larger 127 00:08:21,520 --> 00:08:25,560 statement and nesting is something you'll see a lot of in future videos. 128 00:08:25,560 --> 00:08:29,400 Now before we go any further, are any alarm bells going off? 129 00:08:29,400 --> 00:08:30,880 They should be. 130 00:08:30,880 --> 00:08:36,870 Right here and right here, we have two so-called magic numbers. 131 00:08:36,870 --> 00:08:39,556 Having these hard-coded numbers in your code is a no-no. 132 00:08:39,556 --> 00:08:44,240 Let's refactor those out and create our own variables for them. 133 00:08:44,240 --> 00:08:50,300 So first, we could create int youthAge and we'll set that equal to 13. 134 00:08:50,300 --> 00:08:54,830 And then int seniorAge. 135 00:08:54,830 --> 00:08:57,350 We can set that equal to 65. 136 00:08:57,350 --> 00:09:02,430 Then down here, we can simply 137 00:09:02,430 --> 00:09:06,585 place our variables in our conditional statements. 138 00:09:06,585 --> 00:09:09,700 A-ha, much, much better. 139 00:09:09,700 --> 00:09:13,870 In our next video, we'll continue right here, and see how we can use this code and 140 00:09:13,870 --> 00:09:16,740 our shiny new operators to determine the ticket price.