Nested if Statements

A nested if statement is an if inside another if.
It is used to check conditions within conditions, making it useful for multi-level decision making.

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

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

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 Scenarios

  • If login correct → then check password
  • If ticket available → then check ID
  • If exam passed → then check grade
  • If ATM card valid → then check PIN
  • If user registered → then allow access



Syntax

if condition1:
    if condition2:
        # block

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 checks username
  • Inner condition checks password
  • Both must be True for successful login

Why Nested if is Important

  • Helps handle multiple related conditions
  • Improves logical control
  • Used in real-world systems like login and security
  • Makes programs more structured

Common Beginner Mistakes

  •  Too many nested levels
  • Wrong indentation
  • Missing conditions
  • Not understanding execution flow

➤ Key Points

  • Nested if means an if inside another if
  • Inner condition runs only if outer is True
  • Used for complex conditions
  • Helps in better decision making

➤ Tips

  • Avoid too many nested levels (keep it simple)
  • Use proper indentation
  • Combine conditions using and / or when possible
  • Practice with real-life examples

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