Creating my own algorithm with branching, repetition and a counter

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Creating my own algorithm with branching, repetition and a counter

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which algorithm uses a decision, a loop and a counter together to count how many pencils in a tray are sharp?
A SET count TO 0. REPEAT for each pencil: IF it is sharp THEN ADD 1 to count. SAY count.
B SET count TO 0. REPEAT for each pencil: SAY pencil. SAY count.
2. A game algorithm gives the player 3 lives. Which part is the counter?
A SET lives TO 3, then SUBTRACT 1 each time the player is hit
B IF the player is hit THEN play a sound
3. Which recipe algorithm repeats a step the right number of times?
A SET eggs TO 0. REPEAT UNTIL eggs = 3: ADD 1 egg to the bowl, ADD 1 to eggs.
B SET eggs TO 0. ADD 1 egg to the bowl. ADD 1 to eggs. STOP.
4. Your classroom algorithm should count students who brought a hat. Where does the IF go?
A After the REPEAT, so it checks only the last student
B Inside the REPEAT, so it checks every student
5. You wrote an algorithm to count red cards. What is the best way to check that it works?
A Read the title and decide it looks right
B Follow it step by step with a small set of cards and track the count

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1SET count TO 0 REPEAT 5 TIMES IF page is torn THEN ADD 1 TO count AA school routine that checks 5 library books and counts how many pages are torn
2SET lives TO 3 REPEAT UNTIL lives = 0 IF hit THEN TAKE 1 FROM lives BA game that keeps going until the player has lost all 3 lives
3A recipe algorithm that adds eggs one at a time, counts them and stops at 3 Ccount = 0, round 1: IF true THEN count = 1, round 2: IF false THEN count stays 1
4Checking your algorithm by following it step by step with a small starting value DThe counter was never set to 0 before the loop, so we do not know what number it starts from.
5REPEAT 10 TIMES IF homework done THEN ADD 1 TO count EIf the answer is wrong, score never changes, so the loop could never stop. The counter must go up each round, or the loop needs a way to end.
6SET score TO 0 REPEAT UNTIL score = 10 IF answer correct THEN ADD 1 TO score FSET eggs TO 0 REPEAT UNTIL eggs = 3 ADD 1 egg ADD 1 TO eggs

Definitions

REPEAT + IF + count

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
SET count ← _ REPEAT 5 TIMES IF bag has book THEN ADD 1 to count
2.
SET lives ← 3 REPEAT _____ lives = 0 IF hit THEN SET lives ← lives - 1
3.
SET eggs ← 0 REPEAT 3 TIMES __ egg is fresh THEN ADD 1 to eggs
4.
SET stirs ← 0 REPEAT UNTIL stirs = 10 SET stirs ← stirs _ 1
5.
SET count ← 0 REPEAT 3 TIMES ADD 1 TO count SAY _____

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A SET ready TO 0
B REPEAT 5 TIMES
C IF bag is packed
D ADD 1 TO ready
E END IF
F END REPEAT
Question 2
A SET stars TO 0
B REPEAT 3
C IF card is a star THEN
D ADD 1 TO stars
E END IF
F END REPEAT
Question 3
A SET stirs TO 0
B REPEAT UNTIL stirs = 10
C STIR the mixture
D SET stirs TO 1
E END REPEAT
Question 4
A SET cracked TO 6
B REPEAT 6 TIMES
C IF egg is cracked THEN
D ADD 1 TO cracked
E END IF
F END REPEAT
Question 5
A SET late TO 0
B REPEAT 5 TIMES
C IF student is late THEN
D ADD 1 TO days
E END IF
F END REPEAT

Answer Key

Creating my own algorithm with branching, repetition and a counter
Section A: Quick Questions
Q1
A — SET count TO 0. REPEAT for each pencil: IF it is sharp THEN ADD 1 to count. SAY count.
Q2
A — SET lives TO 3, then SUBTRACT 1 each time the player is hit
Q3
A — SET eggs TO 0. REPEAT UNTIL eggs = 3: ADD 1 egg to the bowl, ADD 1 to eggs.
Q4
B — Inside the REPEAT, so it checks every student
Q5
B — Follow it step by step with a small set of cards and track the count
Section B: Match the Pairs
Q1
SET count TO 0 REPEAT 5 TIMES IF page is torn THEN ADD 1 TO count → A school routine that checks 5 library books and counts how many pages are torn
Q2
SET lives TO 3 REPEAT UNTIL lives = 0 IF hit THEN TAKE 1 FROM lives → A game that keeps going until the player has lost all 3 lives
Q3
A recipe algorithm that adds eggs one at a time, counts them and stops at 3 → SET eggs TO 0 REPEAT UNTIL eggs = 3 ADD 1 egg ADD 1 TO eggs
Q4
Checking your algorithm by following it step by step with a small starting value → count = 0, round 1: IF true THEN count = 1, round 2: IF false THEN count stays 1
Q5
REPEAT 10 TIMES IF homework done THEN ADD 1 TO count → The counter was never set to 0 before the loop, so we do not know what number it starts from.
Q6
SET score TO 0 REPEAT UNTIL score = 10 IF answer correct THEN ADD 1 TO score → If the answer is wrong, score never changes, so the loop could never stop. The counter must go up each round, or the loop needs a way to end.
Section C: Definitions
Q1
REPEAT + IF + count — A finished algorithm joins steps, a decision (IF), a loop (REPEAT) and a counter to solve a school, game or recipe problem. You check it by following it step by step.
Section D: Fill in the Blanks
Q1
SET count ← 0 REPEAT 5 TIMES IF bag has book THEN ADD 1 to count
Q2
SET lives ← 3 REPEAT UNTIL lives = 0 IF hit THEN SET lives ← lives - 1
Q3
SET eggs ← 0 REPEAT 3 TIMES IF egg is fresh THEN ADD 1 to eggs
Q4
SET stirs ← 0 REPEAT UNTIL stirs = 10 SET stirs ← stirs + 1
Q5
SET count ← 0 REPEAT 3 TIMES ADD 1 TO count SAY count
Section E: Spot the Error
Q1
C — An IF decision needs the word THEN after its condition. Line 3 should be: IF bag is packed THEN.
Q2
B — A set-number loop must say TIMES so we know it repeats a fixed number of times. Line 2 should be: REPEAT 3 TIMES.
Q3
D — Setting stirs to 1 each time means the counter never goes up past 1, so the loop never stops. Line 4 should be: ADD 1 TO stirs.
Q4
A — A counter must start at 0 before we begin counting. Starting at 6 gives the wrong total. Line 1 should be: SET cracked TO 0.
Q5
D — The counter being used is late, so we must add 1 to late. Adding to a different variable leaves late unchanged. Line 4 should be: ADD 1 TO late.