Nested Conditionals

Help Questions

AP Computer Science Principles › Nested Conditionals

Questions 1 - 10
1

Given this shopping-cart pseudo-code, what will the discount be if isMember=false, total=160, isSeasonSale=true?

// Inputs: isMember, total, isSeasonSale

IF isMember

IF total >= 100

IF isSeasonSale

  discount = 0.20

ELSE

  discount = 0.10

ELSE

discount = 0.05

ELSE

IF isSeasonSale AND total >= 150

discount = 0.15

ELSE

discount = 0.00

0.00

0.10

0.15

0.20

Explanation

This question tests understanding of nested conditionals in AP Computer Science Principles, specifically evaluating how non-member paths execute. Nested conditionals allow programs to make complex decisions by checking conditions within conditions, creating different execution paths based on multiple criteria. In this scenario, with isMember=false, the code immediately branches to the ELSE block, bypassing all member-specific discounts and checking only non-member conditions. Choice B is correct because when isMember is false, total is 160 (>= 150), and isSeasonSale is true, the condition 'isSeasonSale AND total >= 150' evaluates to true, resulting in discount = 0.15. Choice C is incorrect because 0.10 is only available to members with total >= 100 when it's not a season sale. To help students: Draw flowcharts showing how false conditions immediately skip entire nested blocks, practice with concrete values to see which paths execute, and emphasize that AND requires both conditions to be true. Highlight that understanding which blocks are skipped is as important as knowing which execute.

2

Given the conditions in the pseudo-code, what will the output be if timeOfDay="day", traffic="high", hasPedestrian=true?

// Inputs: timeOfDay, traffic, hasPedestrian

IF timeOfDay == "night"

PRINT "FLASHING"

ELSE

IF traffic == "high"

PRINT "GREEN"

ELSE

IF hasPedestrian

  PRINT "WALK"

ELSE

  PRINT "RED"

RED

FLASHING

GREEN

WALK

Explanation

This question tests understanding of nested conditionals in AP Computer Science Principles, focusing on how early conditions can override later ones in the decision tree. Nested conditionals evaluate conditions in order, and once a condition is met and an action is taken, subsequent conditions in that branch are not evaluated. The traffic light code first checks time of day, then traffic levels, with pedestrian considerations only relevant in specific circumstances. Choice C is correct because with timeOfDay="day" (not night) and traffic="high", the code prints "GREEN" immediately after the traffic check, never reaching the hasPedestrian condition. Choice A is incorrect because "WALK" would require traffic to NOT be "high", but here traffic is explicitly "high", causing an earlier exit. To help students: Demonstrate that the order of conditions matters, show how some inputs make certain code sections unreachable, and use flowcharts with highlighted paths. Emphasize that in nested conditionals, the first matching condition "wins" and prevents further evaluation in that branch.

3

Which sequence of conditions must be met for the traffic light to set signal="WALK"?

// Inputs: hasPedestrian, timeOfDay, traffic

IF timeOfDay == "night"

signal = "FLASHING"

ELSE

IF traffic == "high"

signal = "GREEN"

ELSE

IF hasPedestrian

  signal = "WALK"

ELSE

  signal = "RED"

not night, traffic not high, hasPedestrian true

night and traffic not high

night and hasPedestrian true

not night, traffic high, hasPedestrian true

Explanation

This question tests understanding of nested conditionals in AP Computer Science Principles, specifically identifying the exact path through multiple decision points. Nested conditionals create branching logic where reaching certain outcomes requires specific combinations of conditions. The traffic light system first checks time of day, then traffic levels, and finally pedestrian presence to determine the appropriate signal. Choice B is correct because signal="WALK" only occurs when: timeOfDay is NOT "night" (to pass the first condition), traffic is NOT "high" (to reach the inner ELSE), and hasPedestrian is true (to trigger the WALK signal). Choice C is incorrect because if traffic is "high", the signal would be set to "GREEN" immediately, never reaching the pedestrian check. To help students: Work backwards from the desired outcome, identify all conditions that must be true or false, and use process of elimination. Teach students to recognize that ELSE branches represent the negation of the IF condition, making "not night" and "traffic not high" necessary conditions.

4

In this shopping cart code, which conditions must be met for a 20% discount?

// Online Shopping Cart discount rules

IF isMember

IF total >= 100

IF isSeasonalSale

  discount = 20

ELSE

  discount = 15

ELSE

discount = 5

ELSE

IF total >= 150

IF isSeasonalSale

  discount = 10

ELSE

  discount = 0

