Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
In previous lessons,
2
we learned about using the if and else statements to check whether if somebody is
3
over 120 centimeters or not and allow them to actually purchase a ticket if they
4
are over a certain height. Now in addition to the height,
5
there's another condition that we need to check for, namely their age.
6
If somebody is over 18 years old and they should be paying the adult price,
7
which is let's say $12. But if they are 18 or under,
8
then they should only be paying $7. So how can we represent this extra condition
9
that we need to check for in our code? Well,
10
we could use something called a nested if/else statement.
11
We've seen our,
12
if/else statements look like this where it's only got two choices.
13
If this condition is true, do this, otherwise do that.
14
But in a nested if statement, once the first condition has passed,
15
we can check for another condition. And then we can have another if/else statement
16
inside this if condition. In order for this thing to happen,
17
this has to be true and this also has to be true. In order for this to happen,
18
this condition has to be true but this condition has to be false.
19
So essentially the computer first looks at the larger picture,
20
which is this first condition and decides on whether if it should go into the
21
else block here or if it should go into the nested block inside the if
22
statement.
23
So now this is what our flow chart looks like. In the first if statement we check
24
whether if their height is over 120 centimeters. If no,
25
then the if statements all end. You can't ride, you can't buy a ticket.
26
But if yes,
27
we actually take them to yet another if statement where we check their age.
28
If their age is 18 or under, then we give them a $7 ticket.
29
If they're over 18 then they have to pay $12. So the place where we're going to
30
nest our if statement is inside here. Notice how it's indented.
31
So it's already inside this if block,
32
and this already has to be true.
33
Now here we're going to create another if and else statement and the condition
34
checks for their age. So we better ask them for an age.
35
Let's say age equals convert the inputs to an int and "what is your age?"
36
So now that we've gotten hold of their age,
37
we can see if their age is less than or equal to 18. Well in this case we're
38
going to give them the $7 ticket.
39
But else namely, if this is not true,
40
if their age is over 18, well, in that case, we're going to give them the $12
41
ticket.
42
So now we have a nested if statement,
43
because this if and else statement lives inside this if statement.
44
So this condition will only be checked if this is already deemed to be true.
45
Now let's say our situation got a little bit more complex. The boss comes over,
46
checks our code and says, wait a wait, wait, wait,
47
there's actually more price tiers than that. In fact,
48
if you're less than 12 years old, you pay $5.
49
If you're between 12 and 18 you pay $7 and if you're over 18 then you pay the
50
full adult price,
51
which is $12. Now there are three possibilities,
52
so how do we represent this in our if statement? Well,
53
we could use something called the elif. Instead of having a simple if/else statement
54
where there's only one condition. If it's true, do this.
55
Otherwise do that.
56
You can add as many elif conditions as you want. So we can check for condition
57
1. If that's true, then do a, but if that's not true,
58
then we can continue and check for condition 2.
59
If condition 2 is true, well then we can do B. And finally,
60
if none of those conditions were true, we can do this final thing.
61
Our flow chart now looks something like this and this is the logic that we're
62
trying to program. Once we're inside this nested if statement,
63
we're going to check if the age is under 12 in which case they should pay $5. If
64
they're between 12 and 18 then they should pay $7 and finally if they're over 18
65
then they should pay $12. Now the first thing I'm going to check is if the age is
66
less than 12. Under this condition,
67
they should pay $5. So let's change that to five.
68
Now the next condition should be created using an elif,
69
which stands for else if. So it means,
70
if this is not true else if, can you check if this is true?
71
Well in that case then we should do this. For example,
72
if the age is not less than 12, so they're over 12, then are they under 18? Well then
73
this condition basically catches everybody who's between 12 and 18. And finally,
74
if they're not less than or equal to 18 and they're not less than 12 then that's
75
everybody else who is over 18.
76
Now remember that we can use as many elif conditions between the if and else as we
77
like.
78
So I could add another elif that checks whether if the age is less than,
79
say, 22 well in this case, do something else.
80
And then I can keep going with these elifs until I'm done with all my
81
conditions. So have a play around with this code,
82
write it yourself, see if it makes sense,
83
and then mess around with the elifs so that it does what you expect it to do.
84
And then once you're ready,
85
go ahead and head over to the next lesson where I've got a coding challenge for
86
you.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.