Study CommonsRead · reason · practise

Home / Reasoning / If–Else: Programs That Make Decisions

Reasoning & Coding · Coding

If–Else: Programs That Make Decisions

Check comparisons such as >, < and ==, trace which branch of an if–else runs for each input, use an if inside a loop and fix a > versus >= boundary bug.

Learning objectiveDecide whether a comparison such as score > 5 or n == 10 is true or false, trace which branch of an if–else runs for a given input, follow an if inside a loop with a trace table, choose test values at the boundary and fix a program that uses > where it needs >=.

The idea

The school rule says: if it is raining, recess is inside; otherwise, recess is outside. A program can follow the same kind of rule. It checks something, then picks one of two paths. In coding this is called an if–else, and it is how a game knows whether you won or a robot knows when to turn.

In this lesson an if–else is written like this: If score > 5 [say "Win"] Else [say "Try again"]. The part after If is the condition. It is a question with only two possible answers: true or false. If it is true, the commands in the first brackets run. If it is false, the commands after Else run. Exactly one branch runs, never both. An If with no Else does nothing at all when its condition is false.

Conditions often compare two numbers. > means greater than, < means less than, and >= means greater than or equal to. == means is equal to. It has two equal signs because one = already means store, as in the variables lesson. Watch the boundary: 10 > 10 is false, because 10 is not greater than itself, but 10 >= 10 is true. A condition can also ask whether a number is even (2, 4, 6 …) or odd (1, 3, 5 …).

An If can sit inside a loop. Then the condition is checked again on every pass, using the newest values. That is how a program counts only the even numbers, or how a robot turns only when it meets a wall. In the robot questions, wall ahead is true when the square in front of the robot is a wall or the edge of the grid. F, L and R work as in the robot lesson, and the robot starts on S facing right (east).

Coders test an if–else with a value just below the boundary, the boundary itself and a value just above it, because most mistakes happen right at the edge. Programs are written on one line with semicolons between commands, as in the variables lesson. All programs on this page are invented examples, and every answer was checked by running the same program on a computer.

Worked examples

Example 1 · Pick the branch

Program: temp = -3; If temp < 0 [say "Wear mittens"] Else [say "No mittens needed"]. What does it say?

  1. temp = -3 stores −3 in the box called temp.
  2. The condition asks: is temp < 0? Is −3 less than 0? Yes, so the condition is true.
  3. A true condition runs the first brackets. The Else part is skipped completely.

It says "Wear mittens".

Example 2 · An If inside a loop

Program: n = 0; evens = 0; Repeat 6 [n = n + 1; If n is even [evens = evens + 1]]; say evens. What does it say?

  1. Make a trace table with three columns: pass, n and evens. Before the loop both are 0.
  2. Each pass adds 1 to n first, then checks the condition with the new n: n is 1, 2, 3, 4, 5, 6 on passes 1 to 6.
  3. The condition is true only when n is 2, 4 or 6. On those passes evens goes up by 1. On the other passes nothing happens, because there is no Else.
  4. Trace: n 1 → evens 0 · n 2 → 1 · n 3 → 1 · n 4 → 2 · n 5 → 2 · n 6 → 3.

It says 3.

Example 3 · Fix a boundary bug

Goal: anyone 120 cm or taller may go on the ride. Program: If height > 120 [say "Ride"] Else [say "Too short"]. Test it with 119, 120 and 121.

  1. height 119: 119 > 120 is false, so it says "Too short". Correct.
  2. height 120: 120 > 120 is false, so it says "Too short". Wrong: the goal says 120 cm is tall enough.
  3. height 121: 121 > 120 is true, so it says "Ride". Correct.
  4. Only the boundary value fails. "120 or taller" means greater than or equal to, so the sign must be >=.

Correct program: If height >= 120 [say "Ride"] Else [say "Too short"]. Now 120 says "Ride".

Common mistakes

Running both branches.

Why this fails: An if–else runs exactly one branch. When the condition is true, everything after Else is skipped. When it is false, the first brackets are skipped.

Treating > as if it were >=.

Why this fails: 8 > 8 is false. If the rule says "8 or more", the program needs >=. Always test the boundary number itself.

Writing = when you mean ==.

Why this fails: One = stores a value. Two == asks a question: are these equal? A condition must be a question with a true or false answer.

Checking the condition only once in a loop.

Why this fails: An If inside a loop is checked again on every pass with the newest values. Write one row per pass in your trace table and check the condition in each row.

