Selection Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Selection.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

Choosing which block of code to execute based on whether a condition is true or false.

If this is true, do that path. If it is false, take a different path instead.

Read the full concept explanation β†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Programs can choose between different execution paths depending on the current data values.

Common stuck point: Conditions must be unambiguousβ€”what happens when temperature = 30?

Worked Examples

Example 1

easy
Trace this code: SET age = 15. IF age >= 18 THEN OUTPUT 'Adult' ELSE OUTPUT 'Minor'.

Solution

  1. 1
    Step 1: age = 15.
  2. 2
    Step 2: Check condition: is 15 >= 18? No.
  3. 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.

Example 2

medium
Write pseudocode using IF-ELIF-ELSE to assign a grade: 90+ = A, 80–89 = B, 70–79 = C, below 70 = F.

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

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

Example 2

medium
Trace this code: SET temp = 25. IF temp > 30 THEN fan = 'high' ELSE IF temp > 20 THEN fan = 'low' ELSE fan = 'off'. What is the value of fan?

Background Knowledge

These ideas may be useful before you work through the harder examples.

sequence