Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Selection

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


Quick Answer

Choosing which block of code to execute based on whether a condition is true or false. Use it whenever a program must behave differently for different inputs: if the score is 90 or more, give an A; else if it is 80 or more, give a B; otherwise give a C. Each condition is checked using the values at that moment, exactly one branch of an if / else-if / else chain runs, and the other branches are skipped.

Why This Matters

Selection is how a program makes decisions: a game ends when health reaches 0, a login rejects a wrong password, a thermostat switches the heating on below 18 °C. Knowing that an else-if chain stops at the first true condition lets you predict which branch runs and spot conditions written in the wrong order, a common reason a program gives the wrong grade or the wrong price.

Intuitive Explanation

A condition is a question with a yes-or-no answer, such as temperature > 30. When the program reaches an if statement, it works out the answer from the current values and then takes one path. If temperature is 34, then 34 > 30 is true, so the 'turn on the fan' block runs. If temperature is 22, the condition is false, so the else block runs instead. For any one value, only one of the two blocks runs.

With more than two options you chain the tests: if score >= 90, grade A; else if score >= 80, grade B; else if score >= 70, grade C; else grade F. The computer checks from the top and stops at the first true test. For a score of 85, the first test (85 >= 90) is false and the second (85 >= 80) is true, so the grade is B, and the remaining tests are never checked. That is why the order of the tests matters.

Compare a loop. A while loop also tests a condition, but it goes back and tests again after every pass; an if tests once and moves on. Selection is also different from simply computing a true-or-false value: passed = score >= 50 stores true or false but chooses nothing. It becomes selection when that answer decides which lines run.

Think of it like this. Selection works like a fork in a hiking trail with a sign: 'More than 2 hours of daylight left? Take the left path to the peak. Otherwise take the right path back to the car.' You read the sign once, take exactly one path, and never walk both. An else-if chain is a series of forks where you turn off at the first sign that applies to you.

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


When to Use

Use selection when code contains if, else if, or else, or when a task says 'only when', 'otherwise', or 'depending on': grading, price tiers, game rules, checking input. Evaluate each condition with the current values and follow the first true branch. If the code repeats while a condition stays true, that is Iteration; if you are only combining conditions with AND, OR, or NOT into one true-or-false value, that is Boolean Logic.

Tip. Ask: Does a true-or-false test decide which one of several blocks runs, with each block running at most once?

How to Recognize It

Before calling code selection, find the condition and check what happens to the code after it has been tested.

  1. Is there a condition, such as score >= 90, that comes out true or false?

    Yes is the first sign of selection. If there is no condition at all and every line runs, it is plain Sequence.

  2. After the condition is checked once, does the program move on rather than coming back to test it again?

    Yes means selection. If the program keeps returning to the same test and repeats the block while it stays true, that is a While Loop.

  3. Is there one if statement inside the block of another?

    Yes points to Nested Conditionals, a layered form of selection where the inner test is checked only when the outer one sends the program there.

  4. Is the question only whether a combined condition, like (a > 2) AND (b < 5), is true?

    Yes points to Boolean Logic. Selection begins once that true-or-false answer is used to pick which code runs.

Selection vs Boolean Logic vs Nested Conditionals vs While Loop

All four involve a condition that is true or false, which is why they get mixed up. Ask what the condition does: picks a path once, is only worked out, picks inside another pick, or keeps a block repeating.

Selection

Meaning
Selection checks a condition once and runs the matching block, skipping the others.
Key test
One test, one path taken, then the program moves on.
Formula
IF B THEN S1 ELSE S2
Example
if age >= 13: show 'Welcome' else: show 'Ask a parent'.

Boolean Logic

Meaning
Boolean logic works out whether a combined condition is true or false using AND, OR and NOT.
Key test
Only the true-or-false value is asked for, not which code runs.
Formula
(a>2)∧(b<5)
Example
(age >= 13) AND (hasPermission) is false whenever hasPermission is false.

Nested Conditionals

Meaning
Nested conditionals put one if statement inside another, so the inner test is reached only on one branch of the outer test.
Key test
An if sits inside another if's block.
Example
If it is raining, then check: if it is windy, take a coat; otherwise take an umbrella.

While Loop

Meaning
A while loop tests its condition again after each pass and repeats the block for as long as it stays true.
Key test
The test is checked again and again, and the block can repeat.
Example
while the password is wrong: ask for it again.

Formula & Notation

Worked Examples

Example 1 — Trace a grading chain

Easy

What grade does this code give for score = 85, and for score = 62? if score >= 90: grade = 'A'; else if score >= 80: grade = 'B'; else if score >= 70: grade = 'C'; else: grade = 'F'.
Show the steps and the answer
  1. score = 85: 85 >= 90 is false; 85 >= 80 is true, so grade = 'B'.

    The chain runs the first branch whose condition is true.

  2. Skip the remaining tests for 85.

    Once a branch has run, the rest of an else-if chain is not checked, even though 85 >= 70 is also true.

  3. score = 62: 62 >= 90, 62 >= 80, and 62 >= 70 are all false, so the else branch runs: grade = 'F'.

    else catches every value that failed all the tests above it.

Answer: 85 gives B; 62 gives F.

Takeaway: In an else-if chain, check the tests from the top and stop at the first true one.

Example 2 — Separate ifs are not a chain

Standard

