TalentJR

Lesson 13

Nested If Statements

Make layered decisions

1What Are Nested If Statements?✨

Nested if statements are used when you need to make decisions inside another decision. This allows you to create more complex logic in your programs. Think of it as asking a question within another question!

2Why Use Nested If Statements?🎯

  • ✓ Handle multiple conditions
  • ✓ Create layered decision-making
  • ✓ Improve program flexibility
  • ✓ Allow for more detailed user interactions

3Basic Structure of Nested If Statements💡

A nested if statement includes an if statement inside another if or else statement. Here's a simple example:

💻 code
age = 15
if age >= 13:
    print("You're a teenager!")
    if age >= 18:
        print("You're also an adult!")
    else:
        print("Enjoy your teenage years!")

4Understanding the Example🚀

In this code, we first check if the person is a teenager. If they are, we then check if they're also an adult. This layered checking helps us provide more specific messages based on the age.

5Creating Your Own Nested If Statement🔍

Try creating a program that asks for a grade and gives feedback based on the score:

💻 code
grade = 85
if grade >= 60:
    print("You passed!")
    if grade >= 90:
        print("Excellent work!")
    else:
        print("Good job, keep it up!")
else:
    print("You need to study harder.")

6Practice with Conditions⭐

Experiment with different grades to see how the feedback changes. This helps you understand how nested conditions work and allows for tailored responses!

7Next Steps🧩

Now that you understand nested if statements, think about how you can use them in your projects. They are perfect for games, quizzes, and any interactive application where you need to respond to user input!

🎯 Practice Challenges

Master Nested If Statements by completing all 3 difficulty levels

🟢 EASY

Nested If Statements - Easy Challenge

Start with the basics. Perfect for beginners!

🟡 MEDIUM

Nested If Statements - Medium Challenge

Get more challenging. Build your confidence!

🔴 HARD

Nested If Statements - Hard Challenge

Master this concept. Prove your skills!

🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →

Ready to Master Nested If Statements? 🚀

Complete the challenges, take the quiz and keep your learning streak going!