Introduction
If else statements are also called decision making statements which are used to execute a block of statement on certain conditions.
Types of decision-making statements
There are 4 types of decision making statements in python.
Python if statement
Syntax:
- if condition:
- statement(s)
In the above syntax,
if statement contains particular condition if the condition will be true, then statements which are written under if statement will execute.
Flow Chart:
Example:
- a=int(input("Enter 1st number : "))
- b=int(input("Enter 2nd number : "))
-
- if(b!=0):
- c=a/b
- print("%d / %d = %d"%(a,b,c))
Output:
If-else statements
Syntax:
- if condition:
- statement(s)
- else:
- Statement(s)
In the above syntax, if the condition of
"if statement" will be true, then all the statements which are written under
if statement will execute, otherwise else part always execute.
Flow Chart:
Example:
- num=int(input("Enter a number : "))
- if(num%2==0):
- print("%d is Even Number"%num)
- else:
- print("%d is Odd Number"%num)
Nested if Statements
Syntax:
- if condition-1:
- if condition-2:
- if condition-3:
- statement-1
- else:
- statement-2
- else:
- statement-3
- else:
- statements-4
When a series of decision are involved, we may have to use more then one if-else statement in the nested form.
In the above syntax:
- condition-1, condition-2, and condition-3 all are true, then statement which is written under condition-3 will execute.
- condition-1, condition-2 all are true but condition-3 is false, then statement(s) which are written under condition-3's else part will be executed.
- condition-1 is true but condition-2, is false, then it will not check for condition-3, and statement(s) which are written under condition-2's else part will be executed.
- condition-1 is false then condition-2 and condition-3 never be executed and condition-1's else part will execute.
Flow chart:
Example:
- print("Welcome to KBC")
- print()
- print("C Language is developed By:")
- print("A. Dennis Ritchie")
- print("B. Bjarne Stroustrup")
- print("C. James A. Gosling")
- print("D. Dr. E.F. Codd")
- choice=str.upper(input("Choice : "))
- if(choice=='A'):
- print("Right Answer :)")
- print()
- print()
- print("When C Language Developed :")
- print("A. 1980")
- print("B. 1982")
- print("C. 1972")
- print("D. 1990")
- choice=str.upper(input("Choice : "))
- if(choice=='C'):
- print("Correct Answer :)")
- else:
- print("Wrong Answer :(")
- else:
- print("Sorry! Worng Answer :(");
Output:
elif ladder statements
Syntax:
- if condition-1:
- Statement-1
-
- elif condition-2:
- Statement-2
-
- elif condition-3:
- Statement-3
-
- elif condition-n:
- Statement-n
-
- else:
- Default Statement
In Python,
elif ladder statement is the same as an else-if statement of other programming languages. Sometimes we need to check some conditions when if condition is false.
In the above syntax, if the
condition-1 will be false, then it will check
condition-2 and if
condition-2 will also be false, then it will check
condition-3. If
condition-3 will be false, then it will check
condition-n and if
condition-n will also be false, then the default else statement always execute.
Flow chart:
Example:
- marks=float(input("Father: How much marks you got in exam ?\nSon : "))
- if(marks>89):
- print("Father:Excellent, Now I'll give you iPhone")
- elif(marks>79):
- print("Father:Very Good,I'll give you Laptop")
- elif(marks>69):
- print("Father:Good,I'll give you Android Phone")
- elif(marks>59):
- print("Father:Nice,I'll give you Nokia-1100")
- else:
- print("Father:Fustigate, Slap and Scold...")
Example of "Nested if" and "elif" :
In this example I am combining nested if and elif together in a simple example. Suppose we have a problem. We want to find out greatest number among 3 numbers.
Flow chart:
- print("Progam to check greatest number among three numbers")
- print("__________________________________________________________")
- a=int(input("Please Enter First number : "))
- b=int(input("Please Enter Second number : "))
- c=int(input("Please Enter Third number : "))
- if(a>b):
- if(a>c):
- print("%d is gretest number"%a)
- else:
- print("%d is gretest number"%c)
- elif(b>c):
- print("%d is gretest number"%b)
- else:
- print("%d is gretest number"%c)
Output: