If-Else, If-Elif-Else & Nested If-Else

Three conditional structures, built up step by step through a login/authentication simulator โ€” from a single check to three levels of nesting.

Overview

This topic covers three related conditional structures in Python, building up in complexity:

  1. if-else โ€” a simple two-way decision (do this, or do that).
  2. if-elif-else โ€” a multi-way decision with several distinct conditions checked in order.
  3. Nested if-else โ€” placing an if-else (or if-elif-else) block inside another if-else block, so a second condition is only checked after a first condition has already been satisfied (or failed).

All three are demonstrated together using a login/authentication simulator example, showing how the logic evolves from a basic check to a more capable, multi-step validation flow.

The running example throughout is a login/authentication simulator:

Key Concepts

Detailed Explanation

Version 1 โ€” Simple if-else (single condition check)

# correct email- campusx@gmail.com
# password - 1234

email = input("Apna email bata")
password = input("Apna passowrd bhi bata")

if email == "campusx@gmail.com" and password == "1234":
    print("Welcome")
else:
    print("Incorrect credentials")

How it works:

Example run:

Apna email bata: campusx@gmail.com
Apna passowrd bhi bata: 1234
Welcome
โš ๏ธ Limitation: This version can't distinguish between "wrong email" and "correct email but wrong password" โ€” it treats both as the same generic failure. This motivates moving to nested if-else.

Version 2 โ€” If-Elif-Else with a Nested If-Else Inside It

# correct email- campusx@gmail.com
# password - 1234

email = input("Apna email bata")
password = input("Apna passowrd bhi bata")

if email == "campusx@gmail.com" and password == "1234":
    print("Welcome")
elif email == "campusx@gmail.com" and password != "1234":
    print("Password Incorrect")
    password = input("Password fir se bol")
    if password == "1234":
        print("Finally correct")
    else:
        print("Still incorrect")
else:
    print("Incorrect credentials")

How it works โ€” step by step:

This version is an if-elif-else structure at the top level, with a small nested if-else tucked inside the elif branch.

  1. if (first condition): email correct and password correct โ†’ "Welcome".
  2. elif (second condition): email correct but password wrong โ†’ enters this branch.
    • Prints "Password Incorrect".
    • Asks the user to re-enter the password with a second input().
    • Nested if-else: checks the new password.
      • If it now matches "1234" โ†’ "Finally correct".
      • If it's still wrong โ†’ "Still incorrect".
  3. else (fallback): if the email itself was wrong (regardless of password) โ†’ "Incorrect credentials".

Important points:

Version 3 โ€” Nested If-Else Wrapping an If-Elif-Else (multi-level nesting)

A further extended version (seen in the full code) wraps the entire if-elif-else from Version 2 inside an outer if-else that first validates the email format:

# correct email- campusx@gmail.com
# password - 1234

email = input("Apna email bata")
if '@' in email:
    password = input("Apna passowrd bhi bata")

    if email == "campusx@gmail.com" and password == "1234":
        print("Welcome")
    elif email == "campusx@gmail.com" and password != "1234":
        print("Password Incorrect")
        password = input("Password fir se bol")
        if password == "1234":
            print("Finally correct")
        else:
            print("Still incorrect")
    else:
        print("Incorrect credentials")
else:
    print("Email galat hai sahi likho")

How this differs from Version 2:

This demonstrates three levels of nesting stacked together:

Traced Example Runs

Email enteredPassword enteredOutput
campusx@gmail.com1234Welcome
campusx@gmail.com12535 (then re-entered 1234)Password Incorrect โ†’ Finally correct
campusx@gmail.com315236 (then re-entered rjyyjr)Password Incorrect โ†’ Still incorrect

Trace for the "Finally correct" example

Apna email bata: campusx@gmail.com
Apna passowrd bhi bata: 12535
Password Incorrect
Password fir se bol: 1234
Finally correct

Email matched โ†’ entered elif branch (password didn't match 1234 the first time). Prompted again, user typed 1234 correctly this time โ†’ nested if triggered โ†’ "Finally correct".

Trace for the "Still incorrect" example

Apna email bata: campusx@gmail.com
Apna passowrd bhi bata: 315236
Password Incorrect
Password fir se bol: rjyyjr
Still incorrect

Same path, but the second password attempt was also wrong โ†’ nested else triggered โ†’ "Still incorrect".

Things to Remember

Quick Revision

In the login example, all three come together: the outer if-else validates the email format ('@' in email); nested inside it, an if-elif-else validates the email+password combination; and nested inside the elif (correct email, wrong password), a third-level if-else gives the user a second chance to type the password correctly.