Making Decisions with Conditionals

Conditionals allow programs to make decisions based on boolean logic. Mastering if/elif/else statements is crucial for creating dynamic, responsive code.

Conditional Structure

age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
Adult

Key points:

  • Conditions evaluated top-to-bottom
  • First true condition triggers its block
  • else catches all remaining cases

Multiple Conditions

score = 85
if score >= 90:
grade = "A"
elif score >= 75:
grade = "B"
elif score >= 60:
grade = "C"
else:
grade = "F"
B

elif (else-if) allows checking multiple exclusive conditions in sequence.

Common Pitfalls

# Incorrect comparison order
if n > -10:
...
elif n > 0: # Never reached
# Missing colon
if n == 0 # SyntaxError

Your Task: Number Classifier

Implement logic to categorize numbers:

  1. If n > 0 → "positive"
  2. If n < 0 → "negative"
  3. Else → "zero"

Test edge cases: n = 0, large numbers, negative zero

Practical Use: Temperature Alert

temp = -5.5
if temp <= 0:
status = "FREEZING ⚠️"
elif temp <= 10:
status = "Cold"
elif temp <= 25:
status = "Mild"
else:
status = "Hot 🔥"
FREEZING ⚠️
Section 1/5Conditional Structure

Conditional Structure

Key points:Conditions evaluated top-to-bottomFirst true condition triggers its blockelse catches all remaining cases

Making Decisions with Conditionals

Conditionals allow programs to make decisions based on boolean logic. Mastering if/elif/else statements is crucial for creating dynamic, responsive code.

Conditional Structure

age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
Adult

Key points:

  • Conditions evaluated top-to-bottom
  • First true condition triggers its block
  • else catches all remaining cases

Output:

Click "Check" to run your code.