Vous êtes sur la page 1sur 8

Module 4

Python - IF...ELIF...ELSE Statement


Conditional constructs are used to incorporate decision making into programs. The result of this decision making determines the sequence in which a program will execute instructions. You can control the flow of a program by using conditional constructs. This tutorial will discuss the programming conditional constructs available in Python, such as if, if...else, elif, and nested if.

The if statement:
The if statement of Python is similar to that of other languages. The if statement contains a logical expression using which data is compared, and a decision is made based on the result of the comparison. The syntax of the if statement is if expression: statement(s) Here if statement, condition is evaluated first. If condition is true that is, if its value is nonzero then the statement(s) block are executed. Otherwise, the next statement following the statement(s) block is executed. Note: In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.

Example:
var1 = 100 if var1==100: print "True value is",100 var2 = 0 if var2==0:

print "True value is", var2 This will produce following result: True value is 100 True value is 0 Explanation: If var1==100: hundred or not If var1==0: not

# checks whether the var1 is # checks whether the var2 is zero or

1) If statement contains________________ (a) for loop (b) while loop (c) logical expression (d) none
The else Statement: An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a false value. The else statement is an optional statement and there could be at most only one else statement following if . The syntax of the if...else statement is: if expression: statement(s) else: statement(s)

Example:
var1 = 100 if var1==100: print "True value" print var1 else: print "False value" print var1

var2 = 0 if var2==0: print "True value" print var2 else: print "False value" print var2 This will produce the following out put True value 100 False value 0 Explanation: var2 =0 var1 = 100 if var1 ==100: if var2!=0 # var2 is a variable holds the 0 value # var1 is a variable holds the 100 value # checks whether var1 is equal to 100 # checks whether var2 is not equal to 0

2) else: statement is always comes after _____________ statement (a) if statement (b) loop statement statement (d) none (c) while

The elif Statement


The elif statement allows you to check multiple expressions for truth value and execute a block of code as soon as one of the conditions evaluates to true. Like the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if. The syntax of the if...elif statement is: if expression1: statement(s) elif expression2: statement(s)

elif expression3: statement(s) else: statement(s) Note: Python does not currently support switch or case statements as in other languages.

Example:
var = 100 if var == 200: print "Got a true expression value" print var elif var == 150: print "Got a true expression value" print var2 elif var == 100: print "Got a true expression value" print var else: print "Got a false expression value" print var This will produce following result: Got a true expression value 100 Explanation: In the above example:Var=100 # variable var holds the value 100 If var==200: # checks whether the var value is 200 or not If var==150: # checks whether the var value is 150 or not If var==100: #checks whether the var value is 10 or not else: #prints the false value 3) elif statement allows you to check multiple conditions true or false? (a) false (b) true

The Nested if...elif...else Construct

There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if construct. In a nested if construct, you can have an if...elif...else construct inside another if...elif...else construct. The syntax of the nested if...elif...else construct may be: if expression1: statement(s) if expression2: statement(s) elif expression3: statement(s) else statement(s) elif expression4: statement(s) else: statement(s)

Example: var = 100 if var < 200: print "Expression value is less than 200" if var == 150: print "Which is 150" elif var == 100: print "Which is 100" elif var == 50: print "Which is 50" elif var < 50: print "Expression value is less than 50" else: print "Could not find true expression"

This will produce following result: Expression value is less than 200 Which is 100

4) Nested if can be used after a condition becomes true? (a) true (b) false

2) if (a==1): print True else: print False In the above program if a is not equal to 1, what is the out put (a) True (b) False (c) True or false (d) both

Problem Set:
1. Test if a number is a multiple of 3, 5 or 7. 2. Write a program to find the given number is an even or odd number using if else statements? 3. Write a program to give the grades to marks of the students using if and else statements, the marks and grades are as follows
A1 A2 B1 B2 C1 grade - 550 to 600 marks grade- 499 to 549 marks grade- 448 to 498 marks grade- 397 to 447 marks grade- 346 to 396 marks

C2 grade- 295 to 345 marks D1 grade- 245 to 294 marks

IT Quiz: 6) Conditional constructs are used to incorporate _____ making into programs (a) value (b) variable (c) statement (d) decision 7) If statement is executed when condition is ________________ (a) False (b) True (c) true or false (d) true and false 8) else statement is executed when if statement values becomes_____ (a) True (b) false (c) true or false (d) 0 or false 9) a=5%2 it gives ________ (a) Coefficient (b) remainder (c) dividend (d) 0

10) Select the correct sequence of nested if

(a) if: elif:else: none

(b) if:else:elif : (c) else:elif:if :

(d)

11) if and nested ifs always follows ends with_________ (a) comma (d)semicolon (c)colon (d) full stop

Vous aimerez peut-être aussi