Nested-if statement in Python

Last Updated : 11 Aug, 2026

A nested if statement is an if statement placed inside another if or else block. It is used when a decision depends on multiple conditions that need to be checked one after another.

Example: Here, we use a nested if statement to determine whether a student is eligible for a scholarship.

Python
marks = 92
attendance = 88

if marks >= 90:
    if attendance >= 85:
        print("Scholarship Approved")
    else:
        print("Attendance requirement not met")
else:
    print("Marks requirement not met")

Output
Scholarship Approved

Explanation: outer if checks whether marks are at least 90. If this condition is True, the inner if checks whether attendance is at least 85. Since both conditions are satisfied, "Scholarship Approved" is printed.

Syntax

if condition1:
if condition2:
# Code

Parameters:

  • condition1: The outer condition that is evaluated first.
  • condition2: The inner condition that is evaluated only if condition1 is True.

Below flowchart illustrates the concept of a nested if statement in programming:

nested
Nested If Statement in Python

A nested if statement is executed in the following order:

  • The outer if condition is evaluated first.
  • If the outer condition is True, the inner if condition is evaluated.
  • If both conditions are True, the nested block of code executes.
  • If the outer condition is False, the inner if statement is skipped, and the corresponding else block (if present) is executed.

Examples

Example 1: Here, we use a nested if statement to verify whether a user can access an account.

Python
username = "Emma"
password = "python123"

if username == "Emma":
    if password == "python123":
        print("Login Successful")
    else:
        print("Incorrect Password")
else:
    print("Invalid Username")

Output
Login Successful

Explanation: outer if checks whether the username is correct. If it is, the inner if verifies the password. Since both conditions are True, "Login Successful" is printed.

Example 2: Here, we use a nested if statement to determine whether a customer receives a premium discount.

Python
purchase_amount = 1200
premium_member = True

if purchase_amount >= 1000:
    if premium_member:
        print("25% Discount Applied")
    else:
        print("10% Discount Applied")
else:
    print("No Discount")

Output
25% Discount Applied

Explanation: outer if checks whether the purchase amount is at least 1000. If the condition is True, the inner if checks the membership status. Since both conditions are satisfied, the customer receives a 25% discount.

Example 3: Here, we use a nested if statement to check whether a user is allowed to open a file.

Python
file_exists = True
has_permission = True

if file_exists:
    if has_permission:
        print("File Opened")
    else:
        print("Access Denied")
else:
    print("File Not Found")

Output
File Opened

Explanation: outer if checks whether the file exists. If it does, the inner if verifies whether the user has permission to access it. Since both conditions are True, the file is opened.

Comment