WEBVTT

0
00:00.170 --> 00:07.760
In previous lessons, we learned about using the if and else statements to check whether if somebody

1
00:07.760 --> 00:16.850
is over 120cm or not, and allow them to actually purchase a ticket if they are over a certain height.

2
00:17.030 --> 00:23.810
Now, in addition to the height, there's another condition that we need to check for, namely their

3
00:23.810 --> 00:24.530
age.

4
00:24.560 --> 00:31.280
If somebody is over 18 years old, then they should be paying the adult price, which is let's say $12,

5
00:31.280 --> 00:36.470
but if they are 18 or under, then they should only be paying $7.

6
00:37.280 --> 00:42.980
So how can we represent this extra condition that we need to check for in our code?

7
00:43.250 --> 00:48.080
Well, we could use something called a nested if else statement.

8
00:48.260 --> 00:53.780
We've seen our if else statements look like this, where it's only got two choices,

9
00:53.780 --> 00:57.710
if this condition is true, do this, otherwise do that.

10
00:57.710 --> 01:06.260
But in a nested if statement, once the first condition has passed, we can check for another condition,

11
01:06.530 --> 01:13.340
and then we can have another if else statement inside this if condition.

12
01:13.430 --> 01:17.990
In order for this thing to happen, this has to be true,

13
01:17.990 --> 01:23.420
and this also has to be true. In order for this to happen, this condition has to be true,

14
01:23.420 --> 01:25.790
but this condition has to be false.

15
01:26.450 --> 01:33.170
So essentially the computer first looks at the larger picture, which is this first condition, and

16
01:33.170 --> 01:40.280
decides on whether if it should go into the else block here or if it should go into the nested block

17
01:40.280 --> 01:42.440
inside the if statement.

18
01:42.800 --> 01:47.720
So now this is what our flowchart looks like, in the first if statement,

19
01:47.720 --> 01:51.680
we check whether if their height is over 120cm,

20
01:51.680 --> 01:54.560
if no, then the if statements all end.

21
01:54.560 --> 01:56.780
You can't write, you can't buy a ticket.

22
01:56.780 --> 02:03.110
But if yes, we actually take them to yet another if statement where we check their age.

23
02:03.110 --> 02:08.360
If their age is 18 or under, then we give them a $7 ticket.

24
02:08.540 --> 02:12.050
If they're over 18, then they have to pay $12.

25
02:12.710 --> 02:17.180
So the place where we're going to nest, our if statement is inside here.

26
02:17.210 --> 02:19.190
Notice how it's indented.

27
02:19.190 --> 02:22.520
So it's already inside this if block.

28
02:22.520 --> 02:25.160
And this already has to be true.

29
02:25.460 --> 02:30.200
Now here we're going to create another if and else statement.

30
02:30.290 --> 02:33.110
And the condition checks for their age.

31
02:33.110 --> 02:35.450
So we better ask them for an age.

32
02:35.450 --> 02:40.520
Let's say age equals, convert the input to an int.

33
02:40.520 --> 02:42.710
And, "What is your age?"

34
02:42.740 --> 02:50.150
So now that we've gotten hold of their age, we can see if their age is less than or equal to 18.

35
02:50.180 --> 02:54.680
Well in this case we're going to give them the $7 ticket.

36
02:56.510 --> 03:04.910
But else namely if this is not true, if their age is over 18, well, in that case, we're going to

37
03:04.910 --> 03:06.980
give them the $12 ticket.

38
03:07.880 --> 03:18.110
So now we have a nested if statement because this if and else statement lives inside this if statement.

39
03:18.110 --> 03:23.570
So this condition will only be checked if this is already deemed to be true.

40
03:23.600 --> 03:27.140
Now let's say our situation got a little bit more complex.

41
03:27.140 --> 03:31.280
The boss comes over, checks our code and says, wait, wait, wait, wait.

42
03:31.280 --> 03:34.010
There's actually more price tiers than that.

43
03:34.040 --> 03:38.720
In fact, if you're less than 12 years old, you pay $5.

44
03:38.840 --> 03:42.590
If you're between 12 and 18, you pay $7.

45
03:42.620 --> 03:47.900
And if you're over 18, then you pay the full adult price, which is $12.

46
03:48.170 --> 03:51.200
Now there are three possibilities.

47
03:51.200 --> 03:54.200
So how do we represent this in our if statement?

48
03:54.890 --> 03:57.920
Well, we could use something called the elif.

49
03:58.190 --> 04:05.540
Instead of having a simple if else statement where there's only one condition, if it's true, do this,

50
04:05.540 --> 04:07.190
otherwise do that.

51
04:07.460 --> 04:14.210
You can add as many elif conditions as you want. So we can check for condition1,

52
04:14.210 --> 04:16.430
if that's true, then do A,

53
04:16.430 --> 04:21.830
but if that's not true, then we can continue and check for condition2.

54
04:21.860 --> 04:25.700
If condition2 is true, well then we can do B.

55
04:25.700 --> 04:31.250
And finally, if none of those conditions were true, we can do this final thing.

56
04:31.400 --> 04:34.550
Our flowchart now looks something like this,

57
04:34.550 --> 04:37.400
and this is the logic that we're trying to program.

58
04:37.820 --> 04:44.720
Once we're inside this nested if statement, we're going to check if their age is under 12, in which

59
04:44.720 --> 04:46.800
case they should pay $5.

60
04:46.800 --> 04:50.970
If they're between 12 and 18, then they should pay $7.

61
04:50.970 --> 04:55.080
And finally, if they're over 18, then they should pay $12.

62
04:55.590 --> 05:05.250
Now, the first thing I'm going to check is if the age is less than 12 under this condition, they should

63
05:05.250 --> 05:06.990
pay $5.

64
05:06.990 --> 05:09.090
So let's change that to 5.

65
05:09.630 --> 05:15.680
Now the next condition should be created using an elif, which stands for else-if.

66
05:15.680 --> 05:17.450
So, it means,

67
05:17.450 --> 05:20.240
if this is not true, else if,

68
05:20.240 --> 05:21.980
can you check if this is true?

69
05:22.010 --> 05:24.380
Well, in that case then we should do this.

70
05:24.740 --> 05:33.320
For example, if the age is not less than 12, so they're over 12, then are they under 18?

71
05:33.320 --> 05:39.770
Well then this condition basically catches everybody who's between 12 and 18.

72
05:40.070 --> 05:47.720
And finally, if they're not less than or equal to 18 and they're not less than 12, then that's everybody

73
05:47.720 --> 05:49.580
else who is over 18.

74
05:50.840 --> 05:56.750
Now remember that we can use as many elif conditions between the if and else as we like.

75
05:56.750 --> 06:04.100
So I could add another elif that checks whether if the age is less than, say, 22.

76
06:04.130 --> 06:10.040
Well, in this case, do something else, and then I can keep going with these elif's until I'm done

77
06:10.040 --> 06:11.570
with all my conditions.

78
06:12.080 --> 06:14.330
So have a play around with this code.

79
06:14.330 --> 06:21.170
Write it yourself, see if it makes sense, and then mess around with the elif"s so that it does what

80
06:21.170 --> 06:22.640
you expect it to do.

81
06:22.640 --> 06:27.530
And then once you're ready, go ahead and head over to the next lesson where I've got a coding challenge

82
06:27.530 --> 06:27.950
for you.