Selection and Conditions

9 min✏️ Quiz at the end

Algorithms That Decide

A plain sequence does the same steps every time. But real life needs decisions:

  • IF it is raining → take an umbrella
  • IF the password is correct → unlock the phone
  • IF the player's health reaches 0 → game over

Selection is the building block that lets an algorithm choose between paths. It is the "IF" in computing — and you can master it without writing a single line of real code.

Conditions: True or False?

Every selection is controlled by a condition — a test whose answer is always exactly true or false. Never "maybe".

ConditionExample answer
Is it raining?true
score > 10false (if score is 7)
name = "Alex"true (if the name is Alex)

Conditions often compare values using: = equals, > greater than, < less than, >= at least, <= at most.

IF, THEN, ELSE

The full pattern of selection, in pseudocode:

IF it is raining THEN
    take an umbrella
ELSE
    wear sunglasses
  • Condition true → the THEN branch runs
  • Condition false → the ELSE branch runs
  • Exactly one branch runs — never both, never neither

The ELSE part is optional. Without it, a false condition simply means "skip and carry on". In a flowchart, selection appears as a diamond with two labelled exits:

             ┌──────────────┐
             │  Is it       │
        ┌────│  raining?    │────┐
        │YES └──────────────┘ NO │
        ▼                        ▼
 ┌──────────────┐        ┌──────────────┐
 │ Take         │        │ Wear         │
 │ umbrella     │        │ sunglasses   │
 └──────────────┘        └──────────────┘

Chaining Decisions — ELSE IF

Some decisions have more than two outcomes. Chain the tests:

IF mark >= 80 THEN
    award Gold
ELSE IF mark >= 50 THEN
    award Silver
ELSE
    award Bronze

Trace it with mark = 65: Is 65 ≥ 80? No. Is 65 ≥ 50? Yes → Silver, and everything after is skipped. Tests run top to bottom and stop at the first true one — so the order of the tests matters.

Combining Conditions — AND, OR, NOT

Conditions can be joined with three logic words:

OperatorThe result is true when…Example
ANDboth parts are trueheight > 120 AND age >= 8
ORat least one part is trueis a student OR is over 65
NOTthe condition is false (it flips it)NOT raining

Worked example: a ride allows you on IF height > 120 AND age >= 8. Sam is 130 cm and 7 years old → height passes, age fails → AND needs both → Sam cannot ride. Priya is 145 cm and 9 → both pass → Priya rides.

A common trap: AND is stricter than OR. Swapping them changes who gets through — a serious bug in a real system.

Worked Example — Nested Selection for a Login Screen

Real systems often need a decision inside another decision. Consider a login screen that must check both the username and the password, and give a helpful message either way:

IF username is correct THEN
    IF password is correct THEN
        OUTPUT "Welcome back!"
    ELSE
        OUTPUT "Wrong password — try again"
    ENDIF
ELSE
    OUTPUT "Username not recognised"
ENDIF

This is called nested selection: an IF inside another IF, explored further in nested structures. Trace it with a correct username but wrong password: the outer condition is true, so we enter the outer THEN branch; inside it, the inner condition is false, so we get "Wrong password — try again". Notice that nesting lets the program give a more specific message than a single flat condition could — IF username correct AND password correct could only ever say "login failed" without knowing which part was wrong, because AND collapses both tests into one true/false answer.

Why the Order of ELSE IF Tests Matters

Return to the grading example — IF mark >= 80 THEN Gold ELSE IF mark >= 50 THEN Silver ELSE Bronze — and watch what happens if the tests are written in the wrong order:

IF mark >= 50 THEN
    award Silver
ELSE IF mark >= 80 THEN
    award Gold
ELSE
    award Bronze

Trace this broken version with mark = 95: is 95 >= 50? Yes — award Silver, and stop. The mark >= 80 test is never even reached, even though 95 clearly deserves Gold. This is a genuinely common bug: chained ELSE IF tests run top to bottom and stop at the first true one, so broader conditions must come after narrower ones, not before. Whenever a chain of ELSE IFs gives a surprising result, checking the order of the tests — not just their content — is one of the first things to try while debugging.

Key Words

  • Selection — choosing between paths in an algorithm based on a condition
  • Condition — a test that is either true or false
  • IF–THEN–ELSE — run one branch when true, the other when false
  • AND / OR / NOT — logic operators for combining or flipping conditions