0% completed
A nested if statement is an if statement inside another if statement. It allows checking multiple conditions in a structured way. The inner if block runs only if the outer if condition is True. This is useful when one condition depends on another before executing a block of code.
if condition1: if condition2: # Code executes if both condition1 and condition2 are True else: # Code executes if condition1 is True but condition2 is False else: # Code executes if condition1 is False
if is checked first.True, the inner if is evaluated.True, its block runs; otherwise, the else block under it executes.if is False, the entire inner block is skipped.age is 20, and has_license is True.if age >= 18 is True, so Python checks the inner if has_license.has_license is True, the program prints:
You can drive.
has_license were False, the inner else would run, printing:
You need a driving license.
age were less than 18, the outer else would execute.marks is 85.if marks >= 50 is True, so "You passed the exam." is printed.if marks >= 80 is also True, so "You passed with distinction!" is printed.marks were 60, only "You passed the exam." would be printed.marks were below 50, the outer else would run, printing "You failed the exam."A nested if statement helps check conditions that depend on previous conditions. The outer if must be True for the inner if to execute. This structure is useful in real-world situations like verifying eligibility, granting permissions, or checking multiple conditions step by step.
.....
.....
.....