1 00:00:00,260 --> 00:00:05,160 If the condition of an if statement is false, the code in its body won't run. 2 00:00:05,160 --> 00:00:09,980 Here, we set the if_condition variable to equal the false Boolean value. 3 00:00:09,980 --> 00:00:13,620 And since that's our condition for this if statement, and 4 00:00:13,620 --> 00:00:18,230 that's false, the code here doesn't run, and we have no output down below. 5 00:00:18,230 --> 00:00:22,260 If you have some code that you want to run when the if statement doesn't run, 6 00:00:22,260 --> 00:00:24,890 you can add an else clause. 7 00:00:24,890 --> 00:00:28,970 You add the keyword else between if and end, followed by one or 8 00:00:28,970 --> 00:00:33,390 more lines of code that you want to run instead of the if statement's code. 9 00:00:33,390 --> 00:00:38,644 So here we'll call puts, and we'll pass it the string else clause. 10 00:00:42,598 --> 00:00:46,982 Ruby doesn't require it, but it's conventional to have the else keyword 11 00:00:46,982 --> 00:00:51,570 aligned with the if keyword, and the else code indented one level. 12 00:00:51,570 --> 00:00:53,720 Again, it just makes the code easier to read. 13 00:00:54,820 --> 00:00:56,600 Let's save this, and try running it. 14 00:00:58,300 --> 00:01:02,950 And this time because the if clause is false, the else clause runs instead, and 15 00:01:02,950 --> 00:01:05,770 it prints else clause in our output. 16 00:01:05,770 --> 00:01:08,903 If we were to change the if_condition variable to true, 17 00:01:12,512 --> 00:01:16,876 And try running it again, then the if clause will run, and 18 00:01:16,876 --> 00:01:20,210 we'll see if clause in our program output. 19 00:01:20,210 --> 00:01:22,070 But let's change this back to false. 20 00:01:23,560 --> 00:01:27,563 If you have other conditions that you want to test only when the if_condition is 21 00:01:27,563 --> 00:01:33,120 false, you can add one or more elsif clauses between the if and else clauses. 22 00:01:33,120 --> 00:01:36,860 You add the keyword elsif, another condition expression. 23 00:01:36,860 --> 00:01:40,410 So we'll test the value of the variable elsif_condition, 24 00:01:40,410 --> 00:01:41,710 which we'll define in a moment. 25 00:01:43,160 --> 00:01:45,860 And some code you want to run if the condition is true. 26 00:01:45,860 --> 00:01:51,495 So we'll say puts elsif_clause to show that the elsif clause is running. 27 00:01:55,093 --> 00:01:58,407 Then up here, let's add an elsif_condition variable. 28 00:02:02,081 --> 00:02:04,600 And we'll set this to true. 29 00:02:04,600 --> 00:02:06,750 Let's try running this again. 30 00:02:06,750 --> 00:02:09,100 And because our if_condition is false and 31 00:02:09,100 --> 00:02:13,230 our elsif_condition is true, the elsif clause is what runs down here. 32 00:02:14,950 --> 00:02:19,950 As with the else clause, the elsif keyword should be lined up with the if keyword, 33 00:02:19,950 --> 00:02:22,690 and the code following it should be indented one level. 34 00:02:23,780 --> 00:02:26,490 Now what if our if_condition variable were set to true? 35 00:02:27,870 --> 00:02:28,760 Let's try running that. 36 00:02:30,020 --> 00:02:33,910 If the if clause is true, then neither the elsif clauses nor 37 00:02:33,910 --> 00:02:35,440 the else clause will be run. 38 00:02:36,520 --> 00:02:40,399 What if we were to set both if_condition and elsif_condition to false? 39 00:02:44,140 --> 00:02:46,100 Let's try running that. 40 00:02:46,100 --> 00:02:49,337 If the if_condition and all elsif_conditions are false, 41 00:02:49,337 --> 00:02:51,120 then the else clause will be run. 42 00:02:52,146 --> 00:02:55,650 The else clause isn't required, if all conditions are false and 43 00:02:55,650 --> 00:02:58,440 there's no else clause, then no code will be run. 44 00:03:00,320 --> 00:03:03,710 Let's apply what we've learned to the widget store program. 45 00:03:03,710 --> 00:03:07,065 Right now, we're checking the conditions for all three if statements. 46 00:03:07,065 --> 00:03:09,530 We've tried to fix it so this won't happen. 47 00:03:09,530 --> 00:03:11,740 But if more than one condition were true, 48 00:03:11,740 --> 00:03:15,490 the price_per_unit variable would be set and then overwritten. 49 00:03:15,490 --> 00:03:18,780 No matter what we only want price_per_unit set once. 50 00:03:19,910 --> 00:03:23,550 Let's use elsif and else clauses to clean this up. 51 00:03:23,550 --> 00:03:26,650 So, we're going to combine all this into one if statement. 52 00:03:26,650 --> 00:03:29,460 So, we're going to get rid of these end keywords here. 53 00:03:29,460 --> 00:03:34,563 And we're going to convert these if statements to an elsif 54 00:03:34,563 --> 00:03:39,130 statement and an else clause. 55 00:03:39,130 --> 00:03:43,280 But really the elsif branch can't possibly run if the quantity is equal to or 56 00:03:43,280 --> 00:03:46,630 greater than 100 because then only the if branch will run. 57 00:03:47,740 --> 00:03:52,360 So we can get rid of the Boolean and operator and the second comparison. 58 00:03:52,360 --> 00:03:57,640 So let's save that and try running it ruby widgets.rb. 59 00:03:57,640 --> 00:04:03,290 We'll try running it with a quantity of 100, and it's multiplied by 8. 60 00:04:03,290 --> 00:04:07,921 Let's try running it again with a quantity of 50, and it's multiplied by 9. 61 00:04:07,921 --> 00:04:13,361 And let's try running it with a quantity of less than 50, we'll go with 10, 62 00:04:13,361 --> 00:04:17,450 and it's multiplied by the full price, $10 per widget. 63 00:04:17,450 --> 00:04:19,910 Our both discount features working great. 64 00:04:19,910 --> 00:04:21,910 Thanks to the elsif and else clauses, 65 00:04:21,910 --> 00:04:24,510 we can be sure that the discount is applied only once. 66 00:04:25,630 --> 00:04:28,090 We've completed all of our features. 67 00:04:28,090 --> 00:04:30,640 We display a welcome message to our user. 68 00:04:30,640 --> 00:04:33,100 We prompt them for the number of widgets they want. 69 00:04:33,100 --> 00:04:35,018 And store it with the [INAUDIBLE] or in a variable. 70 00:04:35,018 --> 00:04:37,972 Then we calculate a total price for the order. 71 00:04:37,972 --> 00:04:41,830 We also apply a discount if they're ordering 50 or more widgets. 72 00:04:41,830 --> 00:04:42,800 Our program is done.