Selection CS Thinking Example 3

Follow the full solution, then compare it with the other examples linked below.

Example 3

easy
What is the output? SET temp = 25. IF temp > 30 THEN OUTPUT 'Hot' ELIF temp > 20 THEN OUTPUT 'Warm' ELSE OUTPUT 'Cold'.

Solution

  1. 1
    Step 1: temp = 25. Is 25 > 30? No. Is 25 > 20? Yes.
  2. 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