Program Control Flow
Conditional statements allow a program to make decisions based on whether a condition is True or False. They use **operators** to compare values and combine logic.
Operators Used in Conditions
1. Comparison Operators
Used to compare two values. Result is always a bool (True or False).
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 3 != 4 | True |
> | Greater than | 10 > 2 | True |
< | Less than | 1 < 5 | True |
>= | Greater than or equal | 7 >= 7 | True |
<= | Less than or equal | 6 <= 5 | False |
2. Arithmetic Operators (Used in Expressions)
Often used inside conditions to compute values before comparing.
+(addition),-(subtraction)*(multiplication),/(division)
// (floor division), % (modulo), ** (exponent)total = 10 + 5
if total > 12:
print("Enough")Types of Conditional Statements
1. Unidirectional: if
Runs a block **only if** the condition is True.
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")2. Bidirectional: if-else
Chooses **between two paths**.
temperature = float(input("Current temperature: "))
if temperature > 30:
print("It's hot outside!")
else:
print("It's cool outside.")3. Multidirectional: if-elif-else
Handles **multiple conditions** in order.
score = int(input("Enter your score (0-100): "))
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")Real-World Scenarios
Scenario 1: Login Age Check
user_age = int(input("How old are you? "))
if user_age < 13:
print("Access denied: Must be 13+.")
else:
print("Welcome to the platform!")Scenario 2: Discount Eligibility
total_bill = float(input("Total bill amount: "))
is_member = input("Are you a member? (yes/no): ") == "yes"
if total_bill > 100 and is_member:
print("You get a 20% discount!")
else:
print("No discount applied.")Scenario 3: Grade Classifier (Multi-path)
marks = int(input("Enter marks: "))
if marks == 100:
print("Perfect score!")
elif marks >= 80:
print("Excellent")
elif marks >= 60:
print("Good")
elif marks >= 40:
print("Pass")
else:
print("Fail")