Decision Making Statements - Notes
Decision Making Statements - Notes
if statement
Syntax:
if condition:
<True - Statements to be executed>
Example 1:
a=int(input("Enter 1st number: "))
b=int(input("Enter 2nd number: "))
if(b!=0):
c=a/b
print(f"{a} / {b} = {c}")
Output:
Enter 1st number : 10
Enter 2nd number : 5
10 / 5 = 2.0
****************************************************
Example 2: Program to find the Greatest among 3 Numbers
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = int(input("Enter C: "))
Output:
Enter 3 Numbers separated by space: 50 20 45
A is the Greatest
----------------------------------------------------------------------------------------
if-else statement
To be used when we want a single condition to be tested and take the program’s
flow according to the condition’s result.
Syntax:
if condition:
statement(s) – To be executed when condition is True
else:
Statement(s) – To be executed when condition is False
Example 1:
a=int(input("Enter 1st number: "))
b=int(input("Enter 2nd number: "))
if(b!=0):
c=a/b
print(f"{a} / {b} = {c}")
else:
print("Cannot divide a Number by Zero")
****************************************************
Example 2: Program to find whether the given number is Odd or Even
num=int(input("Enter a number: "))
if(num%2==0):
print(f"{num} is Even Number")
else:
print(f"{num} is Odd Number")
****************************************************
Output:
Enter a number: 25
25 is Odd Number
****************************************************
Example 3: Program to find whether the given number is Positive or Negative
num=int(input("Enter a number: "))
if(num>0):
print(f"{num} is Positive Number")
else:
print(f"{num} is Negative Number")
Output:
Enter a number: 25
25 is Odd Number
----------------------------------------------------------------------------------------
elif statement
In Python, elif ladder statement is the same as an else-if statement of other
programming languages. When you want a decision to be made based on multiple
options, then we can go for the elif statements.
Syntax:
if condition_1:
Statements to be executed – when condition1 is True
elif condition_2:
Statements to be executed – when condition2 is True
elif condition_3:
Statements to be executed – when condition3 is True
elif condition_N:
Statements to be executed – when condition N is True
else:
Statements to be executed – when all conditions fails.
****************************************************
Example 2: Gift for Father
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: Son you are a waste fellow !")
Output:
Father: How much marks you got in exam ?
Son : 45
Father: Son you are a waste fellow !
----------------------------------------------------------------------------------------
Example 1:
n1 = int(input("Enter the 1st Subject Marks: "))
n2 = int(input("Enter the 2nd Subject Marks: "))
n3 = int(input("Enter the 3rd Subject Marks: "))
if n1 >= 40:
if n2 >=40:
if n3 >= 40:
print("RESULT - PASS") Output:
else: Enter the 1st Subject Marks: 98
print("RESULT - FAIL in 3rd Subject") Enter the 2nd Subject Marks: 87
else: Enter the 3rd Subject Marks: 90
print("RESULT - FAIL in 2nd Subject") RESULT - PASS
else:
print("RESULT - FAIL in 1st Subject")
****************************************************
Example 2: Program to check greatest number among three numbers
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): Output:
print("%d is greatest number” % a) Please Enter First number: 25
else: Please Enter Second number: 10
print("%d is greatest number" %c) Please Enter Third number: 45
elif(b>c): 45 is greatest number
print("%d is greatest number" %b)
else:
print("%d is greatest number" %c)
----------------------------------------------------------------------------------------