Study CommonsRead · reason · practise

Home / Reasoning / Counters and Totals: Put an If Inside a Loop

Reasoning & Coding · Coding

Counters and Totals: Put an If Inside a Loop

Loop through a list, test each item with an If, then count the matches or add them up. Trace counters and totals, and fix four common tally bugs.

Learning objectiveUse an If inside a For each loop to count the items that pass a test and to add up only those items, tell a counter from a total, choose the right comparison (such as < or ≤), and fix bugs where the counter is in the wrong place or changes by the wrong amount.

The idea

When a class votes with a show of hands, someone looks at each hand, checks one thing and makes a tally mark only when it matches. A program can do the same job with a list: look at each item, ask a question, and change a variable only when the answer is yes.

Two kinds of variables do most of this work. A counter answers how many: it changes by 1 each time an item passes the test. A total (programmers also call it an accumulator) answers how much: it changes by the item's own value. In [25, 5, 100], counting the coins of 25¢ or more gives 2, but adding up those coins gives 125.

The pattern has three places, and each step belongs in one of them. Before the loop, set the counter or total to 0, once. Inside the loop, put the If and, inside the If, the Change step. After the loop, say the answer, once.

One loop can do two jobs at the same time. Inside the same If you can change a counter by 1 and a total by the item. An If–Else can keep two counters: one for items that pass and one for items that do not.

Read the test words carefully. More than 5 is s > 5 and leaves out 5. At least 5 is s ≥ 5 and includes 5. Less than 10 is v < 10; at most 10 is v ≤ 10. Between 3 and 7, including both, needs two tests: n ≥ 3 and n ≤ 7. A trace table with one row per item, and columns for the item, yes or no, and each variable, keeps you honest.

Robot questions use the same rules as earlier lessons: F moves forward one square, L and R turn a quarter turn without moving, S is the start, G is the goal and dark squares are walls. The robot starts on S facing right (east). run m makes the robot do the move m.

Worked examples

Example 1 · Count the matches

Set temps to [-3, 2, 0, -1, 4]. Set cold to 0. For each t in temps [If t < 0 [Change cold by 1]]. say cold. How many days were below zero?

  1. cold starts at 0, before the loop.
  2. -3 < 0 yes (cold 1). 2 no. 0 no, because 0 is not below 0. -1 yes (cold 2). 4 no.
  3. The loop ends after the last item, then say cold runs once.

It says 2.

Example 2 · Add up only the matches

Set coins to [25, 5, 100, 10, 25]. Set total to 0. For each c in coins [If c ≥ 25 [Change total by c]]. say total. What is said?

  1. The test is c ≥ 25, so a 25 counts.
  2. 25 yes (total 25). 5 no. 100 yes (total 125). 10 no. 25 yes (total 150).
  3. This is a total, not a counter: it changes by the coin's value, not by 1.

It says 150 (cents), which is $1.50.

Example 3 · A counter and a total in the same loop

Set pages to [12, 30, 8, 25]. Set count to 0. Set sum to 0. For each p in pages [If p > 10 [Change count by 1; Change sum by p]]. say count, then sum.

  1. Trace table: p | p > 10? | count | sum.
  2. 12 | yes | 1 | 12. 30 | yes | 2 | 42. 8 | no | 2 | 42. 25 | yes | 3 | 67.
  3. Both Change steps sit inside the same If, so they change only for books longer than 10 pages.

It says 3, then 67: three books, 67 pages.

Example 4 · Two counters with If–Else

Set votes to [pizza, tacos, pizza, pizza, tacos]. Set p to 0. Set t to 0. For each v in votes [If v = pizza [Change p by 1] Else [Change t by 1]]. say p, then t.

  1. Every item goes into exactly one branch.
  2. pizza (p 1), tacos (t 1), pizza (p 2), pizza (p 3), tacos (t 2).
  3. Check: p + t = 3 + 2 = 5, which is the length of the list.

It says 3, then 2. Pizza wins.

Example 5 · Count the steps on a robot trip

