Coding to Analyse Data: Subprograms and Nested Loops

What your child is learning
By Grade 8 children have written loops and decisions for several years. This year the code has a job: going through a set of data, counting what is there and turning the counts into something readable, such as a percent. Two tools make that practical.
Children learn to:
- Write a subprogram, a named block, and call it more than once.
- Use a loop to go through a list of data and count the items that match.
- Put a decision inside a loop, and a loop inside another loop.
- Turn counts into percents so that groups of different sizes can be compared.
- Test code on a small set of data before running it on the full set.
Loops inside loops are introduced in Coding with Nested Events, and named blocks in Coding with Nested Events and Subprograms. Here both tools are put to work on data.
Why it matters
Twenty answers can be tallied by hand. Two hundred cannot, at least not without mistakes. A short program that counts, checks and converts to percents does in a second what would take an afternoon, and gives the same result every time.
Reading data with code is also how most statistics are produced today. Even a child who never programs for a living benefits from knowing that a percent on a screen came from a count divided by a total.
How to teach it
1. Count one answer by hand
Write twelve answers on paper: pizza, sushi, pizza, tacos, pizza, sushi, pizza, sushi, tacos, sushi, pizza, pizza. Ask how many say pizza. Six. Then ask how your child knew when to stop: they went through the list one item at a time and added one each time they saw the word. That is exactly what the loop will do.
2. Turn the method into a loop
Say the steps aloud in order: start the pizza count at zero; look at the first answer; if it says pizza, add one; move to the next answer; stop after the last one. Those steps are a program. In a block language the middle steps sit inside a "for each item in the list" block.
3. Name it so you can use it again
The same steps count sushi or tacos; only the word being compared changes. Make a block called count (choice), with a blank for the word. Now the program reads: count (pizza), count (sushi), count (tacos). Each call runs the same loop. That named block is a subprogram: written once, used as often as needed.
4. Put loops inside loops
Instead of three separate calls, put the counting subprogram inside another loop that steps through a list of choices. The outer loop runs once per choice, three times; the inner loop runs once per answer, twelve times on each pass. Altogether 3 × 12 = 36 comparisons. Predicting that product before running is a good check that the structure is understood.
define count (choice)
set tally to 0
for each answer in the list
if answer = choice then change tally by 1
say tally ÷ (number of answers) × 100
5. Turn counts into percents
A count of 6 says little until it is compared with the whole set. Divide by the total, 12, and multiply by 100: 6 ÷ 12 × 100 = 50%. The program can do this step too, using the length of the list as the total, so nothing has to be typed in by hand.
6. Test small, then trust it big
Run the program first on the twelve answers counted by hand. If the screen shows 6, 4 and 2, the code works. Only then load the survey from all three classes. Fixing a program on twelve items takes seconds; on three hundred, the errors hide.
Common mistakes
- Counting the wrong text. The comparison has to match the exact text in the list. "Pizza" with a capital will not equal "pizza" unless the check ignores case.
- Forgetting to reset the tally. A subprogram that starts from the last total gives sushi the count of pizza plus sushi. Set the tally to zero at the start of the subprogram.
- Dividing by the wrong total. The percent uses the number of answers, not the number of choices. Six pizza answers out of twelve are 50%, not 200%.
- Putting the report inside the loop. A "say" block inside the loop prints after every answer. Move it after the loop so it reports once, at the end.
- Trusting the first run. A program that finishes has not necessarily finished correctly. Check the output against a hand count before using the results.
Do this together
Code a vote counter. Collect real answers from the family over a day: favourite season, with the choices spring, summer, fall or winter. Aim for at least twenty answers, written in a list. Count them by hand first and record the counts.
Now build the program together in a block editor: a subprogram that takes a choice, loops through the list, counts the matches and reports the percent. Test it on five answers, then on the full list, and compare with the hand count. Finish by asking a question the percents can answer, such as which two seasons together make up half the family.
Printable worksheet
19 questions, a picture warm-up and drawing tasks, with an answer key on its own page. Print it or save it as a PDF.
Every worksheet is on the printable worksheets page.
Ontario curriculum: Grade 8, Algebra, expectation C3 · official document · U.S. Common Core crosswalk