Study CommonsRead · reason · practise

Home / Reasoning / Else If Chains: Many Choices, First Match Wins

Reasoning & Coding · Coding

Else If Chains: Many Choices, First Match Wins

Write and trace If, Else If and Else chains: check conditions from the top, stop at the first match, and put the conditions in the right order.

Learning objectiveTrace an If … Else If … Else chain by finding the first true condition, explain why the order of conditions changes the result, and test boundary values.

The idea

An If–Else makes a choice between two paths. But many real rules have more than two outcomes. A swimming test might give Level 1, 2, 3 or 4. A ticket booth charges one price for babies, another for children and another for adults. A program handles this with an else-if chain.

In this lesson a chain is written like this: If score >= 80 [say "Level 4"] Else If score >= 70 [say "Level 3"] Else [say "Level 1"]. The computer checks the conditions from the top, one at a time. The first condition that is true wins: its brackets run, and every remaining Else If and Else is skipped. If no condition is true, the final Else runs. If there is no Else, nothing runs.

Because the first match wins, the order of the conditions matters. A condition that catches too much, written too early, steals the cases that belong to later branches. For a rule such as "80 or more is Level 4, 70 or more is Level 3", check the highest cut-off first. Each later Else If then only sees values that failed the earlier checks, so score >= 70 already means "70 to 79".

A chain with a separate If on every line is different. Separate If blocks are all checked, and several can run. An else-if chain runs at most one branch. When you trace a chain, cross off the branches after the winner so you do not count them by mistake. Remember the boundary tools from the earlier lessons: >= includes the number itself, and 10 < 10 is false.

Coders test a chain with the value just below each boundary, the boundary itself and the value just above it. All programs and prices on this page are invented examples, and every answer was checked by running the same rules on a computer.

Worked examples

Example 1 · Find the first true condition

Program: If score >= 80 [say "Level 4"] Else If score >= 70 [say "Level 3"] Else If score >= 60 [say "Level 2"] Else [say "Level 1"]. The score is 82. What does it say?

  1. Check the first condition: 82 >= 80 is true.
  2. The first match wins, so the program says "Level 4".
  3. The checks for 70 and 60 are skipped. They would also be true for 82, but the chain never reaches them.

It says "Level 4".

Example 2 · The order changes the answer

Program: If n > 10 [say "big"] Else If n > 20 [say "huge"] Else [say "small"]. What does it say when n is 25, and what is the bug?

  1. Check the first condition: 25 > 10 is true, so it says "big" and stops.
  2. The check n > 20 is never reached. Any number above 20 is also above 10, so "huge" can never be said.
  3. Fix: put the stricter condition first. If n > 20 ["huge"] Else If n > 10 ["big"] Else ["small"]. Now 25 says "huge".

It says "big". The bug is the order: check n > 20 before n > 10.

Example 3 · Test the boundaries

Use the level program: If score >= 80 [say "Level 4"] Else If score >= 70 [say "Level 3"] Else If score >= 60 [say "Level 2"] Else [say "Level 1"]. What does it say for the scores 59, 60, 69 and 70?

  1. 59: not >= 80, not >= 70, not >= 60, so the Else runs: "Level 1".
  2. 60: 60 >= 60 is true, so "Level 2".
  3. 69: not >= 70 but >= 60, so "Level 2".
  4. 70: 70 >= 70 is true, so "Level 3". The answers change at 60 and at 70, exactly where the rules say they should.

59 is Level 1, 60 is Level 2, 69 is Level 2 and 70 is Level 3.

Common mistakes

Running every branch whose condition is true.

Why this fails: Only the first true condition runs. A chain stops at its first match, so the later branches are skipped even when they would also be true.

Putting the biggest cut-off last.

Why this fails: A loose condition such as n > 10 written first catches every larger number too. Check the strictest condition first so that it gets its turn.

Using > when the rule says "at least".

Why this fails: 80 > 80 is false. When a score of exactly 80 should count, use >= so the boundary belongs to the group.

Forgetting the Else.

Why this fails: When no condition is true and there is no Else, the program does nothing. An Else catches the leftovers so every input gets an answer.

Repeating checks that earlier branches already settled.

Why this fails: By the time an Else If is reached, all earlier conditions were false. Writing score >= 70 is enough; adding score < 80 repeats what the first branch already ruled out.

Foundation

Foundation practice

Trace a chain from the top and stop at the first true condition.

Concept

1. In an If … Else If … Else chain, how many of the branches run?

  1. Every branch whose condition is true
  2. Exactly one: the last branch whose condition is true
  3. Exactly one: the first branch whose condition is true, or the Else if none are
  4. Every branch, one after another, from top to bottom
Trace

