Nested if Statements

A Nested If Statement is a conditional statement where one if statement is written inside another if statement.

The inner block executes only when the outer condition is True.

This type of statement is commonly used for handling complex logic in programs.

What Does Nested Mean

The word:

Nested

means:

One thing inside another thing

Similarly, in Python:

  • one if statement inside another if statement
  • is called a Nested If Statement

Nested If Statements in Python

Nested if statements are an important concept in Python programming used for checking conditions inside another condition.
They help programs make more detailed and complex decisions.

In real-world applications, many situations require checking one condition first and then checking another condition inside it.

For example:

  • First check whether the username is correct
  • Then check whether the password is correct

This type of logic is implemented using Nested If Statements.

Why Nested If Statements are Important

Nested if statements are important because they help programs:

  • check multiple levels of conditions
  • improve security systems
  • create complex decision-making
  • validate user information
  • build real-world applications

Explanation

  • First, outer condition is checked
  • If True, inner condition is checked
  • If both are True, the code runs
  • If outer is False, inner is skipped
  • Used when decisions depend on each other

Real-Time Applications of Nested If Statements

Application Usage
Banking System PIN verification and balance checking
Login Systems Username and password validation
School Management Admission eligibility
Online Shopping Coupon and payment verification
Gaming Applications Unlocking advanced levels



Syntax

if condition1:
    if condition2:
        statements

Explanation of Syntax

Part Meaning
condition1 Outer condition
condition2 Inner condition
statements Executes when both conditions are True

How Nested If Statement Works

Step 1

Python first checks the outer condition.


Step 2

If the outer condition is True:

  • Python moves to the inner if statement
  • then checks the inner condition

Step 3

If the outer condition is False:

  • Python skips the inner if statement completely

Example 1

marks = 75
if marks >= 50:
    if marks >= 70:
        print("Good marks")

Output:

Good marks

Example 2

username = "admin"
password = "1234"

if username == "admin":
    if password == "1234":
        print("Login successful")
    else:
        print("Wrong password")
else:
    print("Wrong username")

Output:

Login successful

Explanation

  • Outer condition is True
  • Inner condition is False
  • So "Hello" is skipped
  • "Hi" executes because it is outside the nested block

Important Point

Proper indentation is very important in nested if statements.

Python uses indentation to identify:

  • outer block
  • inner block

Proper indentation is very important in nested if statements.

Python uses indentation to identify:

  • outer block
  • inner block

Common Beginner Mistakes

Mistake Problem
Wrong indentation Indentation error
Forgetting colon : Syntax error
Using = instead of == Logical error
Incorrect nesting Unexpected output

➤ Summary

Nested if statements in Python are used when one condition depends on another condition.

Important Points

  • One if statement inside another if statement
  • Python first checks the outer condition
  • Then Python checks the inner condition
  • Indentation is very important in nested if statements

Nested if statements are widely used in:

    • login systems
    • banking applications
    • school admission systems
    • gaming applications
    • security systems

Check your knowledge

Quickly verify what you've learned from this tutorial.

Question 1

What is a nested if statement?

A nested if is an if inside another if.

Question 2

When does the inner if execute?

Inner block runs only when outer condition is True.

Question 3

What is the output?

x = 10

if x > 5:
    if x > 8:
        print("Yes")

Both conditions are True → prints Yes.

Question 4

What is the output?

x = 4

if x > 5:
    if x > 2:
        print("Hello")

Outer condition is False → inner block is skipped.

Question 5

What is the output?

username = "admin"
password = "0000"

if username == "admin":
    if password == "1234":
        print("Login successful")
    else:
        print("Wrong password")
else:
    print("Wrong username")

Username is correct but password is wrong → prints Wrong password.

Congratulations!

You've successfully mastered the knowledge check for "Nested if Statements."

For more questions and practice, click the link below:

Practice More Questions
Previous Topic if–elif–else Statement Next Topic Introduction to Loops