Conditional Statements
Introduction
Conditional statements allow decision-making in programs. They control the flow of
execution based on conditions.
If Statement
x = 10
if x > 5:
print("x is greater than 5")
If-Else Statement
x=3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
If-Elif-Else
x=0
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
Logical Operators
- and, or, not
age = 20
if age > 18 and age < 30:
print("Young adult")
Use Cases
- Making decisions in games.
- Validating user input.
- Controlling access in applications.
Common Mistakes
- Using = instead of == in conditions.
- Forgetting proper indentation.
Practice Questions
1. Write a program that checks if a number is even or odd.
2. Write a program that determines the largest of three numbers.
3. Write a program to classify a grade (A, B, C, D, F) based on marks.