2. Program: If score >= 80 [say "Level 4"] Else If score >= 70 [say "Level 3"] Else If score >= 60 [say "Level 2"] Else [say "Level 1"]. Score is 75. What does it say?

  1. Level 3
  2. Level 4
  3. Level 2
  4. Level 1
Boundary

3. Same program: If score >= 80 [say "Level 4"] Else If score >= 70 [say "Level 3"] Else If score >= 60 [say "Level 2"] Else [say "Level 1"]. Score is exactly 70. What does it say?

  1. Level 2
  2. Level 4
  3. Level 1
  4. Level 3
Choose an input

4. Same program: If score >= 80 [say "Level 4"] Else If score >= 70 [say "Level 3"] Else If score >= 60 [say "Level 2"] Else [say "Level 1"]. Which score makes it say "Level 1"?

  1. 60
  2. 59
  3. 69
  4. 65
Trace

5. Program: If temp < 0 [say "Freezing"] Else If temp < 10 [say "Cold"] Else If temp < 20 [say "Cool"] Else [say "Warm"]. The temperature is 10. What does it say?

Practice

Practice practice

Use order, loops and counting with chains.

Order matters

6. Program: If n > 10 [say "big"] Else If n > 20 [say "huge"] Else [say "small"]. n is 25. What does it say?

  1. huge
  2. small
  3. big
  4. big and huge
Fix the order

7. The program in the last question never says "huge". Which change makes it say "huge" when n is 25 and "big" when n is 15?

  1. Check n > 20 first, then n > 10
  2. Change n > 10 to n > 5
  3. Replace Else If with a second Else
  4. Check n > 10 twice
Chain in a loop

8. Program: total = 0; for each age in [3, 5, 12, 64, 65] [If age < 5 [price = 0] Else If age <= 12 [price = 6] Else If age <= 64 [price = 12] Else [price = 8]; total = total + price]; say total. (Invented ticket prices.) What does it say?

  1. 38
  2. 44
  3. 26
  4. 32
Chain in a loop

9. Program: a = 0; b = 0; c = 0; n = 0; Repeat 12 [n = n + 1; If n is a multiple of 6 [a = a + 1] Else If n is a multiple of 3 [b = b + 1] Else If n is even [c = c + 1]]. What are a, b and c at the end?

  1. a = 2, b = 4, c = 6
  2. a = 2, b = 2, c = 4
  3. a = 2, b = 2, c = 6
  4. a = 4, b = 2, c = 4
Order matters

10. A programmer writes the ticket rules in the wrong order: If age <= 64 [say 12] Else If age <= 12 [say 6] Else If age < 5 [say 0] Else [say 8]. What does it say for age 8?

  1. 6
  2. 0
  3. 12
  4. 8
Simplify

11. A programmer writes: If score >= 80 [say "Level 4"] Else If score >= 70 and score < 80 [say "Level 3"] … Why is "and score < 80" not needed?

  1. Every score of 80 or more was already caught by the first branch, so the second condition only sees scores below 80
  2. It is needed, because without it the program says "Level 3" for every score
  3. It is needed, because >= cannot be used with and
  4. It is not needed because 80 and 70 are the same number

Challenge

Challenge practice

Find bugs, count outcomes and rewrite chains.

Missing Else

12. Program: If n < 5 [say "small"] Else If n < 10 [say "medium"]. There is no final Else. What does it say when n is 15?

  1. "medium"
  2. Nothing: no condition is true, so no branch runs
  3. "small"
  4. An error message
Find the bug

13. A teacher wants a score of exactly 80 to get "Level 4", but the program says "Level 3". The first line is: If score > 80 [say "Level 4"]. What is the bug?

  1. The 80 should be 79
  2. The Else If lines should be removed
  3. The > should be >=, because 80 is not greater than 80
  4. The > should be <
Count with a chain

14. Use the level program: If score >= 80 [say "Level 4"] Else If score >= 70 [say "Level 3"] Else If score >= 60 [say "Level 2"] Else [say "Level 1"]. Run it on every score in [95, 82, 80, 79, 60, 59, 70]. How many scores get "Level 3"?

  1. 3
  2. 1
  3. 4
  4. 2
Reason

15. An else-if chain has 5 possible outcomes and ends with a single Else. How many conditions does it need to test?

  1. 4
  2. 5
  3. 3
  4. 6
Reason

16. The level rules are written from the lowest level up. Which chain gives the same answer as the level program for every score from 0 to 100?

  1. If score <= 60 [Level 1] Else If score <= 70 [Level 2] Else If score <= 80 [Level 3] Else [Level 4]
  2. If score < 60 [Level 1] Else If score < 70 [Level 2] Else If score < 80 [Level 3] Else [Level 4]
  3. If score > 60 [Level 2] Else If score > 70 [Level 3] Else If score > 80 [Level 4] Else [Level 1]
  4. If score < 80 [Level 3] Else If score < 70 [Level 2] Else If score < 60 [Level 1] Else [Level 4]

