Decision Making Statements In Python


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
  1. if condition:  
  2.     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:
  1. a=int(input("Enter 1st number : "))  
  2. b=int(input("Enter 2nd number : "))  
  4. if(b!=0):  
  5.     c=a/b  
  6.     print("%d / %d = %d"%(a,b,c))  
If-else statements
  1. if condition:  
  2.     statement(s)
  3. else:  
  4.     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: 
  1. num=int(input("Enter a number : "))  
  2. if(num%2==0):  
  3.     print("%d is Even Number"%num)  
  4. else:  
  5.     print("%d is Odd Number"%num)  
Nested if Statements
  1. if condition-1:    
  2.       if condition-2:    
  3.                if condition-3:    
  4.                      statement-1  
  5.                else:    
  6.                      statement-2   
  7.        else:    
  8.               statement-3  
  9. else:    
  10.      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:
  1. condition-1, condition-2, and condition-3 all are true, then statement which is written under condition-3 will execute.
  2. 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.
  3. 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.
  4. condition-1 is false then condition-2 and condition-3 never be executed and condition-1's else part will execute.
Flow chart:
  1. print("Welcome to KBC")  
  2. print()  
  3. print("C Language is developed By:")  
  4. print("A. Dennis Ritchie")  
  5. print("B. Bjarne Stroustrup")  
  6. print("C. James A. Gosling")  
  7. print("D. Dr. E.F. Codd")  
  8. choice=str.upper(input("Choice : "))  
  9. if(choice=='A'):  
  10.     print("Right Answer :)")  
  11.     print()  
  12.     print()  
  13.     print("When C Language Developed :")  
  14.     print("A. 1980")  
  15.     print("B. 1982")  
  16.     print("C. 1972")  
  17.     print("D. 1990")      
  18.     choice=str.upper(input("Choice : "))  
  19.     if(choice=='C'):  
  20.         print("Correct Answer :)")  
  21.     else:  
  22.         print("Wrong Answer :(")  
  23. else:  
  24.     print("Sorry! Worng Answer :(");  
elif ladder statements
  1. if condition-1:    
  2.     Statement-1  
  4. elif condition-2:    
  5.     Statement-2  
  7. elif condition-3:    
  8.     Statement-3  
  10. elif condition-n:    
  11.     Statement-n  
  13. else:     
  14.     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:
  1. marks=float(input("Father: How much marks you got in exam ?\nSon : "))    
  2. if(marks>89):    
  3.     print("Father:Excellent, Now I'll give you iPhone")    
  4. elif(marks>79):    
  5.     print("Father:Very Good,I'll give you Laptop")    
  6. elif(marks>69):    
  7.     print("Father:Good,I'll give you Android Phone")    
  8. elif(marks>59):    
  9.     print("Father:Nice,I'll give you Nokia-1100")    
  10. else:    
  11.     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:
  1. print("Progam to check greatest number among three numbers")  
  2. print("__________________________________________________________")  
  3. a=int(input("Please Enter First number : "))  
  4. b=int(input("Please Enter Second number : "))  
  5. c=int(input("Please Enter Third number : "))  
  6. if(a>b):  
  7.     if(a>c):  
  8.         print("%d is gretest number"%a)  
  9.     else:  
  10.         print("%d is gretest number"%c)  
  11. elif(b>c):  
  12.     print("%d is gretest number"%b)  
  13. else:  
  14.     print("%d is gretest number"%c)  

Similar Articles