coding180 icon
Coding180
Nested If Statements: When and How to Use Them

Nested If Statements: When and How to Use Them

Home > Tutorials > Python for beginners > Python Conditional Looping


Conditional statements are an essential part of programming, and they allow you to execute specific blocks of code based on certain conditions. However, sometimes a single condition is not enough to decide what action to take. That's when we need nested if statements. In this article, we'll explore nested if statements in Python and cover examples and exercises that will help you understand their usage.

What are Nested If Statements?

A nested if statement is an if statement inside another if statement. The nested if statement allows you to check for additional conditions before executing a block of code.

For example:

x = 15

if x > 10:
    if x < 20:
        print("x is between 10 and 20")

In this example, we have two if statements. The first if statement checks if x is greater than 10. If that's true, the second if statement checks if x is less than 20. If both conditions are true, then the print statement is executed.

When to Use Nested If Statements

You can use nested if statements whenever you need to test multiple conditions. For example, if you're building a login system, you might want to check if the user entered a valid username and password. Here's an example:

username = "john"
password = "secret"

if username == "john":
    if password == "secret":
        print("Login successful")
    else:
        print("Incorrect password")
else:
    print("Invalid username")

In this example, we have three possible outcomes. If both the username and password are correct, the message "Login successful" is printed. If the username is correct but the password is incorrect, the message "Incorrect password" is printed. Finally, if the username is invalid, the message "Invalid username" is printed.

Exercises

Let's try some exercises to practice using nested if statements.

Exercise 1

Write a program that asks the user for their age. If the user is over 18, the program should ask them if they want to continue. If the user enters "yes", print "Access granted". If the user enters "no", print "Access denied". If the user enters anything else, print "Invalid input".

age = int(input("Enter your age: "))

if age > 18:
    choice = input("Do you want to continue? ")

    if choice == "yes":
        print("Access granted")
    elif choice == "no":
        print("Access denied")
    else:
        print("Invalid input")
else:
    print("You're too young!")

In this exercise, we're checking two conditions - age and user response. If both conditions are met, we print "Access granted." Otherwise, we print an appropriate message.

Exercise 2

Write a program that asks the user to enter a number between 1 and 10. If the user enters a number less than 1 or greater than 10, print "Invalid input". Otherwise, print "Correct input".

num = int(input("Enter a number between 1 and 10: "))

if num >= 1:
    if num <= 10:
        print("Correct input")
    else:
        print("Invalid input")
else:
    print("Invalid input")

In this exercise, we're checking if the user entered a valid number between 1 and 10. If the condition is true, we print "Correct input." Otherwise, we print "Invalid input."

Conclusion

Nested if statements are useful when you need to test multiple conditions. By using nested if statements, you can create more complex programs that can handle different scenarios. Keep practicing and experimenting with nested if statements to improve your programming skills!


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel