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.
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?
- cold starts at 0, before the loop.
- -3 < 0 yes (cold 1). 2 no. 0 no, because 0 is not below 0. -1 yes (cold 2). 4 no.
- 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?
- The test is c ≥ 25, so a 25 counts.
- 25 yes (total 25). 5 no. 100 yes (total 125). 10 no. 25 yes (total 150).
- 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.
- Trace table: p | p > 10? | count | sum.
- 12 | yes | 1 | 12. 30 | yes | 2 | 42. 8 | no | 2 | 42. 25 | yes | 3 | 67.
- 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.
- Every item goes into exactly one branch.
- pizza (p 1), tacos (t 1), pizza (p 2), pizza (p 3), tacos (t 2).
- 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?
- F, F: two squares right. R: turn to face down (no step).
- F, F: two squares down to the bottom row. L: turn to face right (no step). F: one square right onto G.
- 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
Why this fails: Then the count changes on every pass, match or not, and the answer is just the length of the list.
Why this fails: Change count by s adds up the matching values. That answers how much, not how many.
Why this fails: At most 10 includes 10, so the test is v ≤ 10. The test v < 10 leaves out every 10.
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.
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.
1. What is the difference between a counter and a total?
- A counter is always bigger than a total
- A counter changes by 1 for each match; a total changes by the item's value
- A counter goes before the loop and a total goes after it
- They are the same thing with different names
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?
- 2
- 3
- -7
- 5
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
- 5
- 2
- 3
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?
- 18
- 2
- 14
- 8
5. You want to count the scores above 5. Where should Change count by 1 go?
- Before the loop
- Inside the loop but after the If
- Inside the If, inside the loop
- After the loop
Practice
Practice practice
Choose the right test, keep two variables and use If–Else.
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
- 5
- 2
- 3
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.
- 4, then 86
- 2, then 2
- 2, then 62
- 3, then 77
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.
- 3, then 3
- 4, then 2
- 4, then 6
- 2, then 4
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?
- 3
- 39
- 27
- 12
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?
- 6, on G
- 4, one square above G
- 3, on G
- 4, on G
Think
Think practice
Find the bug in tally programs and explain the pattern.
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?
- Change count by 1 is outside the If, so it runs on every pass; move it inside the If.
- The test should be s < 5.
- Set count to 0 should be Set count to 1.
- say count should be inside the loop.
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?
- Change the test to s ≥ 5
- Change count by 1 instead of by s
- Move Set count to 0 inside the loop
- Change count by s × 2
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?
- v > 10
- v = 10
- v ≤ 10
- v ≥ 4
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?
- The list has too many no votes.
- Set count to 0 is inside the loop, so the count restarts on every pass.
- The test should be v = no.
- say count is in the right place, so nothing is wrong.
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.
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?
- 8
- 6.2
- 3
- 24
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?
- n > 3 and n < 7; count 1
- n ≥ 3 or n ≤ 7; count 6
- n ≥ 3 and n ≤ 7; count 3
- n ≥ 3 and n ≤ 7; count 2
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?
- 2
- 4
- 3
- 1
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?
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. 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
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
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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.