0% found this document useful (0 votes)
6 views7 pages

Conditions

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)
6 views7 pages

Conditions

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/ 7

So far the statements in all our programs got executed sequentially

or one after the other.


Sequence of execuation of instruction in a program can be altered
using:-
1)Decision Control Instruction
2)Repetition Control instruction

Decision Control Instruction:-


Three ways for taking decisions in a programs
a) if condition:
statements1
statements2

b) if condition:
statements1
statements2
else:
statements3
statements4

c) if condition1:
statements1
statements2
elif condition2:
statements3
statements4
elif condition3:
statements5
statements6
else:
statements7

-The colon(:) after if, else, elif. It is compulsory.


-Statements in if block, else block, elif block have to be an
intended. Indented statements are treated as a block of
statements.
-Indentation is used to group statements. Use either 4 spaces or
a tab for indentation. Don’t mix tabs and spaces.

- In if condition else and elif are optional


- In if else condition when if condition is True then all the
statements in if block get executed. If condition is False then
statements in else block get executed.
- In elif condition when if condition fails then condition in the
following elif block is checked. The else block goes to work if
all conditions are fail.
- If-else statements can be nested. Nesting can be as deep as
the programs logic demands.

Conditions is built using relational operators such as


<, >, <=, >=, ==, !=

Example:-
10<20 #True
‘gang’ < ‘God’ # False because Lower case > uppercase.

A=’b’ is an assignment but a==b is an comparison


Elif condition:---
Example 1: Checking multiple conditions for a number
number = int(input("Enter a number: "))
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")

Example 2: Categorizing the temperature


temperature = int(input("Enter the temperature: "))
if temperature > 30:
print("It's hot outside.")
elif 20 <= temperature <= 30:
print("The weather is moderate.")
elif temperature<20:
print("It's cold outside.")

recipient
Example 3: Determining the grade based on a score
score = float(input("Enter the score: "))
if score >= 90:
grade = 'A'
elif 80 <= score < 90:
grade = 'B'
elif 70 <= score < 80:
grade = 'C'
else:
grade = 'D'

print(f"The grade is: {grade}")

Example 4: Identifying the day of the week


day = input("Enter the day of the week: ").lower()
if day == 'monday':
print("It's the start of the week.")
elif day == 'friday':
print("It's Friday.")
else:
print("It's a regular day.")
Example 6: Checking eligibility for voting
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
elif 16 <= age < 18:
print("You can register to vote soon.")
else:
print("You are not eligible to vote.")

Example 8: Determining eligibility for a discount based on purchase


amount
purchase_amount = float(input("Enter the purchase amount: "))
if purchase_amount >= 1000:
discount = 0.1
elif 500 <= purchase_amount < 1000:
discount = 0.05
else:
discount = 0

final_amount = purchase_amount - (purchase_amount * discount)


print(f"Discounted amount: {final_amount}")
Example 9: Identifying the type of angle
angle = int(input("Enter the angle in degrees: "))
if angle == 90:
print("It's a right angle.")
elif angle < 90:
print("It's an acute angle.")
elif angle>90:
print("It's an obtuse angle.")
else:
print(“Invalid Angle degree”)

Example 10: Checking the validity of a date


day = int(input("Enter the day: "))
month = int(input("Enter the month: "))
year = int(input("Enter the year: "))

if 1 <= day <= 31 and 1 <= month <= 12:


print("The date is valid.")
else:
print("The date is invalid.")

You might also like