1 00:00:00,310 --> 00:00:01,630 In previous lessons, 2 00:00:01,720 --> 00:00:06,720 we learned about using the if and else statements to check whether if somebody is 3 00:00:07,990 --> 00:00:12,990 over 120 centimeters or not and allow them to actually purchase a ticket if they 4 00:00:14,530 --> 00:00:19,390 are over a certain height. Now in addition to the height, 5 00:00:19,870 --> 00:00:24,370 there's another condition that we need to check for, namely their age. 6 00:00:24,850 --> 00:00:29,260 If somebody is over 18 years old and they should be paying the adult price, 7 00:00:29,290 --> 00:00:33,940 which is let's say $12. But if they are 18 or under, 8 00:00:34,030 --> 00:00:39,030 then they should only be paying $7. So how can we represent this extra condition 9 00:00:40,510 --> 00:00:43,810 that we need to check for in our code? Well, 10 00:00:43,810 --> 00:00:47,830 we could use something called a nested if/else statement. 11 00:00:48,550 --> 00:00:49,390 We've seen our, 12 00:00:49,420 --> 00:00:53,650 if/else statements look like this where it's only got two choices. 13 00:00:54,040 --> 00:00:57,370 If this condition is true, do this, otherwise do that. 14 00:00:58,060 --> 00:01:02,980 But in a nested if statement, once the first condition has passed, 15 00:01:03,400 --> 00:01:08,400 we can check for another condition. And then we can have another if/else statement 16 00:01:10,990 --> 00:01:15,990 inside this if condition. In order for this thing to happen, 17 00:01:16,750 --> 00:01:21,640 this has to be true and this also has to be true. In order for this to happen, 18 00:01:21,850 --> 00:01:25,690 this condition has to be true but this condition has to be false. 19 00:01:26,590 --> 00:01:30,250 So essentially the computer first looks at the larger picture, 20 00:01:30,760 --> 00:01:35,200 which is this first condition and decides on whether if it should go into the 21 00:01:35,200 --> 00:01:40,200 else block here or if it should go into the nested block inside the if 22 00:01:41,650 --> 00:01:42,483 statement. 23 00:01:43,090 --> 00:01:48,090 So now this is what our flow chart looks like. In the first if statement we check 24 00:01:48,520 --> 00:01:52,690 whether if their height is over 120 centimeters. If no, 25 00:01:52,810 --> 00:01:56,590 then the if statements all end. You can't ride, you can't buy a ticket. 26 00:01:57,100 --> 00:01:58,000 But if yes, 27 00:01:58,030 --> 00:02:02,920 we actually take them to yet another if statement where we check their age. 28 00:02:03,430 --> 00:02:08,169 If their age is 18 or under, then we give them a $7 ticket. 29 00:02:08,770 --> 00:02:13,770 If they're over 18 then they have to pay $12. So the place where we're going to 30 00:02:14,350 --> 00:02:19,270 nest our if statement is inside here. Notice how it's indented. 31 00:02:19,330 --> 00:02:22,390 So it's already inside this if block, 32 00:02:22,780 --> 00:02:24,850 and this already has to be true. 33 00:02:25,750 --> 00:02:30,750 Now here we're going to create another if and else statement and the condition 34 00:02:31,690 --> 00:02:35,320 checks for their age. So we better ask them for an age. 35 00:02:35,740 --> 00:02:40,740 Let's say age equals convert the inputs to an int and "what is your age?" 36 00:02:43,060 --> 00:02:45,400 So now that we've gotten hold of their age, 37 00:02:45,460 --> 00:02:50,460 we can see if their age is less than or equal to 18. Well in this case we're 38 00:02:51,760 --> 00:02:54,460 going to give them the $7 ticket. 39 00:02:56,800 --> 00:03:00,370 But else namely, if this is not true, 40 00:03:00,370 --> 00:03:05,370 if their age is over 18, well, in that case, we're going to give them the $12 41 00:03:06,280 --> 00:03:07,113 ticket. 42 00:03:08,200 --> 00:03:12,280 So now we have a nested if statement, 43 00:03:12,400 --> 00:03:17,400 because this if and else statement lives inside this if statement. 44 00:03:18,370 --> 00:03:23,320 So this condition will only be checked if this is already deemed to be true. 45 00:03:23,830 --> 00:03:28,600 Now let's say our situation got a little bit more complex. The boss comes over, 46 00:03:28,660 --> 00:03:31,210 checks our code and says, wait a wait, wait, wait, 47 00:03:31,540 --> 00:03:35,020 there's actually more price tiers than that. In fact, 48 00:03:35,410 --> 00:03:38,470 if you're less than 12 years old, you pay $5. 49 00:03:39,130 --> 00:03:44,130 If you're between 12 and 18 you pay $7 and if you're over 18 then you pay the 50 00:03:45,310 --> 00:03:46,450 full adult price, 51 00:03:46,480 --> 00:03:51,130 which is $12. Now there are three possibilities, 52 00:03:51,430 --> 00:03:55,570 so how do we represent this in our if statement? Well, 53 00:03:55,600 --> 00:04:00,600 we could use something called the elif. Instead of having a simple if/else statement 54 00:04:01,600 --> 00:04:05,380 where there's only one condition. If it's true, do this. 55 00:04:05,650 --> 00:04:06,970 Otherwise do that. 56 00:04:07,750 --> 00:04:12,750 You can add as many elif conditions as you want. So we can check for condition 57 00:04:13,660 --> 00:04:18,130 1. If that's true, then do a, but if that's not true, 58 00:04:18,459 --> 00:04:21,519 then we can continue and check for condition 2. 59 00:04:22,180 --> 00:04:26,920 If condition 2 is true, well then we can do B. And finally, 60 00:04:26,980 --> 00:04:30,910 if none of those conditions were true, we can do this final thing. 61 00:04:31,570 --> 00:04:36,160 Our flow chart now looks something like this and this is the logic that we're 62 00:04:36,160 --> 00:04:40,690 trying to program. Once we're inside this nested if statement, 63 00:04:40,990 --> 00:04:45,990 we're going to check if the age is under 12 in which case they should pay $5. If 64 00:04:47,350 --> 00:04:52,350 they're between 12 and 18 then they should pay $7 and finally if they're over 18 65 00:04:53,200 --> 00:04:58,200 then they should pay $12. Now the first thing I'm going to check is if the age is 66 00:05:00,700 --> 00:05:04,450 less than 12. Under this condition, 67 00:05:04,720 --> 00:05:08,890 they should pay $5. So let's change that to five. 68 00:05:10,000 --> 00:05:13,870 Now the next condition should be created using an elif, 69 00:05:14,320 --> 00:05:17,410 which stands for else if. So it means, 70 00:05:17,530 --> 00:05:21,880 if this is not true else if, can you check if this is true? 71 00:05:22,090 --> 00:05:25,720 Well in that case then we should do this. For example, 72 00:05:25,960 --> 00:05:30,960 if the age is not less than 12, so they're over 12, then are they under 18? Well then 73 00:05:34,240 --> 00:05:39,240 this condition basically catches everybody who's between 12 and 18. And finally, 74 00:05:41,350 --> 00:05:46,350 if they're not less than or equal to 18 and they're not less than 12 then that's 75 00:05:47,200 --> 00:05:49,420 everybody else who is over 18. 76 00:05:51,130 --> 00:05:56,130 Now remember that we can use as many elif conditions between the if and else as we 77 00:05:56,140 --> 00:05:56,973 like. 78 00:05:57,050 --> 00:06:02,050 So I could add another elif that checks whether if the age is less than, 79 00:06:02,720 --> 00:06:06,380 say, 22 well in this case, do something else. 80 00:06:06,650 --> 00:06:10,610 And then I can keep going with these elifs until I'm done with all my 81 00:06:10,610 --> 00:06:14,210 conditions. So have a play around with this code, 82 00:06:14,510 --> 00:06:17,180 write it yourself, see if it makes sense, 83 00:06:17,570 --> 00:06:22,340 and then mess around with the elifs so that it does what you expect it to do. 84 00:06:23,000 --> 00:06:24,080 And then once you're ready, 85 00:06:24,290 --> 00:06:27,680 go ahead and head over to the next lesson where I've got a coding challenge for 86 00:06:27,680 --> 00:06:27,920 you.