Decision Making

Introduction

In this article, you will learn that decision making is an expectation condition, which occurs at the time of execution of the program and decision making is used to control the flow to the program, which means decision making is used to execute the statement, according to the particular condition.

Decision making statement works on true or false.



Decision making executes the statement, when the condition is true.

Python supports (decision making) statement, given below.
  • if statement
  • if else statement
  • elif statement
  • nested if statement
if statement

If statement contains a logical expression and a logical expression is compared for the execution of statement inside the “if statement” block.

Flow chart of “if statement” is given below.



Syntax of “if statement”

if condition(c):
    statement(s)

Only statement is executed, when the condition statement (c) is “true”.

Otherwise statement (s) is skipped.

Example
  1. a= 100  
  2. if a:  
  3.    print("true expression value")  
  4.    print(a)  
  5. b= 0  
  6. if b:  
  7.   print("true expression value")  
  8.   print(b)  
Output



Explanation

a=100, condition is true because in Python, every non zero condition is true and it prints the statement.
b=0, condition is false because in Python, every zero condition is false and skips the statement.

if else statement

If else statement in Python is used to execute the statement, according to the condition. Else is executed, when the condition of “if statement” is false.

Flowchart



Syntax

If condition(c):
    Statement(s1)
else:
    statement(s2)

statement(s1) is executed, when the condition(c) is “true” and statement(s2) is executed, when the condition(c) is “false”.

Example
  1. n=10  
  2. if n%2==0:  
  3.      print("n is even number")  
  4. else:  
  5.      print("n is odd")  
Output



Explanation

In the example, shown above, n=10 checks the condition of if statement, its true and print “n is even” and if statement is false, else statement is executed.

elif statement 

We can say “elif” statement as a ladder. It’s used to execute one statement among the many statements, according to the condition.
 
Flow chart



Syntax of elif statement

if condition(c1):
     statement(s1)

elif condition(c2):
     statement(s2)
elif condition(c3)
     statement(s3)

else:
     statement(s4)

Example
  1. n = int(input("enter a month number"))  
  2. if n == 1:  
  3.     print("jan")  
  4. elif n == 2:  
  5.     print("feb")  
  6. elif n == 3:  
  7.     print("march")  
  8. elif n == 4:  
  9.     print("april")  
  10. elif n == 5:  
  11.     print("may")  
  12. elif n == 6:  
  13.     print("june")  
  14. elif n == 7:  
  15.     print("july")  
  16. elif n == 8:  
  17.     print("august")  
  18. elif n == 9:  
  19.     print("september")  
  20. elif n == 10:  
  21.     print("october")  
  22. elif n == 11:  
  23.     print("november")  
  24. elif n == 12:  
  25.     print("december")  
  26. else :  
  27.     print("wrong input try again!")  
Output



Explanation

The program, given above, prints the month, according to an input.

elif checks the condition, until condition is not satisfied and when the condition is satisfied, it terminates whole “elif” statement
 
Nested if statement

Nested if statement is “if statement inside the if statement”. It checks the condition’s upper condition and if it is true, then inside, if condition is checked.

Flowchart



Syntax

if condition:
    statement
    if condition:
        statement
else:
    statement

Example
  1. n = int(input("enter a number"))  
  2.   
  3. if n % 2 == 0:  
  4.     print("even number")  
  5.   
  6.     if n > 0:  
  7.        print("\npositive number")  
  8.   
  9.     else :  
  10.        print("\nnegative number")  
  11.   
  12. else :  
  13.     print("odd number")  
Output


 
Explanation

The program, given above, first checks the condition for even or odd, if the number is even then “inner if” statement checks whether the number is negative or positive.

Summary

In this article, you learnt how to use decision making statement in Python.