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
38

Practical lesson: the if…else statement

Textbook: p. 78
GoalIn the practical lesson, programs tasks on divisibility, finding the larger number and changing a value by a condition with the if…else operator.
New words
divisibility condition: a % b == 0 means a is divisible by b with no remainder · bo‘linish shartimaximum: the larger of two or more numbers · maksimumabsolute value (modulus): the number without its sign · modulconditional assignment: a variable receives a value depending on a condition · shartli o‘zlashtirish
Explanation

This lesson solves three typical tasks. The first is divisibility: if a is divisible by b with no remainder then a % b == 0, otherwise the remainder is not zero. The second is finding the larger of two numbers: if a > b then m = a, otherwise m = b – on both paths m receives a value, so print(m) always works. The third is finding the absolute value of a number: we change the sign only if x is negative, so else is not needed. Before working on a task draw the flowchart: each condition is one rhombus and each path is one block. Test the program with at least two different values: once with the condition true and once with it false. This two-sided test is the main check of branching programs.

Starta, ba > byesnom = am = bmEnd
💡 Where will I use this?
In real life

For a yard game, teams had to be split evenly: if the number of players is even, make two teams; if odd, one person is the referee. This is an even-odd check done with the remainder.

Who uses it

Organisers, referees and programmers check a remainder to share work into groups.

Try it today

Write five numbers and divide each by 2 on paper to mark even or odd. Then find those divisible by 3.

Worked examples
Python
1a = int(input())
2b = int(input())
3if a % b == 0:
4    print("ha")
5else:
6    print("yo‘q", a % b)
7
8a = int(input())
9b = int(input())
10if a > b:
11    m = a
12else:
13    m = b
14print(m)
Output
yo‘q 2
15
Checking divisibility – the first program above. For 17 and 5 the remainder is 2, so yo‘q 2 is printed. Finding the larger – the second program above. For 8 and 15, 15 is printed.
Python
1t = int(input())
2if t >= 25:
3    print("issiq")
4else:
5    print("salqin")
6
7x = int(input())
8if x < 0:
9    x = -x
10print(x)
Output
issiq
9
Temperature – the first program above. For 30 degrees issiq is printed. Absolute value – the second program above. If -9 is typed, 9 is printed; a positive number stays unchanged.
Class activity

“Two-sided test”: each group chooses two test values for its program (one making the condition true and one false), predicts the result in advance and then checks in IDLE.

Practice
1
What does this program print?
Python
1a = 12
2b = 5
3if a % b == 0:
4    print(a // b)
5else:
6    print(a % b)
2
Write a program that reads a temperature and prints “issiq” if it is 25 or higher and “salqin” otherwise.
3
Write a program that finds the absolute value of a number (9 for -9, 4 for 4).
4
Why is else not needed in the absolute value task?
Story of the Week · The Khwarizmi WayThe first hand-overClose the week: all of this week’s subjects in one storyRead →