Answer key

  1. 1. Exactly one: the first branch whose condition is true, or the Else if none are
    The computer checks from the top and stops at the first true condition. Later branches are skipped even if their conditions would also be true. If every condition is false, the Else runs. Running every true branch describes separate If blocks, not a chain, and the last true condition never gets a turn.
  2. 2. Level 3
    75 >= 80 is false, so the program moves on. 75 >= 70 is true, so it says "Level 3" and skips the rest. 75 is also at least 60, but the chain never gets that far, so "Level 2" is not said.
  3. 3. Level 3
    >= means greater than or equal to, so 70 >= 70 is true and the program says "Level 3". Choosing "Level 2" treats 70 as if it were below the line. The score is not 80 or more, so it cannot be "Level 4".
  4. 4. 59
    Only a score below 60 reaches the Else. 60 is the tempting boundary choice, but 60 >= 60 is true, so it says "Level 2". 65 and 69 are also at least 60.
  5. 5. Cool
    10 < 0 is false. 10 < 10 is false, because 10 is not less than itself. 10 < 20 is true, so it says "Cool". Saying "Cold" is the common boundary slip.
  6. 6. big
    25 > 10 is true, so it says "big" and stops. The check for "huge" is never reached, so the program can never say "huge" for any n. "big and huge" would need two separate If blocks.
  7. 7. Check n > 20 first, then n > 10
    Put the strictest condition first: If n > 20 ["huge"] Else If n > 10 ["big"] Else ["small"]. Then 25 says "huge", 15 says "big" and 5 says "small". The other changes leave n > 10 in front, so every n above 10, including 25, still says "big".
  8. 8. 32
    The prices are 0 (age 3), 6 (age 5), 6 (age 12), 12 (age 64) and 8 (age 65), and 0 + 6 + 6 + 12 + 8 = 32. 38 charges age 3 the child price, 44 charges ages 5 and 12 the adult price, and 26 forgets that age 65 pays 8.
  9. 9. a = 2, b = 2, c = 4
    Multiples of 6 (6, 12) go to a, so a = 2. The multiples of 3 left over are 3 and 9, so b = 2. The even numbers left over are 2, 4, 8 and 10, so c = 4. The choice a = 2, b = 4, c = 6 is what separate If blocks would give, because 6 and 12 would be counted again. In a = 2, b = 2, c = 6 the 6 and 12 are counted as even again, and a = 4 counts the multiples of 3 as multiples of 6.
  10. 10. 12
    8 <= 64 is true, so it says 12 and stops. Every age up to 64 is caught by the first condition, so the child and free prices are never reached. The right answer for age 8 is 6, which needs the youngest ages checked first.
  11. 11. Every score of 80 or more was already caught by the first branch, so the second condition only sees scores below 80
    Each Else If only runs when all earlier conditions were false, so score >= 70 already means "70 or more, but below 80". Adding score < 80 is harmless but repeats work. Without it the program still gives the same answer for every score from 0 to 100.
  12. 12. Nothing: no condition is true, so no branch runs
    15 < 5 and 15 < 10 are both false, and there is no Else to catch the leftovers, so the program says nothing at all. It does not use the last branch by default, and it does not break; it simply does nothing for this n.
  13. 13. The > should be >=, because 80 is not greater than 80
    80 > 80 is false, so the score of 80 falls through to the next branch and gets "Level 3". Changing > to >= makes 80 count. Using 79 would mean 80 is not the boundary written in the rules, and < would send low scores to Level 4.
  14. 14. 2
    Level 3 needs a score from 70 to 79. Only 79 and 70 qualify, so 2 scores get "Level 3". 95, 82 and 80 get "Level 4" (3 scores), 60 gets "Level 2" and 59 gets "Level 1". Answering 3 counts the Level 4 group, and 4 adds 60 and 80 by mistake.
  15. 15. 4
    The Else covers one outcome without a condition, so the other 4 outcomes each need a condition, which makes 4 conditions in total. Five conditions would leave the Else with nothing to catch, and three conditions would only separate four outcomes.
  16. 16. If score < 60 [Level 1] Else If score < 70 [Level 2] Else If score < 80 [Level 3] Else [Level 4]
    Starting from the bottom, a score below 60 is Level 1, a score below 70 (but not below 60) is Level 2, and so on, so the first chain matches the original on every score. Using <= moves the boundaries, so 60 would become Level 1 instead of Level 2. The third chain checks > 60 first, so almost every score becomes Level 2. The last chain checks < 80 first, so everything below 80 becomes Level 3.

Related practice