Expecting something to happen when an If has no Else.

Why this fails: If the condition is false and there is no Else, the program does nothing for that If and moves on to the next command.

Foundation

Foundation practice

True or false? Decide, then run only one branch.

Condition

1. x = 5. Which condition is true?

  1. x > 5
  2. x < 5
  3. x == 5
  4. x > 8
Pick the branch

2. Program: score = 8; If score > 5 [say "Win"] Else [say "Try again"]. What does it say?

  1. Win
  2. Try again
  3. Win, then Try again
  4. Nothing
Pick the branch

3. Program: age = 10; If age == 12 [say "Free"] Else [say "Pay $5"]. What does it say?

  1. Free
  2. Pay $5
  3. Nothing
  4. Free, then Pay $5
Boundary

4. Program: tickets = 10; If tickets > 10 [say "Sold out"] Else [say "Seats left"]. What does it say?

  1. Sold out
  2. Sold out, then Seats left
  3. Nothing
  4. Seats left
If without Else

5. Program: coins = 3; If coins > 5 [coins = coins + 10]; coins = coins + 1; say coins. What does it say?

Practice

Practice practice

Try different inputs. Check the condition again on every pass of a loop.

Choose an input

6. Program: If temp < 5 [say "Cold"] Else [say "Mild"]. Which value of temp makes it say "Cold"?

  1. 5
  2. 8
  3. 4
  4. 12
If in a loop

7. Program: n = 0; evens = 0; Repeat 7 [n = n + 1; If n is even [evens = evens + 1]]; say evens. What does it say?

  1. 3
  2. 4
  3. 7
  4. 6
If–else in a loop

8. Program: x = 0; Repeat 5 [x = x + 1; If x > 3 [say "big"] Else [say "small"]]. What does it say, in order?

  1. small, small, big, big, big
  2. small, small, small, big, big
  3. small, small, small, small, big
  4. big, big, big, small, small
Trace table

9. Program: n = 0; total = 0; Repeat 6 [n = n + 1; If n > 2 [total = total + n]]; say total. Make a trace table for n and total. What does it say?

Robot decides

10. The robot starts on S facing right. Program: Repeat 6 [If wall ahead [R] Else [F]]. Which lettered square does it end on?

  1. A
  2. B
  3. C
  4. It hits a wall
A 3 by 4 grid. S top left. A wall at the right end of the top row. C in the middle row, third column. A bottom left and B bottom row, third column.SCAB
A 3 by 4 grid. S top left. A wall at the right end of the top row. C in the middle row, third column. A bottom left and B bottom row, third column.
Choose the comparison

11. Goal: say "Game over" when lives reaches 0 and "Keep playing" otherwise. Lives are never below 0. Program: If lives ___ 0 [say "Game over"] Else [say "Keep playing"]. Which sign fills the blank?

  1. >
  2. >=
  3. <
  4. ==

Think

Think practice

Find the bug. Test below, at and above the boundary.

Debug

12. Goal: say "Pass" for marks of 50 or more. Program: If mark > 50 [say "Pass"] Else [say "Try again"]. A mark of 50 says "Try again". What is the bug?

  1. > should be >=
  2. > should be <
  3. Pass and Try again are swapped
  4. 50 should be 51
Debug

13. Goal: count how many of the numbers 1 to 10 are greater than 6, and say 4. Program: n = 0; count = 0; Repeat 10 [n = n + 1; If n < 6 [count = count + 1]]; say count. It says 5. Find the bug and fix it.

Debug

14. Goal: reach G. Program: Repeat 4 [If wall ahead [L] Else [F]]. The robot never leaves the top row. Find the bug and fix it.

A 2 by 4 grid. S top left. A wall at the right end of the top row. G in the bottom row, third column, just below the square in front of the wall.SG
A 2 by 4 grid. S top left. A wall at the right end of the top row. G in the bottom row, third column, just below the square in front of the wall.
Predict

15. Program: a = 10; Repeat 4 [If a > 5 [a = a - 3] Else [a = a + 1]]; say a. What does it say?

  1. 2
  2. -2
  3. 6
  4. 5

Challenge

Challenge practice

Write and test your own decisions.

Write a program

16. Write an if–else that says "Hot" when temp is 25 or more and "Not hot" otherwise. Then list three test values you would try.

Test values

17. Program: If speed > 40 [say "Slow down"] Else [say "OK"]. Which three test values best check that the boundary is right?

  1. 10, 20, 30
  2. 100, 200, 300
  3. 40, 40, 40
  4. 39, 40, 41
