1 00:00:00,340 --> 00:00:02,460 If a user is ordering 100 or 2 00:00:02,460 --> 00:00:07,400 more widgets, we set the price per unit variable to 8 as we should. 3 00:00:07,400 --> 00:00:10,340 But then we overwrite price per unit with a value 4 00:00:10,340 --> 00:00:15,040 of 9 because a quantity of 100 is also greater than the quantity of 50. 5 00:00:15,040 --> 00:00:19,390 We need to test whether quantity is greater than or equal to 50 and 6 00:00:19,390 --> 00:00:23,710 less than 100 before we apply a price per unit of 9. 7 00:00:23,710 --> 00:00:26,515 We can do that using boolean operators. 8 00:00:26,515 --> 00:00:31,823 [SOUND] Just like math operators return a new number based on other numbers, 9 00:00:31,823 --> 00:00:37,315 boolean operators return a new boolean value based on other boolean values. 10 00:00:37,315 --> 00:00:41,406 The Or operator returns true as the boolean value to its left or 11 00:00:41,406 --> 00:00:44,850 the boolean value to its right are true. 12 00:00:44,850 --> 00:00:49,010 The And operator returns true if the Boolean value to its left and 13 00:00:49,010 --> 00:00:50,810 the Boolean value to its right are true. 14 00:00:51,810 --> 00:00:56,140 And the Not operator returns the opposite of the Boolean value to its right, 15 00:00:56,140 --> 00:00:59,430 that is true becomes false and false becomes true. 16 00:01:00,690 --> 00:01:04,790 Let's launch IRB to experiment with some Boolean operators. 17 00:01:04,790 --> 00:01:07,704 Here is how Boolean values appear when you type them in IRB. 18 00:01:07,704 --> 00:01:10,860 If you type the value true, the result is going to be true. 19 00:01:10,860 --> 00:01:14,540 If you type the value false, the result is going to be false. 20 00:01:14,540 --> 00:01:17,600 Now, let's try changing those up, using Boolean operators. 21 00:01:17,600 --> 00:01:19,505 We'll start with the Or operator. 22 00:01:19,505 --> 00:01:24,158 So, if you type the Boolean value true and then use the Or 23 00:01:24,158 --> 00:01:28,940 operator to combine that with the value false. 24 00:01:28,940 --> 00:01:32,940 The result is going to be true, because it tests whether either the first value or 25 00:01:32,940 --> 00:01:34,470 the second value is true. 26 00:01:34,470 --> 00:01:38,090 And if either of them is true, the final result is true. 27 00:01:38,090 --> 00:01:41,890 You get the same result if you try typing false or true. 28 00:01:43,480 --> 00:01:47,720 Well, the second value is true, therefore the result of the Or operator is true. 29 00:01:47,720 --> 00:01:50,180 The same holds if both values are true. 30 00:01:50,180 --> 00:01:53,330 If we type true or true, the result is true. 31 00:01:53,330 --> 00:01:56,770 But if both values on both sides are false, 32 00:01:58,680 --> 00:02:01,810 then the result of the Or operator is going to be false. 33 00:02:01,810 --> 00:02:05,275 Now once again, you're probably not going to be using Boolean operators with 34 00:02:05,275 --> 00:02:07,610 hard-coded Boolean values too often. 35 00:02:07,610 --> 00:02:11,520 You're probably going to be generating your Boolean values through comparison 36 00:02:11,520 --> 00:02:12,360 operators. 37 00:02:12,360 --> 00:02:15,400 So let's try the results of a couple of comparisons. 38 00:02:15,400 --> 00:02:19,684 We'll test whether 2 is < 3 or 4 is > 5. 39 00:02:19,684 --> 00:02:22,663 Well the first comparison is true and 40 00:02:22,663 --> 00:02:26,611 therefore the result of the Or operator is true. 41 00:02:26,611 --> 00:02:31,381 Now let's try it with 42 00:02:31,381 --> 00:02:35,625 2 > 3 or 4 < 5. 43 00:02:35,625 --> 00:02:40,084 Well 2 isn't greater than 3, so the first boolean value is false. 44 00:02:40,084 --> 00:02:45,270 But 4 is less than 5, so the second value is true, and therefore the result is true. 45 00:02:45,270 --> 00:02:50,660 Finally, let's try it with a test whether 2 is greater than 3, 46 00:02:50,660 --> 00:02:53,070 or 4 is greater than 5. 47 00:02:53,070 --> 00:02:57,750 Neither of those is true, and therefore the result of the Or operator is false. 48 00:02:58,870 --> 00:03:01,660 And again, you're probably not going to be working with hard-coded 49 00:03:01,660 --> 00:03:03,220 numbers very often either. 50 00:03:03,220 --> 00:03:06,060 You're probably going to be working with values in variables. 51 00:03:06,060 --> 00:03:09,763 Let's set a quantity variable that's equal to 75. 52 00:03:09,763 --> 00:03:16,535 If we were to test whether quantity is less than 50, that would be false. 53 00:03:16,535 --> 00:03:20,265 If we were to test whether quantity is greater than 100, 54 00:03:20,265 --> 00:03:22,685 that would also be false. 55 00:03:22,685 --> 00:03:25,674 Because quantity is currently set to 75. 56 00:03:25,674 --> 00:03:30,332 And if we combine those two comparisons with an Or 57 00:03:30,332 --> 00:03:33,966 operator, quantity less than 50 or 58 00:03:33,966 --> 00:03:39,460 quantity, greater than 100. 59 00:03:39,460 --> 00:03:44,230 That too is going to be false because both of the Boolean values are false. 60 00:03:44,230 --> 00:03:48,132 If however we change quantity to 25 and 61 00:03:48,132 --> 00:03:52,272 then run the exact same comparison again. 62 00:03:52,272 --> 00:03:55,870 This time it's true, 25 is less than 50. 63 00:03:55,870 --> 00:03:58,370 Therefore, the first Boolean value is true. 64 00:03:58,370 --> 00:04:01,280 And therefore the result of the Or operator is true. 65 00:04:01,280 --> 00:04:06,146 If we change quantity, to 125, 66 00:04:06,146 --> 00:04:11,230 and run the exact same comparison, that too is true. 67 00:04:11,230 --> 00:04:14,160 The first Boolean value is false, but the second is true. 68 00:04:14,160 --> 00:04:17,230 Therefore the results of the Or operator is true. 69 00:04:17,230 --> 00:04:19,710 Now let's try the Boolean And operator. 70 00:04:19,710 --> 00:04:24,460 Boolean And requires that the value on both sides be true in order for 71 00:04:24,460 --> 00:04:25,510 it to return true. 72 00:04:25,510 --> 00:04:29,860 If either is false, the And operator is going to return false. 73 00:04:29,860 --> 00:04:32,190 So if we were to type true and 74 00:04:32,190 --> 00:04:36,880 false, the result would be false because the second value is false. 75 00:04:36,880 --> 00:04:40,510 Both values have to be true for And to return true. 76 00:04:40,510 --> 00:04:43,600 Likewise, if the first value is false and 77 00:04:43,600 --> 00:04:47,570 the second value is true, that's also going to be false. 78 00:04:47,570 --> 00:04:54,990 If both values are false, that's also a false result. 79 00:04:54,990 --> 00:05:04,080 Only if both values are true, will you get a result of true from the And operator. 80 00:05:04,080 --> 00:05:06,720 This of course works with comparisons as well. 81 00:05:06,720 --> 00:05:10,687 If we test whether 2 is less than 3 which is true, and 82 00:05:10,687 --> 00:05:13,432 4 is greater than 5 which is false. 83 00:05:13,432 --> 00:05:15,570 Well the result is going to be false. 84 00:05:15,570 --> 00:05:22,390 However if we test whether 2 is less than 3, and 4 is less than 5. 85 00:05:22,390 --> 00:05:25,170 Both of those are true and therefore the result is true. 86 00:05:26,330 --> 00:05:30,000 Again, the most common scenario for using the And Boolean operator, 87 00:05:30,000 --> 00:05:32,210 is going to be when you are working with variables. 88 00:05:32,210 --> 00:05:36,956 So, let's set our quantity variable equal to 25. 89 00:05:36,956 --> 00:05:42,412 And let's test whether quantity is greater 90 00:05:42,412 --> 00:05:47,871 than 50 and quantity is less than 100. 91 00:05:47,871 --> 00:05:51,561 In this case the result of the first comparison is false and 92 00:05:51,561 --> 00:05:55,300 so the result of the And operator is going to be false. 93 00:05:55,300 --> 00:05:59,300 If we change quantity to 125. 94 00:05:59,300 --> 00:06:02,288 Well, 125 is greater than 100. 95 00:06:02,288 --> 00:06:05,415 So if we try the exact same comparison again, 96 00:06:05,415 --> 00:06:10,840 that too will be false, because the second Boolean value is false. 97 00:06:10,840 --> 00:06:13,470 It's only if both Boolean values are true 98 00:06:15,320 --> 00:06:17,750 that we'll get a true result from the And operator. 99 00:06:17,750 --> 00:06:21,190 So if we were to set quantity to 75. 100 00:06:21,190 --> 00:06:25,710 75 is greater than 50 and it's less than 100, so 101 00:06:25,710 --> 00:06:29,800 we finally get a true value for the And Boolean operator. 102 00:06:29,800 --> 00:06:30,340 And finally, 103 00:06:30,340 --> 00:06:33,800 we have the Not operator which we're not going to use in this program. 104 00:06:33,800 --> 00:06:36,290 So we won't talk about it too extensively, but 105 00:06:36,290 --> 00:06:38,630 let's just demonstrate it really quickly. 106 00:06:38,630 --> 00:06:42,840 The Not operator takes the Boolean value to its right and reverses it. 107 00:06:42,840 --> 00:06:46,300 So not true equals false. 108 00:06:46,300 --> 00:06:48,530 And not false equals true. 109 00:06:50,640 --> 00:06:54,340 If you want to know more about Boolean operators, we'll have further reading for 110 00:06:54,340 --> 00:06:56,110 you in the teacher's notes. 111 00:06:56,110 --> 00:07:00,090 But for now, let's try using Boolean operators to fix our program. 112 00:07:00,090 --> 00:07:01,900 So here's the situation. 113 00:07:01,900 --> 00:07:04,650 If our user is ordering 100 or more widgets, 114 00:07:04,650 --> 00:07:07,880 the price per unit gets set to 8 as it's supposed to be. 115 00:07:07,880 --> 00:07:11,290 But then, this if condition also runs. 116 00:07:11,290 --> 00:07:16,060 Because any value that's 100 or over is also going to be greater than 50. 117 00:07:16,060 --> 00:07:18,558 So this code here runs as well. 118 00:07:18,558 --> 00:07:22,190 Overwriting the price-per-unit with a value of 9. 119 00:07:22,190 --> 00:07:27,530 We need this code to run only if the quantity is greater than or equal to 50, 120 00:07:27,530 --> 00:07:33,044 and if the quantity is less than 100. 121 00:07:34,974 --> 00:07:39,986 So we've used the Boolean And operator here, to try to ensure that 122 00:07:39,986 --> 00:07:45,550 the price per unit only gets set to 9 if the value is between 50 and 99. 123 00:07:45,550 --> 00:07:48,072 So let's save this and try running it. 124 00:07:48,072 --> 00:07:51,103 ruby widgets.rb. 125 00:07:51,103 --> 00:07:55,830 And we'll try entering the value that was giving us trouble, 100. 126 00:07:55,830 --> 00:07:56,980 It looks like that worked. 127 00:07:57,980 --> 00:08:02,020 We entered a value of 100 or more and it was multiplied by 8. 128 00:08:02,020 --> 00:08:05,234 Let's try a value greater than 100. 129 00:08:05,234 --> 00:08:06,700 That seems to work as well. 130 00:08:06,700 --> 00:08:08,790 It was multiplied by 8 again. 131 00:08:08,790 --> 00:08:11,960 Now, let's test whether it still works for values of 50 or more. 132 00:08:13,320 --> 00:08:15,345 So how many widgets are you ordering? 133 00:08:15,345 --> 00:08:16,530 50. 134 00:08:16,530 --> 00:08:19,470 We entered 50 widgets, and this time, it was multiplied by 9. 135 00:08:19,470 --> 00:08:22,454 So that discount seems to be working, as well. 136 00:08:22,454 --> 00:08:27,104 Let's try 51, and it was multiplied by 9 again. 137 00:08:27,104 --> 00:08:29,217 Now, let's try 99. 138 00:08:29,217 --> 00:08:34,380 99 is also less than 100, so it was multiplied by 9 again. 139 00:08:34,380 --> 00:08:35,220 And just in case, 140 00:08:35,220 --> 00:08:38,950 let's make sure that orders of less than 50 units are working as well. 141 00:08:38,950 --> 00:08:40,890 Let's try ordering 10. 142 00:08:40,890 --> 00:08:45,070 And that was multiplied by full price, \$10 per widget. 143 00:08:45,070 --> 00:08:46,480 So everything's working. 144 00:08:46,480 --> 00:08:49,150 But really, this solution is kind of sloppy. 145 00:08:49,150 --> 00:08:53,420 If we ever make a change to this logic, we could find ourselves in a situation where 146 00:08:53,420 --> 00:08:58,260 one price per unit value is being overwritten by a different value again. 147 00:08:58,260 --> 00:09:01,730 We only want the code in each of these if statements to run when 148 00:09:01,730 --> 00:09:03,470 none of the others is true. 149 00:09:03,470 --> 00:09:05,020 Next, we'll look at a way to do that.