0% found this document useful (0 votes)
25 views4 pages

Decision Making Statements - Notes

Uploaded by

howtoaddme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

Decision Making Statements - Notes

Uploaded by

howtoaddme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DECISION MAKING (or) CONDITIONAL STATEMENTS

if statement

It is the simplest form of decision-making 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: "))

if a > b and a > c:


print("A is the Greatest")
if b > a and b > c:
print("B is the Greatest")
if c > a and c > b:
print("C is the Greatest")

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.

Note: else statement is optional

Example 1: Program to find the Greatest of 2 Numbers


n1 = int(input("Enter the 1st Number: "))
n2 = int(input("Enter the 2nd Number: ")) Output:
if n1 > n2: Enter the 1st Number: 10
print(f"{n1} is greater than {n2}") Enter the 2nd Number: 10
elif n2 > n1: 10 is equal to 10
print(f"{n2} is greater than {n1}")
else:
print(f"{n1} is equal to {n2}")

****************************************************
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 !
----------------------------------------------------------------------------------------

Nested if-else Statements

Having an if condition within another if condition is what is called as Nesting of


conditions.
Syntax:
if condition-1:
if condition-2:
if condition-3:
statement- executed when all the conditions are satisfied
else:
statement- executed when condition-3 Fails
else:
statement- executed when condition-2 Fails
else:
statements- executed when condition-1 Fails

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)

----------------------------------------------------------------------------------------

You might also like