Set moves to [F, F, R, F, F, L, F]. Set steps to 0. For each m in moves [run m; If m = F [Change steps by 1]]. Where does the robot finish, and what is steps?

A 4-by-3 coding grid. The top row is S, two open squares and a wall. The middle row has two walls, an open square and a wall. The bottom row has a wall, two open squares and G in the bottom-right corner.SG
A 4-by-3 coding grid. The top row is S, two open squares and a wall. The middle row has two walls, an open square and a wall. The bottom row has a wall, two open squares and G in the bottom-right corner.
  1. F, F: two squares right. R: turn to face down (no step).
  2. F, F: two squares down to the bottom row. L: turn to face right (no step). F: one square right onto G.
  3. Only the F items pass the test, so steps counts 5 of the 7 items. Turns are in the list but are not steps.

It finishes on G, facing right, and steps is 5.

Common mistakes

Putting Change count by 1 after the If instead of inside it.

Why this fails: Then the count changes on every pass, match or not, and the answer is just the length of the list.

Changing a counter by the item instead of by 1.

Why this fails: Change count by s adds up the matching values. That answers how much, not how many.

Using < when the problem says at most.

Why this fails: At most 10 includes 10, so the test is v ≤ 10. The test v < 10 leaves out every 10.

Setting the counter to 0 inside the loop.

Why this fails: The counter is wiped on every pass, so the answer can only be 0 or 1. Set it once, before the loop.

Putting say inside the loop.

Why this fails: You hear a running tally after every item instead of one final answer. Say the result after the loop ends.

Foundation

Foundation practice

Tell a counter from a total and trace one test.

Concept

1. What is the difference between a counter and a total?

  1. A counter is always bigger than a total
  2. A counter changes by 1 for each match; a total changes by the item's value
  3. A counter goes before the loop and a total goes after it
  4. They are the same thing with different names
Count

2. Set temps to [4, -2, -5, 0, 3]. Set cold to 0. For each t in temps [If t < 0 [Change cold by 1]]. say cold. What is said?

  1. 2
  2. 3
  3. -7
  4. 5
Count words

3. Set shirts to [red, blue, red, green, red]. Set n to 0. For each s in shirts [If s = red [Change n by 1]]. say n. What is said?

  1. 1
  2. 5
  3. 2
  4. 3
Total of matches

4. Set nums to [3, 8, 1, 6]. Set total to 0. For each n in nums [If n > 4 [Change total by n]]. say total. What is said?

  1. 18
  2. 2
  3. 14
  4. 8
Where does it go?

5. You want to count the scores above 5. Where should Change count by 1 go?

  1. Before the loop
  2. Inside the loop but after the If
  3. Inside the If, inside the loop
  4. After the loop

Practice

Practice practice

Choose the right test, keep two variables and use If–Else.

At least

6. Set ages to [7, 9, 8, 10, 9]. Count the children who are at least 9 using If a ≥ 9. What is the count?

  1. 1
  2. 5
  3. 2
  4. 3
Count and total

7. Set pages to [15, 40, 9, 22]. Set count to 0. Set sum to 0. For each p in pages [If p > 20 [Change count by 1; Change sum by p]]. say count, then sum.

  1. 4, then 86
  2. 2, then 2
  3. 2, then 62
  4. 3, then 77
If–Else

8. Set rolls to [6, 2, 5, 1, 4, 6]. Set high to 0. Set low to 0. For each r in rolls [If r ≥ 4 [Change high by 1] Else [Change low by 1]]. say high, then low.

  1. 3, then 3
  2. 4, then 2
  3. 4, then 6
  4. 2, then 4
Spend under a limit

9. Set prices to [3, 12, 7, 15, 2]. Set spend to 0. For each p in prices [If p < 10 [Change spend by p]]. say spend. What is said?

  1. 3
  2. 39
  3. 27
  4. 12
Robot steps

10. Set moves to [F, R, F, F, L, F]. Set steps to 0. For each m in moves [run m; If m = F [Change steps by 1]]. What is steps, and where does the robot finish?

  1. 6, on G
  2. 4, one square above G
  3. 3, on G
  4. 4, on G