This version uses separate if statements instead of else-if: if score >= 90: grade = 'A'; if score >= 80: grade = 'B'; if score >= 70: grade = 'C'. What grade does score = 95 end up with?
Show the steps and the answer
  1. First if: 95 >= 90 is true, so grade = 'A'.

    So far this matches the else-if version.

  2. Second if: 95 >= 80 is true, so grade becomes 'B'.

    Separate if statements are each checked; nothing tells the computer to skip them after a match.

  3. Third if: 95 >= 70 is true, so grade becomes 'C'.

    Each assignment overwrites the last, so the final grade comes from the last true test.

  4. Fix: turn the second and third if into else if.

    Then the chain stops after the first match, and 95 keeps its 'A'.

Answer: It ends with grade 'C'; written as an else-if chain it would correctly give 'A'.

Takeaway: Several separate ifs can all run; use else if when exactly one option should apply.

Example 3 — Find the mistake: the broad test comes first

Application

A cinema charges $6 for children under 12, $8 for seniors aged 65 and over, and $10 for everyone else. Ava writes: if age < 65: price = 10; else if age < 12: price = 6; else: price = 8. She tests age 30 (price 10) and age 70 (price 8) and both are right. Find the mistake and fix it.
Show the steps and the answer
  1. Test a child, age = 7: the first condition, 7 < 65, is true, so price = 10.

    A 7-year-old should pay $6, so the code is wrong for every child.

  2. Find the cause: every child is also under 65, so the first branch catches them before age < 12 is ever checked.

    The conditions overlap, and the broader one comes first.

  3. Fix by testing the narrow cases first: if age < 12: price = 6; else if age >= 65: price = 8; else: price = 10.

    Children are caught by the first test, seniors by the second, and everyone else falls through to else.

  4. Retest one age from each group: 7 gives 6, 30 gives 10, 70 gives 8.

    All three groups now get the right price.

Answer: if age < 12, price 6; else if age >= 65, price 8; else price 10. A 7-year-old now pays $6.

Takeaway: When conditions overlap, put the narrower test first, and test one input from every group.

Where You'll See It

  • A vending machine checking your money

    If you've put in enough coins it gives you the snack, else it asks for more; the machine takes one of two paths based on a true/false condition.

  • A game checking if your health hit zero

    If health is 0 the game shows 'Game Over,' otherwise play continues, which is exactly an if/else decision based on the current value.

  • A theme-park ride height rule

    If you're tall enough you may ride, else you're turned away; the same condition picks one of two outcomes for each person.

Common Mistakes

  1. The slip: Writing if (x = 5) to check whether x equals 5.

    Why it's tempting: A single '=' looks like 'equals' the way it does in math.

    Instead: A single = assigns a value, while == compares; use == in a condition, because in some languages = silently succeeds and the branch behaves wrongly.

  2. The slip: If I cover the main case, I don't need an else.

    Why it's tempting: The expected input always takes the 'if' path in your head, so the other path feels unnecessary.

    Instead: Without an else (or a default), unexpected inputs have no defined behavior; always decide what happens when the condition is false.

  3. The slip: In an if / else-if chain, more than one branch can run if several conditions are true.

    Why it's tempting: Each condition is true on its own, so it feels like each matching branch should fire.

    Instead: An if / else-if chain runs only the first matching branch and skips the rest; overlapping conditions mean a later branch you intended may never execute.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. `score = 72`. The code is `if score >= 60 then print("Pass") else print("Try again")`. What is printed?

    Hint: Is 72 greater than or equal to 60?

  2. Code: `if t > 25 then print("hot") else if t > 10 then print("mild") else print("cold")`. What is printed when `t = 15`? When `t = 10`?

    Hint: Check the conditions top to bottom, and be careful with "greater than" at exactly 10.

  3. Shipping costs $5 but is free on orders of $50 or more. Write this as an if/else and give the shipping cost for orders of $49 and $50.

    Hint: Decide whether the condition should use `>` or `>=`.

  4. Code: `if n % 2 == 0 then print("even") else print("odd")`. Trace it for `n = 7`, then for `n = 12`.

    Hint: `%` gives the remainder after dividing.

  5. A game rule says: "If the player has a key AND the door is unlocked, open the door." Which part is selection, and which part is Boolean logic?

    Hint: One part produces true or false; the other part uses that answer to choose.

  6. Grading code: `if s >= 70 then grade = "C" else if s >= 80 then grade = "B" else if s >= 90 then grade = "A"`. A student with `s = 95` gets "C". Fix it.

    Hint: Only the first true branch runs. Which condition is true first for 95?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

What is the difference between an else-if chain and several separate if statements?

In an if / else-if chain, the program stops at the first true condition. With separate if statements, every condition is checked and several blocks can run. With score = 95, the separate tests 'if score >= 90: grade = A' and 'if score >= 80: grade = B' set the grade to A and then overwrite it with B. The chain gives A and stops.

Why do so many bugs happen at values like exactly 90?

Because that is where a condition flips. If the rule is '90 or more gets an A' but the code says score > 90, a score of exactly 90 slips down to a B. Whenever a condition uses >, <, >= or <=, test the value right on the line, one just below it and one just above it, such as 89, 90 and 91.

Can a program make a choice without writing a full if statement?

Sometimes. Many languages have a shorter form for choosing between two values, such as Python's result = 'pass' if score >= 50 else 'fail', and some have a switch or match statement for choosing among many fixed cases, such as the days of the week. These are still selection: a condition or a value decides which path the program takes.

Learning Path

← Before

Sequence
Selection

You are here

You need Sequence: code runs line by line, using the current values. Selection adds branching, so a condition decides which block of lines runs and which is skipped. Next, Boolean Logic shows how to build richer conditions with AND, OR, and NOT, and Nested Conditionals put one decision inside another, such as checking a rider's age only after checking that they bought a ticket.

See Also