Selection CS Thinking Example 3
Follow the full solution, then compare it with the other examples linked below.
Example 3
easyWhat is the output? SET temp = 25. IF temp > 30 THEN OUTPUT 'Hot' ELIF temp > 20 THEN OUTPUT 'Warm' ELSE OUTPUT 'Cold'.
Solution
- 1 Step 1: temp = 25. Is 25 > 30? No. Is 25 > 20? Yes.
- 2 Step 2: Output: 'Warm'.
Answer
'Warm'
The first true condition determines the output. Since 25 > 20 is true, 'Warm' is printed without checking further conditions.
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 2 mediumWrite pseudocode using IF-ELIF-ELSE to assign a grade: 90+ = A, 80โ89 = B, 70โ79 = C, below 70 = F.
Example 4 mediumTrace this code: SET temp = 25. IF temp > 30 THEN fan = 'high' ELSE IF temp > 20 THEN fan = 'low' EL