ELSE

discount = 0

Member, total < 100, seasonal sale

Member, total >= 100, seasonal sale

Not a member, total >= 150, seasonal sale

Not a member, total >= 150, not seasonal

Explanation

This question tests understanding of nested conditionals in AP Computer Science Principles, focusing on tracing through multiple decision layers to identify specific outcomes. Nested conditionals create a hierarchical decision structure where inner conditions are only evaluated if outer conditions are met. In this shopping cart code, the program first checks membership status, then purchase amount, and finally seasonal sale status to determine the appropriate discount. Choice B is correct because a 20% discount requires three specific conditions: the customer must be a member (first IF), their total must be at least $100 (second IF), and it must be a seasonal sale (third IF). Choice A is incorrect because non-members cannot receive a 20% discount regardless of other conditions, as this discount is nested within the member branch. To help students master this concept, encourage them to trace through each path systematically using a decision tree diagram, highlighting which conditions must be true at each level. Remind students that in nested structures, all parent conditions must be satisfied before inner conditions are even evaluated.

5

If isMember is true and total is 99 during seasonal sale, what is the discount?

// Online Shopping Cart discount rules

IF isMember

IF total >= 100

IF isSeasonalSale

  discount = 20

ELSE

  discount = 15

ELSE

discount = 5

ELSE

IF total >= 150

IF isSeasonalSale

  discount = 10

ELSE

  discount = 0

ELSE

discount = 0

0%

5%

15%

20%

Explanation

This question tests understanding of nested conditionals in AP Computer Science Principles, focusing on how inner conditions are bypassed when outer conditions fail. Nested conditionals create decision trees where each level depends on the previous level's outcome, allowing for precise control flow based on multiple criteria. In this scenario, the code checks membership status (true), then evaluates if the total meets the $100 threshold (false since 99 < 100), which directs execution to the inner ELSE branch. Choice A is correct because when isMember is true but total < 100, the code follows the path: IF isMember (true) → IF total >= 100 (false) → ELSE → discount = 5, regardless of seasonal sale status. Choice C is incorrect because even though it's a seasonal sale, the 20% discount requires a minimum purchase of $100, which isn't met. To help students grasp this concept, demonstrate how failing an outer condition prevents access to deeper nested benefits, similar to how you can't enter a VIP room's special area without first entering the VIP room itself. Practice with boundary cases like this (99 vs 100) helps students understand the importance of precise condition checking.

6

Which sequence of conditions must be met for a 5% discount?

// Online Shopping Cart discount rules

IF isMember

IF total >= 100

IF isSeasonalSale

  discount = 20

ELSE

  discount = 15

ELSE

discount = 5

ELSE

IF total >= 150

IF isSeasonalSale

  discount = 10

ELSE

  discount = 0

ELSE

discount = 0

Not a member, total >= 150, seasonal

Member, total >= 100, seasonal

Not a member, total < 150

Member, total < 100

Explanation

This question tests understanding of nested conditionals in AP Computer Science Principles, requiring students to identify the specific conditions that lead to a 5% discount. Nested conditionals create a decision tree where each branch represents a different scenario, and understanding these paths is essential for program comprehension. In this code, a 5% discount occurs in exactly one scenario: when a customer is a member but their total purchase is less than $100. Choice B is correct because it describes the path: IF isMember (true) → IF total >= 100 (false) → ELSE → discount = 5, and this discount applies regardless of seasonal sale status. Choice A is incorrect because non-members with total < 150 receive 0% discount, not 5%. To help students master this concept, create visual representations showing how each discount percentage can only be reached through one specific path. Emphasize that the 5% discount is a consolation prize for members who don't meet the spending threshold, encouraging loyalty even for smaller purchases.

7

If isMember=true and isSeasonSale=true are true, what is the discount when total=99?

// Inputs: isMember, total, isSeasonSale

IF isMember

IF total >= 100

IF isSeasonSale

  discount = 0.20

ELSE

  discount = 0.10

ELSE

discount = 0.05

ELSE

IF isSeasonSale AND total >= 150

discount = 0.15

ELSE

discount = 0.00

0.05

0.10

0.15

0.20

Explanation

This question tests understanding of nested conditionals in AP Computer Science Principles, demonstrating how failing an intermediate condition bypasses deeper nested checks. Nested conditionals create a hierarchy where inner conditions are only evaluated if all outer conditions pass, making the order and structure crucial for determining outcomes. In this scenario, while the customer is a member (passing the first check), the total of 99 fails the second condition (total >= 100), preventing access to the innermost seasonal discount logic. Choice C is correct because when isMember is true but total is 99 (< 100), the code executes the ELSE block of the second IF statement, setting discount = 0.05, and never evaluates the isSeasonSale condition at all. Choice A is incorrect because 0.20 requires total >= 100 to even reach the seasonal check, which isn't satisfied with total=99. To help students: Use step-by-step tracing with boxes around each conditional level, practice identifying which conditions are 'gatekeepers' to inner logic, and create test cases at boundaries. Emphasize that in nested structures, you must pass each level to reach the next, like unlocking doors in sequence.

8

Identify the error in the nested conditionals that prevents "Severe Storm" from printing.

// Inputs: isThunderstorm, wind

IF isThunderstorm

IF wind <= 30

PRINT "Severe Storm"

ELSE

PRINT "Storm"

ELSE

PRINT "No Alert"

The ELSE should print "Heat Advisory"

The wind check should use wind >= 30

The code needs a loop to repeat checks

The outer IF should check wind, not isThunderstorm

Explanation

This question tests understanding of nested conditionals in AP Computer Science Principles, focusing on debugging logical errors in condition statements. Nested conditionals require careful attention to comparison operators and logical flow to ensure desired outcomes are achievable. The code attempts to categorize storm severity based on wind speed, but contains a logical error preventing the intended output. Choice A is correct because the condition "wind <= 30" means "Severe Storm" only prints when wind is 30 or LESS during a thunderstorm, which is backwards - severe storms should have HIGH winds, so it should be "wind >= 30". Choice B is incorrect because checking wind first would change the entire logic structure and wouldn't specifically fix the severe storm categorization issue. To help students: Teach them to verify that conditions match real-world logic, use test cases with boundary values, and check that all desired outputs are actually reachable. Emphasize the importance of using the correct comparison operators (<, >, <=, >=) to match the intended logic.

9

What condition would cause the code to follow the alternative path and print "NoDeal"?

// Inputs: isMember, total

IF isMember

IF total >= 50

PRINT "MemberDiscount"

ELSE

PRINT "Member"

ELSE

IF total >= 150

PRINT "GuestDiscount"

ELSE

PRINT "NoDeal"

isMember true and total < 50

isMember false and total < 150

isMember true and total >= 50

isMember false and total >= 150

Explanation

This question tests understanding of nested conditionals in AP Computer Science Principles, specifically identifying conditions that lead to specific outputs. Nested conditionals create multiple pathways through code, and understanding which conditions lead to which outcomes is crucial for debugging and design. The code structure shows two main branches based on membership status, with further decisions based on purchase totals. Choice B is correct because "NoDeal" prints when isMember is false (taking the ELSE path) AND total < 150 (failing the nested condition), which describes a non-member with insufficient purchase amount. Choice A is incorrect because if isMember is true, the code would print either "MemberDiscount" or "Member", never reaching the "NoDeal" option. To help students: Create truth tables showing all possible combinations, highlight the specific path that leads to each output, and practice reverse engineering - starting from the output and working backwards. Remind students that understanding the "alternative path" means following the ELSE branches through the nested structure.

10

Given this game-action pseudo-code, what will the action be if health=40, hasPotion=true, enemyNear=true, hasSpecial=false?

// Inputs: health, hasPotion, enemyNear, hasSpecial

IF enemyNear

IF health < 30

IF hasPotion

  action = "HEAL"

ELSE

  action = "RUN"

ELSE

IF hasSpecial

  action = "SPECIAL"

ELSE

  action = "ATTACK"

ELSE

action = "EXPLORE"

HEAL

ATTACK

EXPLORE

RUN

Explanation

This question tests understanding of nested conditionals in AP Computer Science Principles, focusing on game logic decision-making with multiple factors. Nested conditionals enable complex game AI by evaluating threat levels, resources, and capabilities in a hierarchical manner. The code first checks if an enemy is near, then evaluates health status, and finally considers available actions based on resources. Choice C is correct because with enemyNear=true and health=40 (which is >= 30), the code enters the ELSE branch of the health check, then with hasSpecial=false, it defaults to "ATTACK". Choice A would be incorrect because "HEAL" only occurs when health < 30 AND hasPotion is true, but here health=40 exceeds the threshold. To help students: Create decision trees showing all possible paths, test edge cases like health exactly at 30, and trace through with colored pencils to highlight the actual path taken. Emphasize that game logic often involves checking the most critical conditions first (like enemy presence) before considering other factors.

Page 1 of 3