Complex branching¶
Successive conditions¶
There are tasks in which, when one condition is not fulfilled, another one should be checked, and if that condition is not valid either, then a third one is checked and so on. To avoid writting
in Python we use the special word elif
, which stands for else: and the indented if in the following line. So we get a more readable code:
Note 1: Any number of consecutive elif statements can be used this way.
Note 2: The part
is optional and can be omitted if not needed.
Examples and tasks¶
Example - body mass index:
Body mass index (abbreviated bmi) is used for quick orientation regarding the degree of obesity or weight loss. The formula for calculating body mass index is \(bmi = {m \over {h \times h}}\), where m is the mass in kilograms and h is the height in meters. The bmi values are interpreted as follows:
up to 18.5: malnourished person
from 18.5 to 25: person of normal body weight
from 25 to 30: overweight
over 30: obese person
Write a program that takes a person’s weight and height and then writes to which category that person belongs (limit values belong to a lower category).
One possible solution is given below. Consider why it is not necessary to use complex logical conditions (built with words and, or, not) in this solution.
Task - age groups of players:
Young basketball players register at the beginning of the basketball season in one of the age categories (U10, U12, U14, U16, U18), according to how many years they turn in the calendar year in which the season begins. The registration rules are as follows:
9 and under - U10
10 or 11 years - U12
12 or 13 years - U14
14 or 15 years - U16
16 or 17 years - U18
Write a program that takes the age of a basketball player in the year they register and pritnts their age category.
For example, if age is 15, the program should print U16
Task - ordinal number:
Write a program that loads an integer from 1 to 6 (including borders) and prints the appropriate ordinal number in letters. For example, if number 6 is loaded, the “sixth” (without quotation marks) should be printed.
Nested branching¶
Nested branches are if statements in the branches of other if statements. Nested if statements can be found in one or the other, or in both branches of a larger if statement. This way of setting if statements can go to any depth, but it should be borne in mind that that way programs can become difficult to understand exactly and hard to maintain.
In the first example, we intentionally provide a program with three levels of nesting if statements, to help you imagine what a program with even more deeply nested and longer if statements might look like. In other examples and tasks, we will limit ourselves to one level of inserting if statements.
Examples and tasks¶
Example - guess who
There are eight children in the neighborhood who are often together. Their names are: Alice, Ben, Charlotte, Daniel, Emily, Frankie, Gabriella and Harry. Alice, Ben, Charlotte and Daniel go to the programming section, and Alice, Ben, Emily and Frankie to the sports section. The school cook wanted to praise one of the children for some deed, but did not know the name of that child.
Write a program that asks three questions, accepts the answers to those questions (the letter ‘y’ for yes, and every other answer for no) and prints out the name of the child in question. The questions the program asks are:
Is it a girl?
Does he or she go to the sports section?
Does he or she go to the programming section?
Note that programs with nested branches can be modified to use only consecutive conditions and form with elif, without inserting if statements in depth. In doing so, we use complex conditions, which we build using logical operations and, or and not.
Task - crossroads:
There is an intersection of A and B streets. The even house numbers in Street A are on the right and odd ones are on the left. On the even (right) side, the numbers up to the intersection are from 2 to 200, and after the intersection are those greater than 200. On the odd (left) side, the numbers up to the intersection are from 1 to 177, and after the intersection they are those from 179 onwards.
Write a program that loads one house number on street A and answers whether that number is before or after the intersection and which side of street A it is on. For example:
for number 128, print “on the right side, before the intersection”
for number 284 print “on the right side, after the intersection”
for number 177, enter “on the left side, before the intersection”
for number 219 write “on the left side, after the intersection”
Hint: After loading, you should first check if n
is even, that is, if n % 2 == 0
.
Task - studying:
John’s parents told John that if he received fours or fives in maths and English, he could go to an afternoon football tournament, otherwise he had to learn the subject or subjects from which he received grade(s) less than 4 (grades are from 1 to 5, 1 being the worst, 5 being the best).
Write a program that first loads John’s math grade and then English grade and prints a message for John. For example:
for grades 2, 3 print “learn math and English”
for grades 3, 4 print “learn math”
for grades 4, 2 print “learn English”
for grades 5, 4 print “go to the tournament”
Task - dressing up:
Ian is writing a program that reads the current temperature (in degrees Celsius) and the chance of rain (from 0 to 100) from the weather website, and based on that information, it writes a recommendation whether to bring a jacket (which has a hood) or an umbrella, or none of these two. Ian chose this rule:
when the temperature is below 21 Celsius, the advice should be: “wear the jacket”
when the temperature is 21 Celsius or higher and the chance of rain is over 50, the recommendation is: “bring an umbrella”
when the temperature is 21 Celsius or higher and the chance of rain is up to 50, the advice should be “you can go in a T-shirt”
The task for you is to write a program that loads the temperature first, then the chance of rain, and then prints a recommendation.