Nested Conditionals
Help Questions
AP Computer Science Principles › Nested Conditionals
In this shopping-cart pseudo-code, which conditions must be met for message "VIP+Season" to print?
// Inputs: isMember, total, isSeasonSale
IF isMember
IF total >= 100
IF isSeasonSale
PRINT "VIP+Season"
ELSE
PRINT "VIP"
ELSE
PRINT "Member"
ELSE
IF total >= 150
IF isSeasonSale
PRINT "Guest+Season"
ELSE
PRINT "Guest"
ELSE
PRINT "NoDeal"
isMember true, total >= 150, isSeasonSale true
isMember false, total >= 100, isSeasonSale true
isMember true, total >= 100, isSeasonSale true
isMember true, total > 100, isSeasonSale false
Explanation
This question tests understanding of nested conditionals in AP Computer Science Principles, focusing on how multiple conditions must be satisfied in sequence. Nested conditionals create a decision tree where each inner condition is only evaluated if the outer condition is true. In this shopping cart scenario, the code checks three levels of conditions: membership status, purchase total, and seasonal sale status. Choice A is correct because to print "VIP+Season", the code must first check if isMember is true, then verify total >= 100, and finally confirm isSeasonSale is true - all three conditions must be met in this exact sequence. Choice D is incorrect because it requires total >= 150, which would actually prevent the code from entering the correct branch since the condition checks for >= 100, not >= 150. To help students: Use indentation to visualize nesting levels, trace through each path systematically, and create test cases for different combinations. Emphasize that in nested conditionals, the order matters - inner conditions are only evaluated when outer conditions are true.
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"
EXPLORE
HEAL
RUN
ATTACK
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.
If score=89, participation=true, attendance=92, what is the finalGrade?
// Start with letter grade from score
IF score >= 90
grade = "A"
ELSE
IF score >= 80
grade = "B"
ELSE
grade = "C"
// Adjust for class habits
IF grade == "B"
IF participation == false
grade = "C"
ELSE
IF attendance < 90
grade = "C"
finalGrade = grade
C
B
No grade assigned
A
Explanation
This question tests understanding of nested conditionals in AP Computer Science Principles, focusing on how variable values can be modified through conditional logic. Nested conditionals here demonstrate a two-phase process: first assigning a base grade, then potentially adjusting it based on additional criteria. The code initially assigns grade "B" because score=89 falls in the 80-89 range, then checks if this B grade should be downgraded based on participation and attendance. Choice B is correct because with participation=true and attendance=92 (which is >= 90), neither condition for downgrading the B to C is met, so the final grade remains "B". Choice C would be incorrect because it assumes the grade gets downgraded, but both participation is true (not false) and attendance exceeds 90%. To help students: Trace variable values at each step, understand that conditions can modify previously assigned values, and practice with different input combinations. Emphasize the importance of understanding when conditions are NOT met, as this determines which code blocks are skipped.
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 false and total >= 150
isMember true and total < 50
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.
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 outer IF should check wind, not isThunderstorm
The ELSE should print "Heat Advisory"
The wind check should use wind >= 30
The code needs a loop to repeat checks
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.
If score=79 and attendance=95 are true, what is the result?
// Inputs: score, attendance
IF score >= 80
grade = "B"
IF attendance < 90
grade = "C"
ELSE
grade = "C"
PRINT grade
C
A
No output
B
Explanation
This question tests understanding of nested conditionals in AP Computer Science Principles, focusing on how conditions control code execution paths. Nested conditionals can have inner conditions that are never evaluated if the outer condition fails, which is a key concept for understanding program flow. In this grading scenario, the code first checks if score >= 80, and since score=79, this condition is false, causing the program to skip the entire nested block and jump to the ELSE clause. Choice B is correct because with score=79 (less than 80), the code immediately goes to the ELSE block and assigns grade = "C", never checking the attendance condition. Choice A is incorrect because grade "B" is only assigned when score >= 80, which is not satisfied here. To help students: Use step-by-step execution traces, emphasize that failing an outer condition means all nested conditions are ignored, and practice with values just above and below boundary conditions. Teach students to identify "unreachable code" - conditions that won't be evaluated based on the input values.
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"
WALK
FLASHING
GREEN
RED
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.
Which sequence of conditions must be met for action="SPECIAL" to occur?
// Inputs: enemyNear, health, hasSpecial
IF enemyNear
IF health < 30
action = "RUN"
ELSE
IF hasSpecial
action = "SPECIAL"
ELSE
action = "ATTACK"
ELSE
action = "EXPLORE"
enemyNear true, health >= 30, hasSpecial true
enemyNear false and hasSpecial true
enemyNear true and health < 30
enemyNear true, health >= 30, hasSpecial false
Explanation
This question tests understanding of nested conditionals in AP Computer Science Principles, focusing on tracing the exact conditions needed for a specific outcome. Nested conditionals create a hierarchy of decisions where inner conditions are only evaluated if outer conditions are satisfied first. The game logic checks enemy proximity first, then health levels, and finally special ability availability to determine the appropriate action. Choice C is correct because action="SPECIAL" requires three specific conditions in sequence: enemyNear must be true (to enter the first IF), health must be >= 30 (to reach the ELSE branch of the health check), and hasSpecial must be true (to select SPECIAL over ATTACK). Choice D is incorrect because if hasSpecial is false, the action would be "ATTACK", not "SPECIAL". To help students: Create a checklist of required conditions for each outcome, practice backward reasoning from output to input requirements, and use truth tables to verify all paths. Remind students that for deeply nested outcomes, ALL parent conditions must align correctly to reach the desired result.
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"
night and hasPedestrian true
not night, traffic high, hasPedestrian true
not night, traffic not high, hasPedestrian true
night and traffic not high
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.
Given this weather-alert pseudo-code, what will the output be if temp=95, humidity=70, wind=10, isThunderstorm=false?
// Inputs: temp, humidity, wind, isThunderstorm
IF temp >= 90
IF humidity >= 60
PRINT "Heat Advisory"
ELSE
IF wind >= 25
PRINT "Wind Alert"
ELSE
PRINT "No Alert"
ELSE
IF isThunderstorm
IF wind >= 30
PRINT "Severe Storm"
ELSE
PRINT "Storm"
ELSE
PRINT "No Alert"
No Alert
Storm
Wind Alert
Heat Advisory
Explanation
This question tests understanding of nested conditionals in AP Computer Science Principles, specifically evaluating multiple weather conditions to determine appropriate alerts. Nested conditionals allow programs to make complex decisions by checking conditions within conditions, creating sophisticated logic paths. The weather alert system first checks if temperature >= 90, and since temp=95, this condition is true, leading to the nested check for humidity >= 60. Choice B is correct because with temp=95 and humidity=70, both conditions are satisfied, resulting in "Heat Advisory" being printed - the code never reaches the wind or thunderstorm checks. Choice A is incorrect because "Wind Alert" would only print if temp >= 90, humidity < 60, AND wind >= 25, but here humidity is 70 (>= 60), so the code takes a different path. To help students: Draw flowcharts showing each decision point, use actual values to trace through the logic step-by-step, and highlight which branches are never reached. Remind students that once a condition is met and an action is taken, the remaining ELSE branches are skipped entirely.