📖 generic · 12th TN - English Medium · COMPUTER SCIENCE · Page 73question

CONTROL STRUCTURES · Part 2

Chapter 2: 3 · COMPUTER SCIENCE

to check the true block as well as the false block. Following is the syntax of ‘if..else’ statement. Syntax: if <condition>: statements-block else: statements-block 12th Computer Chapter - - if condition is true Statement block - Statement block - Exit Entry condition if condition is false Fig. .

if..else statement execution if..else statement thus provides two possibilities and the condition determines which BLOCK is to be executed. a = int(input("Enter any number :")) if a% == : print (a, " is an even number") else: print (a, " is an odd number") Output : Enter any number : is an even number Output : Enter any number : is an odd number Example . : #Program to check if the accepted number odd or even An alternate method to rewrite the above program is also available in Python. The complete if..else can also written as: Syntax: variable = variable1 if condition else variable 12th Computer Chapter - - Control Structures The condition specified in the if statement is checked, if it is true, the value of variable1 is stored in variable on the left side of the assignment, otherwise variable2 is taken as the value.

Note a = int (input("Enter any number :")) x="even" if a% == else "odd" print (a, " is ",x) Output : Enter any number : is odd Output : Enter any number : is even Example . : #Program to check if the accepted number is odd or even (using alternate method of if...else) (iii) Nested if..elif...else statement: When we need to construct a chain of if statement(s) then ‘elif’ clause can be used instead of ‘if else’. Syntax: if <condition- >: statements-block elif <condition- >: statements-block else: statements-block n In the syntax of if..elif..else mentioned above, condition- is tested if it is true then statements-block1 is executed, otherwise the control checks condition- , if it is true statements- block2 is executed and even if it fails statements-block n mentioned in else part is executed. 12th Computer Chapter - - Test Expression

Related topics

Have a question about this topic?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →