Introduction
Nested conditional execution helps you make decisions inside other decisions. It lets your program check more than one condition step by step.
Jump into concepts and practice - no test required
Nested conditional execution helps you make decisions inside other decisions. It lets your program check more than one condition step by step.
if condition1: if condition2: # code if both conditions are true else: # code if condition1 is true but condition2 is false else: # code if condition1 is false
Indentation is important to show which code belongs inside each condition.
You can nest as many if statements as you need, but keep it clear.
x is positive, then checks if it is even or odd.x = 10 if x > 0: if x % 2 == 0: print("Positive even number") else: print("Positive odd number")
age = 15 if age < 18: if age < 13: print("Child") else: print("Teenager") else: print("Adult")
This program assigns a grade based on the score using nested conditions.
score = 85 if score >= 60: if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") else: print("Grade: C") else: print("Failed")
Too many nested conditions can make code hard to read. Try to keep it simple.
You can use elif inside nested conditions to check multiple cases.
Nested conditionals let you check one condition inside another.
Indentation shows which code belongs to which condition.
Use nested conditionals to handle complex decisions step by step.
if inside an if.if or else inside another if or else correctly describes this as an if or else inside another if or else. Other options describe different or incorrect ideas.if or else inside another if or else -> Option Aif inside if [OK]if and proper indentation for nested blocks.if correctly. Options A and B miss colons or indentation. if x > 0:
print('x is positive')
else if x < 10:
print('x is less than 10') uses invalid else if instead of elif.score = 85
if score >= 90:
print('Grade A')
else:
if score >= 80:
print('Grade B')
else:
print('Grade C')num = 5
if num > 0:
if num < 10:
print('Number is between 1 and 9')