A 4-by-3 coding grid. The top row is S, an open square and two walls. The middle row is a wall, an open square, a wall and an open square. The bottom row is a wall, an open square, G and an open square.SG
A 4-by-3 coding grid. The top row is S, an open square and two walls. The middle row is a wall, an open square, a wall and an open square. The bottom row is a wall, an open square, G and an open square.

Think

Think practice

Find the bug in tally programs and explain the pattern.

Find the bug

11. Set count to 0. For each s in [7, 2, 9, 4] [If s > 5 [say yes]; Change count by 1]. say count. It says 4, but only two scores are above 5. What is the bug?

  1. Change count by 1 is outside the If, so it runs on every pass; move it inside the If.
  2. The test should be s < 5.
  3. Set count to 0 should be Set count to 1.
  4. say count should be inside the loop.
Find the bug

12. Set count to 0. For each s in [7, 2, 9, 4] [If s > 5 [Change count by s]]. say count. It says 16, but the program should count how many scores are above 5. What is the fix?

  1. Change the test to s ≥ 5
  2. Change count by 1 instead of by s
  3. Move Set count to 0 inside the loop
  4. Change count by s × 2
Choose the test

13. Set waits to [10, 4, 12, 10]. You want to count the waits that are at most 10 minutes, but If v < 10 gives 1. Which test gives the right answer, 3?

  1. v > 10
  2. v = 10
  3. v ≤ 10
  4. v ≥ 4
Find the bug

14. Set votes to [yes, no, yes, yes]. For each v in votes [Set count to 0; If v = yes [Change count by 1]]. say count. It says 1 instead of 3. What is wrong?

  1. The list has too many no votes.
  2. Set count to 0 is inside the loop, so the count restarts on every pass.
  3. The test should be v = no.
  4. say count is in the right place, so nothing is wrong.
Explain

15. Explain why Set count to 0 goes before the loop, the If goes inside the loop, and say count goes after the loop.

Challenge

Challenge practice

Combine tests, find an average and watch a running total.

Average of matches

16. Set scores to [8, 3, 6, 10, 4]. Set count to 0. Set sum to 0. For each s in scores [If s ≥ 6 [Change count by 1; Change sum by s]]. say sum ÷ count. What is said?

  1. 8
  2. 6.2
  3. 3
  4. 24
Two tests

17. Set nums to [2, 3, 9, 7, 5, 8]. Count the numbers from 3 to 7, including 3 and 7. Which test, and what count?

  1. n > 3 and n < 7; count 1
  2. n ≥ 3 or n ≤ 7; count 6
  3. n ≥ 3 and n ≤ 7; count 3
  4. n ≥ 3 and n ≤ 7; count 2
Running total

18. Set total to 0. Set count to 0. For each d in [4, 6, 3, 8] [Change total by d; If total > 10 [Change count by 1]]. say count. What is said?

  1. 2
  2. 4
  3. 3
  4. 1
Write the loop

19. Write a program that uses one loop to count how many numbers in [12, 5, 20, 8, 15] are at least 10 and to add those numbers up. What does it say?

Design

20. A hockey team's goals in six games are [3, 0, 5, 2, 0, 4]. Design one loop that finds how many games they scored in, how many they did not, and how many goals they scored altogether.