Two totals

18. Program: n = 0; odds = 0; evens = 0; Repeat 9 [n = n + 1; If n is even [evens = evens + n] Else [odds = odds + n]]; say evens; say odds. What does it say?

Answer key

  1. 1. x == 5
    5 is equal to 5, so x == 5 is true. x > 5 is false because 5 is not greater than itself; that is the boundary trap. 5 is not less than 5, and 5 is not greater than 8.
  2. 2. Win
    8 > 5 is true, so the first branch runs and the Else branch is skipped. An if–else never runs both branches, and it always runs one of them, so it cannot say nothing.
  3. 3. Pay $5
    == asks whether age is exactly 12. 10 is not 12, so the condition is false and the Else branch runs. Reading == as "store 12" would wrongly give Free.
  4. 4. Seats left
    10 > 10 is false, because 10 is not greater than itself, so the Else branch runs. Sold out is the answer you get if you read > as >=.
  5. 5. 4
    3 > 5 is false and there is no Else, so the + 10 is skipped. The next command still runs: 3 + 1 = 4. (14 would mean the If ran even though its condition was false.)
  6. 6. 4
    4 < 5 is true. 5 is the tempting boundary choice, but 5 < 5 is false, so 5 says "Mild". 8 and 12 are greater than 5.
  7. 7. 3
    n takes the values 1 to 7. Only 2, 4 and 6 are even, so evens goes up 3 times. 4 counts the odd numbers (1, 3, 5, 7), 7 counts every pass, and 6 is the biggest even number, not how many there are.
  8. 8. small, small, small, big, big
    x is 1, 2, 3, 4, 5. Only 4 and 5 are greater than 3. The first option treats 3 > 3 as true (that would be >=). The third stops saying big too late, and the last swaps the two branches.
  9. 9. 18
    n is 1, 2, 3, 4, 5, 6. The condition is false for 1 and 2, so total stays 0. Then total becomes 3, 7, 12, 18. Adding all six numbers (21) forgets the condition.
  10. 10. B
    Pass 1 and 2: F, F along the top row. Pass 3: wall ahead, so R (now facing down). Pass 4: F to C. Pass 5: F to B. Pass 6: the edge is ahead, so R; turning does not move it. C is one square short (where it stands after pass 4), A would need two more forward moves after the last turn, and the If stops it from ever walking into the wall.
  11. 11. ==
    lives == 0 is true only when lives is exactly 0. > 0 would end the game while you still have lives, >= 0 would always say Game over, and < 0 would never be true because lives never go below 0.
  12. 12. > should be >=
    50 > 50 is false, so the boundary mark goes to Else. "50 or more" needs mark >= 50. Using < would fail every high mark, swapping the branches would make 30 pass, and 51 makes the boundary problem worse.
  13. 13. The sign is backwards. Use If n > 6 [count = count + 1]; then it says 4.
    n < 6 counts 1, 2, 3, 4, 5, which is 5 numbers. n > 6 counts 7, 8, 9, 10, which is 4. Note that n >= 6 would also be wrong: it counts 6 as well and says 5.
  14. 14. The If branch should turn right: Repeat 4 [If wall ahead [R] Else [F]].
    With L: F, F, then the wall makes it turn left (facing up), and the edge makes it turn left again (facing left). With R: F, F, R to face down, then F onto G.
  15. 15. 6
    Pass 1: 10 > 5, so 7. Pass 2: 7 > 5, so 4. Pass 3: 4 > 5 is false, so 5. Pass 4: 5 > 5 is false, so 6. You get 2 if you treat 5 > 5 as true, −2 if you always subtract, and 5 if you stop after 3 passes.
  16. 16. If temp >= 25 [say "Hot"] Else [say "Not hot"]. Test with 24, 25 and 26.
    "25 or more" includes 25, so the sign is >=. Testing 24, 25 and 26 checks just below, at and just above the boundary. For whole numbers, temp > 24 also works.
  17. 17. 39, 40, 41
    Most if–else bugs happen at the boundary, so test just below, at and just above it. 10, 20, 30 and 100, 200, 300 each test only one branch, and 40, 40, 40 repeats the same test.
  18. 18. It says 20, then 25.
    Even values of n (2, 4, 6, 8) go into evens: 20. Odd values (1, 3, 5, 7, 9) take the Else branch into odds: 25. Every pass runs exactly one branch, so 20 + 25 = 45 = 1 + 2 + … + 9.

Related practice