Page 272 - Computer Science V2.0 Class 11
P. 272
Test False Sequence of Statements
Condition (else block)
True
Sequence of Statements
(if block)
Fig 10.4: Flow diagram of an if-else statement
In the given syntax, there are two header clauses: if and else. The indented sequence of statements in the
if suite (also called, if block) will be executed when the conditional expression yields True. The else suite
(also called, else block) will be executed when the conditional expression yields False. Consider the following
example:
Program 10.1 Write a program check whether a number entered by a user is even or odd.
01 # To check whether a number entered by a user is even or odd.
02 num = int(input("Enter a number: "))
03 if num%2 == 0:
04 print("Number is EVEN")
05 print("Division by 2 leaves the remainder Zero")
06 else:
07 print("Number is ODD")
08 print("Divisibility Check Done")
In the above example, if num is even, the conditional expression in the if statement yields True, and the statements
in the if block are executed. However, if the conditional expression yields False, the statement,
print("Number is ODD")
is executed. The statement,
print("Divisibility Check Done")
is executed in both cases. For example, suppose the user enters the input 24 when the above code is executed. So,
the conditional expression 24%2 == 0 (or equivalently 0 == 0) yields True. Hence, the two statements in the
if-block will get executed. Consequently, the Python interpreter will produce the following output:
>>> Enter a number: 24
Number is EVEN
Division by 2 leaves the remainder Zero
Divisibility Check Done
Next, suppose the user enters the input 15 when the above code is executed. The Python interpreter will produce the
following output:
>>> Enter a number: 15
Number is ODD
Divisibility Check Done
258 Touchpad Computer Science (Ver. 2.0)-XI

