scoresvideos
AP Computer Science Principles
One-page, printable cheatsheet
Cheatsheet visualization
Find gaps with guided practice
Guided practice grid visualization
Table of Contents

⌨️ap computer science principles review

3.7 Nested Conditionals

Verified for the 2025 AP Computer Science Principles examCitation:

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.)

Key Terms to Review (1)

Nested Conditional Statements: Nested conditional statements refer to the practice of placing one conditional statement inside another. This allows for more complex decision-making in a program by evaluating multiple conditions.