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. 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.

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?

Sense of Study hint: When writing a conditional, first identify the exact condition to test and make sure it evaluates to true or false. Then write the code for the 'true' branch, and consider whether you need an 'else' branch for the false case. Always check boundary values where the condition flips.

Worked Examples

Example 1

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

Answer

'Minor'

First step

1
Step 1: age = 15.

Full solution

  1. 2
    Step 2: Check condition: is 15 >= 18? No.
  2. 3
    Step 3: Execute the ELSE branch: output '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.

Example 3

medium
Write a one-line conditional expression in Python that assigns 'pass' if score >= 60 else 'fail'.

Example 4

hard
A function returns 'tween' if 10 <= age <= 12, 'teen' if 13 <= age <= 19, else 'other'. What does it return for age = 13?

Example 5

hard
Rewrite `if x > 0: return True; return False` as a single expression with no `if`.

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?

Example 3

easy
Trace: x=7x=7; if x>5x > 5: print 'big' else: print 'small'. What prints?

Example 4

easy
Trace: x=3x=3; if x>5x > 5: print 'big' else: print 'small'. What prints?

Example 5

easy
How many of the two branches in a single if/else execute for one run?

Example 6

easy
Trace: g=85g=85; if g90g \ge 90: 'A' elif g80g \ge 80: 'B' else 'C'. What prints?

Example 7

easy
In code, what symbol compares two values for equality (not assignment)?

Example 8

easy
Trace: n=4n=4; if n%2==0n \% 2 == 0: print 'even' else 'odd'. What prints?

Example 9

easy
Is 'if temp = 100' a correct condition in a language that distinguishes = and ==? Yes or no.

Example 10

easy
Which control structure lets a program take a different path when a condition is false?

Example 11

medium
Trace: x=10,y=20x=10,y=20; if x>5x > 5 and y>25y > 25: 'A' else 'B'. What prints?

Example 12

medium
Trace: score=72score=72; if score60score \ge 60: pass=true; if score90score \ge 90: honors=true. What are pass and honors?

Example 13

medium
A chain uses 'if x>0: A elif x>10: B'. For x=15x=15, which prints, and what design flaw exists?

Example 14

medium
Trace: a=5a=5; if a<0a < 0: s='neg' elif a==0a == 0: s='zero' else: s='pos'. What is s?

Example 15

medium
A function returns 'child' if age<13, 'teen' if age<20, else 'adult'. What does age=13 return?

Example 16

medium
Identify whether this is selection or iteration: 'while x < 10: x = x + 1'. Answer with one word.

Example 17

medium
Trace nested selection: x=8x=8; if x>5x>5: if x>10x>10: 'high' else 'mid' else 'low'. What prints?

Example 18

medium
Trace: x=0x=0; if x>0x > 0: 'pos' elif x<0x < 0: 'neg' else: 'zero'. What prints?

Example 19

medium
Trace: temp=30temp=30; if temp>25temp > 25: ac='on'; else: ac='off'. What is ac?

Example 20

challenge
A function: if x>0x>0 return 'A'; if x>0x>0 return 'B'. Which can ever be returned, and why is the second line dead code?

Example 21

challenge
Convert this to a single condition: 'if x>0: if y>0: print P'. Write the equivalent one-line condition.

Example 22

challenge
A grading chain checks 90\ge 90 then 80\ge 80 then 70\ge 70. A bug orders them 70\ge 70 first. For score 95, what grade results and why is it wrong?

Example 23

easy
`x = 12`; `if x > 10: print('big') else: print('small')`. What prints?

Example 24

easy
`n = 0`; `if n: print('truthy') else: print('falsy')`. What prints in Python?

Example 25

easy
How many branches of a single `if`/`elif`/`else` chain run for one execution?

Example 26

easy
`age = 18`; `if age >= 18: print('adult') else: print('minor')`. What prints?

Example 27

easy
`x = 7`; `if x > 0: y = 1`. What is `y` after the code runs?

Example 28

easy
`n = 5`; `if n % 2 == 0: print('even') else: print('odd')`. What prints?

Example 29

medium
`x = 10, y = 5`; `if x > 0 and y > 0: print('Q1')`. Does Q1 print?

Example 30

medium
`score = 75`; `if score >= 90: g='A' elif score >= 80: g='B' elif score >= 70: g='C' else: g='F'`. What is g?

Example 31

medium
`x = 5`; `if x > 0 or 1/0 > 0: print('ok')`. Does the program crash?

Example 32

medium
`x = 0`; `if x: print('A') elif x == 0: print('B') else: print('C')`. What prints?

Example 33

medium
`temp = 30`; `if temp > 25: fan='high'; if temp > 35: fan='max' else: fan='medium'`. What is `fan` at the end?

Example 34

medium
A buggy line: `if x = 5: print('hit')` in a language that distinguishes `=` and `==`. What happens?

Example 35

medium
`x = 100`; `if x > 10: print('A') elif x > 50: print('B') elif x > 90: print('C')`. What prints, and why is B/C dead?

Example 36

medium
`a = True, b = False`; `if a and not b: print('X')`. Does X print?

Example 37

medium
`x = 7`; `if x > 5: pass; print('done')`. What prints?

Example 38

medium
`x = 10`; `if x > 5: print('A'); if x > 8: print('B')`. What prints?

Example 39

medium
`x = -2`; `if x > 0: s='pos' elif x == 0: s='zero' else: s='neg'`. What is s?

Example 40

hard
`x = 5, y = 10, z = 7`; `m = x if x > y and x > z else (y if y > z else z)`. What is m?

Example 41

hard
Bug: a chain checks `if score >= 70 elif score >= 80 elif score >= 90`. For score=95 what grade results, and why is it wrong?

Example 42

hard
`x = 4`; `if x % 2 == 0 and x > 0: r='ep' elif x % 2 == 0: r='en' else: r='o'`. What is r?

Example 43

challenge
A program: `if a and b or c`. With a=False,b=True,c=Truea=False, b=True, c=True, what is the value? (Use standard precedence: `not` > `and` > `or`.)

Background Knowledge

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

sequence