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 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
2
Step 2: Check condition: is 15 >= 18? No.
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=7; if x>5: print 'big' else: print 'small'. What prints?
Example 4
easy
Trace: x=3; if x>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=85; if g≥90: 'A' elif g≥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=4; if n%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=20; if x>5 and y>25: 'A' else 'B'. What prints?
Example 12
medium
Trace: score=72; if score≥60: pass=true; if score≥90: honors=true. What are pass and honors?
Example 13
medium
A chain uses 'if x>0: A elif x>10: B'. For x=15, which prints, and what design flaw exists?
Example 14
medium
Trace: a=5; if a<0: s='neg' elif a==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=8; if x>5: if x>10: 'high' else 'mid' else 'low'. What prints?
Example 18
medium
Trace: x=0; if x>0: 'pos' elif x<0: 'neg' else: 'zero'. What prints?
Example 19
medium
Trace: temp=30; if temp>25: ac='on'; else: ac='off'. What is ac?
Example 20
challenge
A function: if x>0 return 'A'; if x>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 then ≥80 then ≥70. A bug orders them ≥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?