1 00:00:00,150 --> 00:00:01,960 We've learned about comparison operators. 2 00:00:01,960 --> 00:00:06,802 So we can test whether the user wants to order 50 or more widgets or 100 or more. 3 00:00:06,802 --> 00:00:09,920 Now we just need to add code that applies to the discounted 4 00:00:09,920 --> 00:00:14,080 price if those conditions are true, like most programming languages, 5 00:00:14,080 --> 00:00:18,490 Ruby has an If statement that executes some code only if a condition is true. 6 00:00:19,640 --> 00:00:22,870 It starts with the if key word followed by a condition, 7 00:00:22,870 --> 00:00:27,520 which in these examples are just the Boolean values true and false. 8 00:00:27,520 --> 00:00:30,500 The if line is followed by one or more lines of code. 9 00:00:30,500 --> 00:00:33,950 Like a method body these are usually indented to make them easier to 10 00:00:33,950 --> 00:00:35,960 read although it's not required. 11 00:00:35,960 --> 00:00:38,919 And then the end of the if statement is marked by the end keyword. 12 00:00:40,040 --> 00:00:41,800 In this case, because the condition for 13 00:00:41,800 --> 00:00:46,200 the first if statement is always true, the code it contains will always be run, so 14 00:00:46,200 --> 00:00:50,860 you can see the output true and additional code here down at the bottom. 15 00:00:50,860 --> 00:00:52,020 And because the condition for 16 00:00:52,020 --> 00:00:56,360 the second if statement is always false the code it contains will never be run. 17 00:00:56,360 --> 00:01:00,180 You can see these lines aren't included in the outputs in the bottom. 18 00:01:00,180 --> 00:01:02,250 Just hard coding the values true and 19 00:01:02,250 --> 00:01:06,070 false isn't going to be very useful in conditional statements, though. 20 00:01:06,070 --> 00:01:09,590 Actual programs will use expressions that evaluate the true or 21 00:01:09,590 --> 00:01:12,610 false, like comparison operators. 22 00:01:12,610 --> 00:01:16,270 You can see here that we test whether 75 is greater than 50, 23 00:01:16,270 --> 00:01:17,930 which results in the value true. 24 00:01:17,930 --> 00:01:21,780 So this code runs, and you can see 75 is greater than 50 and 25 00:01:21,780 --> 00:01:23,120 the output down at the bottom. 26 00:01:23,120 --> 00:01:28,880 75 is not greater than 100, though, so this code is then included in the output. 27 00:01:28,880 --> 00:01:32,670 Finally, we test whether the value 50 is equal to the value 50, 28 00:01:32,670 --> 00:01:34,890 which of course is true so this code runs. 29 00:01:34,890 --> 00:01:37,970 And you can see 50 equals 50 in the output down at the bottom. 30 00:01:39,000 --> 00:01:43,400 Actual programs aren't likely to compare pairs of hard coded numbers either. 31 00:01:43,400 --> 00:01:46,560 Usually, they'll test the value of a variable. 32 00:01:46,560 --> 00:01:49,950 So here we assign the value 75 to the variable number, and 33 00:01:49,950 --> 00:01:52,950 then we test whether a number is greater than 50. 34 00:01:52,950 --> 00:01:58,120 Which of course it is, so we can see number greater than 50 down in the output. 35 00:01:58,120 --> 00:02:02,780 However, 75 is not greater than 100, so we don't see this in the output. 36 00:02:02,780 --> 00:02:07,740 And 75 is also not equal to 50, so we don't see that in the output either. 37 00:02:07,740 --> 00:02:11,170 If statements run some code only if a condition is true. 38 00:02:11,170 --> 00:02:14,880 But what if you need to run some code only if the condition is false? 39 00:02:14,880 --> 00:02:18,650 Many languages require you to use some ugly code to make this work with an if 40 00:02:18,650 --> 00:02:22,020 statement, but Ruby provides a second keyword. 41 00:02:22,020 --> 00:02:25,010 Unless, that's basically the opposite of if. 42 00:02:26,160 --> 00:02:29,680 Unless runs code only if its condition is false. 43 00:02:29,680 --> 00:02:32,680 So let's try changing all these if statements to unless 44 00:02:32,680 --> 00:02:38,470 statements, And try running this. 45 00:02:41,530 --> 00:02:44,660 Now we get the opposite of the previous results. 46 00:02:44,660 --> 00:02:49,043 The value in number 75 which is greater than 50, so that returns true, 47 00:02:49,043 --> 00:02:53,710 but because this is an unless statement it only runs if the result is false. 48 00:02:53,710 --> 00:02:58,085 So this code doesn't run and doesn't appear on the output below. 49 00:02:58,085 --> 00:03:02,110 75 is not greater than 100, so this returns false but 50 00:03:02,110 --> 00:03:07,940 because this is an unless statement it does run because the condition is false. 51 00:03:07,940 --> 00:03:11,920 So you see number greater than 100 in the output below. 52 00:03:13,070 --> 00:03:17,990 And likewise 75 is not equal to 50, so that returns a false value. 53 00:03:17,990 --> 00:03:23,090 But because this is an unless statement, it runs because the condition was false. 54 00:03:23,090 --> 00:03:27,420 So you see number == 50 in the output below. 55 00:03:27,420 --> 00:03:31,740 Let's try updating the price method in our widget shopping program to use if 56 00:03:31,740 --> 00:03:36,920 statements to determine which discount is should apply. 57 00:03:36,920 --> 00:03:41,796 We'll get rid of the existing code and we'll say, if the quantity that 58 00:03:41,796 --> 00:03:46,415 the user has requested is greater than or equal to 100. 59 00:03:50,540 --> 00:03:54,560 We'll add an end keyword for the if statement early. 60 00:03:54,560 --> 00:03:59,210 And in the body of that if statement, we'll say that the price_per_unit, 61 00:03:59,210 --> 00:04:04,390 we'll create a variable to hold that, should be set to 8. 62 00:04:04,390 --> 00:04:07,730 Because if the user is ordering 100 widgets or more, 63 00:04:07,730 --> 00:04:10,441 they should only be charged $8 per widget. 64 00:04:11,900 --> 00:04:16,664 If the quantity, Is greater than or 65 00:04:16,664 --> 00:04:23,547 equal to 50, Then we're going to set the price per unit, 66 00:04:25,530 --> 00:04:30,640 Variable to equal 9 instead of 8 because we don't wanna give them quite as big 67 00:04:30,640 --> 00:04:33,630 a discount if they're only ordering 50 units. 68 00:04:35,180 --> 00:04:40,573 And if the quantity is < 50, 69 00:04:44,603 --> 00:04:46,630 Then we're going to want to charge full price. 70 00:04:46,630 --> 00:04:53,106 So we'll set a price_per_unit of 10. 71 00:04:53,106 --> 00:04:58,875 Down here at the bottom we'll take the quantity that they entered and 72 00:04:58,875 --> 00:05:02,560 we'll multiply that by the price per unit. 73 00:05:04,160 --> 00:05:09,060 So that will be the quantity will be multiplied by 10 if they're ordering less 74 00:05:09,060 --> 00:05:09,850 than 50 units. 75 00:05:09,850 --> 00:05:13,780 It'll be multiplied by 9 if they're ordering more than 50 units, and 76 00:05:13,780 --> 00:05:18,420 it'll be multiplied by 8 if they ordered more than 100 units. 77 00:05:18,420 --> 00:05:21,849 So let's save this and try it out. 78 00:05:23,787 --> 00:05:27,230 Ruby widgets.rb. 79 00:05:27,230 --> 00:05:31,560 Okay, so we'll start with an order of less than 50, let's order 10. 80 00:05:31,560 --> 00:05:35,620 And it's multiplied by 10 for a total of $100. 81 00:05:35,620 --> 00:05:38,190 That part seems to be working correctly. 82 00:05:38,190 --> 00:05:40,182 Now let's try an order of 50. 83 00:05:43,488 --> 00:05:49,130 That time, the price per unit was multiplied by 9 to get $450. 84 00:05:49,130 --> 00:05:52,400 We'll get the same results if we order 51 units. 85 00:05:53,690 --> 00:05:56,650 We're still in the $9 per widget price tier, so 86 00:05:56,650 --> 00:05:59,470 adding one widget adds $9 onto the total. 87 00:06:00,470 --> 00:06:02,804 Now let's try ordering 100 widgets or more. 88 00:06:07,600 --> 00:06:10,870 For 100 widgets here, the total is $900. 89 00:06:10,870 --> 00:06:14,810 It looks like 100 was multiplied by 9 again, not by 8. 90 00:06:14,810 --> 00:06:16,851 It's supposed to be multiplied by 8. 91 00:06:16,851 --> 00:06:20,522 The reason for this is that when the user enters 100 or more, 92 00:06:20,522 --> 00:06:24,990 this condition here does result in true and the price per unit gets set to 8. 93 00:06:24,990 --> 00:06:29,340 But a quantity of 100 or more is also going to be greater than or 94 00:06:29,340 --> 00:06:33,560 equal to 50, so this condition is also true. 95 00:06:33,560 --> 00:06:35,480 And this code here runs as well. 96 00:06:35,480 --> 00:06:39,920 The price per unit get set to eight first, then it gets set to nine. 97 00:06:39,920 --> 00:06:42,080 So, we've got a bug here that we need to fix. 98 00:06:42,080 --> 00:06:44,080 We'll see how to do that in the next video.