Selections The if...else Statement
Programing required to make decisions. The if else and if.. elif..else are used in python to make decisions.
The if statement
Syntax
if (condition):
statement
statement
How if statement works
Here, First the program evaluates if the condition is True
If the condition is True, non zero value if block is executed
If the condition is False, zero value, None if block is not executed
Note:
Indentation in python means spaces (tab or 4 spaces) at the beginning of a code line. Python uses indentation to indicate a block of code.
The block starts with an indentation and the first unindented line of code marks the end.
if Statement Flowchart
Example: Python if Statement
name = "Vijay"
if name != '': #Condition is True
print("Hello ", name)
print("This is inside the if block")
print("This is outside the if block")
Output
Hello Vijay
This is inside the if block
This is outside the if block
Example: Python if Statement
name = ""
if name != '': #Condition is False
print("Hello ", name)
print("This is inside the if block")
print("This is outside the if block")
Output
This is outside the if block
The if.. else statement
Syntax
if condition: statement statement |
|
else: statement statement |
How if...else statement works
First the program evaluates if the condition is True
If the condition is True, if block is executed.
If the test expression is False, else block executed.
if ..else Statement Flowchart
Flowchart of if else statement
Example: Python if Statement
name = "Vijay"
if name != '':
print("Hello", name)
else:
print("Hello Guest")
name = ""
if name != '':
print("Hello", name)
else:
print("Hello Guest")
Output
Hello Vijay
Hello Guest
if...elif...else Statement
If else statement check only two condition , to check more than two conditions if… elif…else statement is used.
Syntax
if condition:
if block
elif condition:
elif block
.
.
.
else:
else block
Note:
The if…elif..else block can have only one if , else block, however it can have multiple elif blocks.
If the condition is False, it checks the elif ‘s condition, next elif block and so on.
If all the conditions are False, the else block is executed.
Only one block among the several if...elif...else blocks are executed according to the condition.
Flowchart of if...elif...else statement
Example of if...elif...else
number = int(input("Enter a number"))
if(number == 0 ):
print("Number is Zero")
elif(number % 2 == 0):
print("Number is Even")
else:
print("Number is Odd")
Output 1
Enter a number: 0
Number is Zero
Output 2
Enter a number: 1
Number is Odd
Output 3
Enter a number: 2
Number is Even
Nested if statements
Nesting is a if...elif...else statement inside another if...elif...else or if else statement.
Nested if else Example
num1 = int(input("Enter number1: "))
num2 = int(input("Enter number2: "))
num3 = int(input("Enter number3: "))
if( num1 > num2 ):
if(num1 > num3):
print(num1, " is greater")
else:
print(num3, " is greater")
else:
if(num2 > num3):
print(num2, " is greater")
else:
print(num3, " is greater")
Output 1
Enter number1: 1
Enter number2: 2
Enter number3: 3
3 is greater
Output 2
Enter number1: 30
Enter number2: 20
Enter number3: 10
30 is greater
Output 3
Enter number1: 3
Enter number2: 9
Enter number3: 6
9 is greater