Selection CS Thinking Example 2
Follow the full solution, then compare it with the other examples linked below.
Example 2
mediumWrite pseudocode using IF-ELIF-ELSE to assign a grade: 90+ = A, 80–89 = B, 70–79 = C, below 70 = F.
Solution
- 1 Step 1: Start with the highest threshold: IF score >= 90 THEN grade = 'A'.
- 2 Step 2: ELIF score >= 80 THEN grade = 'B'. ELIF score >= 70 THEN grade = 'C'.
- 3 Step 3: ELSE grade = 'F'. The conditions are checked in order, so the first match is used.
Answer
IF score >= 90: A. ELIF score >= 80: B. ELIF score >= 70: C. ELSE: F.
Chained selection (IF-ELIF-ELSE) handles multiple conditions. Order matters — conditions should go from most to least restrictive to avoid incorrect matches.
About Selection
Choosing which block of code to execute based on whether a condition is true or false. Selection allows programs to make decisions, following one path when a condition holds and a different path otherwise, using constructs like if, else-if, and else.
Learn more about Selection →More Selection Examples
Example 1 easy
Trace this code: SET age = 15. IF age >= 18 THEN OUTPUT 'Adult' ELSE OUTPUT 'Minor'.
Example 3 easyWhat is the output? SET temp = 25. IF temp > 30 THEN OUTPUT 'Hot' ELIF temp > 20 THEN OUTPUT 'Warm'
Example 4 mediumTrace this code: SET temp = 25. IF temp > 30 THEN fan = 'high' ELSE IF temp > 20 THEN fan = 'low' EL