Answer key

  1. 1. A counter changes by 1 for each match; a total changes by the item's value
    A counter answers how many and a total answers how much. Neither is always bigger, both are set before the loop and changed inside it, and mixing them up is one of the most common bugs.
  2. 2. 2
    Only -2 and -5 are below 0, so cold is 2. 3 wrongly counts 0 as below zero, -7 adds the cold temperatures instead of counting them, and 5 is the length of the list.
  3. 3. 3
    red appears three times, so n is 3. 1 stops after the first match, 5 counts every shirt, and 2 misses the last red.
  4. 4. 14
    Only 8 and 6 pass n > 4, and 8 + 6 = 14. 18 adds every number, 2 counts the matches instead of adding them, and 8 keeps only the first match.
  5. 5. Inside the If, inside the loop
    It must run only when the test passes, so it goes inside the If. Before or after the loop it runs only once, and after the If it runs on every pass, so it counts everything.
  6. 6. 3
    At least 9 includes 9, so 9, 10 and 9 count: 3. 1 comes from using a > 9, which leaves out both 9s, 5 is everyone, and 2 misses one of the 9s.
  7. 7. 2, then 62
    40 and 22 pass, so count is 2 and sum is 62. 4 and 86 use every book, 2 and 2 changes sum by 1 instead of by p, and 3 and 77 wrongly lets 15 pass a test of more than 20.
  8. 8. 4, then 2
    6, 5, 4 and 6 are at least 4, so high is 4 and low is 2; 4 + 2 = 6 rolls. 3 and 3 forgets that 4 counts, 4 and 6 changes low on every pass, and 2 and 4 swaps the two branches.
  9. 9. 12
    3, 7 and 2 are under 10, and 3 + 7 + 2 = 12. 3 counts the cheap items instead of adding them, 39 adds every price, and 27 adds the prices that are 10 or more.
  10. 10. 4, on G
    Four items are F, so steps is 4, and the trip of one right, two down, then one right lands on G. 6 counts the turns too, 3 misses a step, and the robot does not stop above G because the last F moves it to the right along the bottom row.
  11. 11. Change count by 1 is outside the If, so it runs on every pass; move it inside the If.
    Because Change count by 1 sits after the If, it runs for all four items. Flipping the test would count the wrong scores, starting at 1 would give 5, and saying inside the loop would only show the wrong running count.
  12. 12. Change count by 1 instead of by s
    7 + 9 = 16: the program is adding the scores, not counting them. A counter must change by 1. s ≥ 5 still adds, resetting inside the loop wipes the count, and doubling makes the total even bigger.
  13. 13. v ≤ 10
    At most 10 includes 10, so v ≤ 10 counts 10, 4 and 10. v > 10 counts only the 12, v = 10 misses the 4, and v ≥ 4 counts all four waits, including the 12.
  14. 14. Set count to 0 is inside the loop, so the count restarts on every pass.
    The counter is wiped before every item, so only the last vote is left: yes, so 1. Move Set count to 0 before the loop. The votes, the test and the say step are not the problem.
  15. 15. Before the loop, the counter must start at 0 only once. Inside the loop, the If checks every item one at a time. After the loop, every item has been checked, so say gives one final answer.
    Strong answers name all three places and say what would go wrong if a step moved, such as a counter that is wiped or a running tally said many times.
  16. 16. 8
    8, 6 and 10 pass, so sum is 24 and count is 3, and 24 ÷ 3 = 8. 6.2 is the average of every score, 3 is the count, and 24 is the sum before dividing.
  17. 17. n ≥ 3 and n ≤ 7; count 3
    3, 7 and 5 are in the range, so the count is 3. The strict tests leave out 3 and 7 (count 1), the or test lets in all six numbers because each one passes at least one of its two tests, and count 2 misses one of the numbers.
  18. 18. 2
    The total goes 4, 10, 13, 21. It is more than 10 only at 13 and 21, so count is 2. 3 wrongly counts the 10, 4 counts every pass, and 1 stops after the first time it passes.
  19. 19. Set nums to [12, 5, 20, 8, 15]. Set count to 0. Set sum to 0. For each n in nums [If n ≥ 10 [Change count by 1; Change sum by n]]. say count, then sum. It says 3, then 47.
    12, 20 and 15 pass the test. Both Change steps belong inside the same If; the counter changes by 1 and the total changes by n.
  20. 20. Set scored to 0, blank to 0 and goals to 0. For each g in games [Change goals by g; If g > 0 [Change scored by 1] Else [Change blank by 1]]. It finds 4 games with a goal, 2 without and 14 goals.
    The goals total changes on every pass, so it sits outside the If but inside the loop. The If–Else splits the games into two counters, and 4 + 2 = 6 checks that every game was counted once.

Related practice