AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Contents · Computing

If…else practice and the elif operator

Lessons 38–39 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
39

Programming branching algorithms: the elif statement

Textbook: pp. 79–80
GoalChecks several conditions in turn with the elif operator, knows the structure and indentation of a nested if, and writes programs with a choice of many paths.
New words
elif: “else if”, the next condition that is checked only if the earlier ones were False · elifmulti-way choice: a chain of if, elif and else where exactly one block is run · ko‘p yo‘lli tanlovnested if: an if placed inside the block of another if, with one more indent · ichma-ich iforder of conditions: the first True one is chosen, so the order matters · shartlar tartibi
Explanation

When one of many cases must be chosen, a chain of if, elif and else is used: Python checks the conditions from top to bottom, runs the first block that turns out True and skips the rest; if none is True, else works. So the order of the conditions matters: once the winter months have been separated, the condition m <= 5 matches only the spring months. The number of elif parts is not limited, and else is optional. A choice operator like CASE in other languages is replaced in Python by this method. An if may contain another if: the inner if is written with 4 more spaces of indent than the outer one, otherwise it becomes a separate condition. The inner condition is checked only when the outer one is True.

Startmm = 12 or m = 1 or m = 2yesnowinterm ≤ 5yesnospringm ≤ 8yesnosummerautumnEnd
💡 Where will I use this?
In real life

A pupil planned the awards for a school sports day: 90 points or more won a gold card, 70 or more silver, 50 or more bronze, and otherwise a certificate for taking part. Because the checks went from the top down, each result got exactly one award.

Who uses it

Programmers, menu planners and service staff check many options in order and pick the matching one.

Try it today

Make a four-option plan on paper that depends on the day of the week. Write one condition and one action per day.

Worked examples
Python
1m = int(input())
2if m == 12 or m == 1 or m == 2:
3    print("qish")
4elif m <= 5:
5    print("bahor")
6elif m <= 8:
7    print("yoz")
8else:
9    print("kuz")
Output
yoz
Name of the season – code above. For month 7 the winter condition is false, m <= 5 is false, m <= 8 is true – yoz is printed.
Python
1n = int(input())
2if n > 0:
3    if n % 2 == 0:
4        print("musbat juft")
5    else:
6        print("musbat toq")
7else:
8    print("musbat emas")
Output
musbat toq
Nested if – code above. 9 is positive, so the inner condition is checked; 9 % 2 = 1, so musbat toq is printed. If a negative number is typed, the inner if is not checked and musbat emas is printed.
Class activity

“Mark translator”: the group writes a program that reads a mark from 1 to 5; with an elif chain it prints 5 – “a’lo” (excellent), 4 – “yaxshi” (good), 3 – “qoniqarli” (satisfactory), 2 – “qoniqarsiz” (unsatisfactory), and for any other number else prints “no such mark”.

Practice
1
What does the season program print for month 4?
2
Write a program with elif that finds the sign of a number (“musbat”, “manfiy”, “nol”).
3
What does the nested program print if n = -4 is typed?
4
Why is an elif chain better than several separate ifs?
Story of the Week · The Khwarizmi WayThe first hand-overClose the week: all of this week’s subjects in one storyRead →