📚

All Subjects

 > 

⌨️ 

AP Comp Sci P

 > 

📱

Big Idea 3: Algorithms and Programming

Nested Conditionals

1 min read•november 16, 2020

minnachow

Minna Chow


AP Computer Science Principles ⌨️

Bookmarked 2.3k • 69 resources
See Units

Nested conditional statements are conditional statements inside conditional statements. Within an if statement, you have another.

Nested Conditional Example

strawberries_in_fridge = 7 number_of_eggs = 12 if strawberries_in_fridge >= 7: print ("You can make strawberry shortcake!") if number_of_eggs =/ 12: print ("... if you go to the store first.") else: print ("So start baking!")
In this example, the program returns:
You can make strawberry shortcake! So start baking!
In this example, your program would return the line "You can make strawberry shortcake!" regardless of how many eggs you have because you have enough strawberries. It's only after this is confirmed that your program looks at the nested if statement.
It's important to note that nested if statements are part of the larger if statement themselves: none of the code would run if strawberries_in_fridge was less than 7. (This also means you need to be careful about your spacing when you write nested if statements to ensure that everything nests correctly.)

Was this guide helpful?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
🔍 Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
🕹Big Idea 1: Creative Development
⚙️Big Idea 2: Data
📱Big Idea 3: Algorithms and Programming
🖥Big Idea 4: Computer Systems and Networks
⌨️Big Idea 5: Impact of Computing
✏️Blogs
📝Exam Prep
Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
💪🏽 Are you ready for the AP CSP exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