A nested
if statement
is an
if statement
placed inside another
if block.
This allows Python to make decisions in multiple layers, where an inner
condition is evaluated only if the outer condition is true.
Nested conditions are useful when one decision depends on the result of
another.
Basic Nested if Example
Example: Checking Number Ranges
How Nested if Statements Work
Python evaluates nested conditions from the outside inward. If the outer
condition fails, the inner block is skipped entirely.
Example: Eligibility Check
Multiple Levels of Nesting
You can nest multiple
if
statements, but excessive nesting may reduce readability. Always aim for
clarity.
Example: Academic Evaluation
Nested if vs Logical Operators
Sometimes nested conditions can be simplified using logical operators like
and. Both
approaches are valid—choose based on clarity and complexity.
Use nested
if when
conditions have dependent logic
Use logical operators when conditions are simple and equally important