Skip to the page
Year 7 · Spring 2 · Scratch I: Programming essentials

Debugging with PRIMM

PracticalPresent ▶

What this lesson covers

Topic 11 Algorithms

KS3 Computing · An algorithm is a precise, step-by-step plan to solve a problem. Computational thinking turns a messy problem into one. Every algorithm is built from just three things: sequence, selection and iteration.

Programming Computational ThinkingCambridge Lower Secondary Computing 0860

You will learn to

  • Use PRIMM to read a script: predict what it does before running it.
  • Identify the block where a buggy script first goes wrong.
  • Fix a bug, test it again, and explain the fix in one sentence.

Success criteria

  • I can write my prediction down before I click the green flag.
  • I can find and fix the bug in three buggy scripts.
  • I can explain each fix in one clear sentence.

Key words

Say each word, then read what it means. Every one is in the glossary too.

Algorithm
Precise, ordered, finite steps to solve a problem.
Decomposition
Breaking a problem into smaller ones.
Abstraction
Ignoring detail that doesn't matter for the problem.
Sequence · selection · iteration
Steps one after another. First this, then this.
Pseudocode
Code-like writing for algorithms, without strict rules.
Trace table
Records variable values step by step to check logic.
Variable
A named place in memory that holds a value.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

Bugs are normal

A bug is a mistake in a program that makes it do the wrong thing.

Debugging means finding bugs and fixing them.

Every programmer writes bugs. Good programmers find them calmly, one step at a time.

★Remember

Scratch blocks only snap together where they fit. So most Scratch bugs are logic errors: the script runs, but does the wrong thing.

PRIMM: a way to read code

PRIMM is five steps for working with a program someone else wrote.

LetterStepWhat you do
PPredictRead the code. Say what you think it will do, before you run it.
RRunRun it and watch. Was your prediction right?
IInvestigateLook closely. Trace it block by block. Change one thing and see what happens.
MModifyChange the code so it does something new.
MMakeWrite your own program using the same idea.
?Why?

Why predict first?

A wrong prediction is useful. The place where the code surprises you is where to look for the bug.

Debugging step by step

  1. Predict: what should the script do?
  2. Run it.
  3. Compare: what did it really do? Where is it different?
  4. Find the first block where it goes wrong.
  5. Fix one thing at a time.
  6. Run it again to check the fix works.
  7. Explain the fix in one sentence.
!Watch out

Change only one thing, then test. Change three things at once and you will not know which one fixed it.

Common Scratch bugs

BugWhat you seeFix
Variable never resetThe score starts at the last game's numberPut set [score v] to (0) under when green flag clicked
Wrong signLives go up instead of downUse change [lives v] by (-1)
Missing loopThe sprite moves once, then stopsPut the blocks inside forever or repeat
Wrong numberRepeat 3 with turn 90 draws only three sides of a squareUse repeat (4)
Blocks in the wrong orderThe sprite says 'Done!' before it movesDrag the say block below the move blocks
Script on the wrong spriteThe wrong sprite movesDrag the script onto the right sprite
e.g.Worked example

This script should count lives down from 3 and then say 'Game over'. It never says it. Why? Script: set lives to 3. Repeat until lives = 0: wait 1 second, change lives by 1. Say Game over.

  1. Predict: lives should go 3, 2, 1, 0, then the sprite says 'Game over'.
  2. Run: lives go 4, 5, 6, 7 … and never stop.
  3. Compare: lives go up, not down, so they never reach 0.
  4. Find: the block change [lives v] by (1) adds 1.
  5. Fix: change it to change [lives v] by (-1) and run it again.
  6. Explain: 'The loop never ended because lives went up, so they never reached 0.'

Study it

Open the study page and go one step at a time.

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.