Selection CS Thinking Example 1
Follow the full solution, then compare it with the other examples linked below.
Example 1
easyTrace this code: SET age = 15. IF age >= 18 THEN OUTPUT 'Adult' ELSE OUTPUT 'Minor'.
Solution
- 1 Step 1: age = 15.
- 2 Step 2: Check condition: is 15 >= 18? No.
- 3 Step 3: Execute the ELSE branch: output 'Minor'.
Answer
'Minor'
Selection (IF-ELSE) allows a program to choose between two paths based on a condition. Only one branch executes depending on whether the condition is true or false.
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 2 medium
Write pseudocode using IF-ELIF-ELSE to assign a grade: 90+ = A, 80โ89 = B, 70โ79 = C, below 70 = F.
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