Logical reasoning: predicting a program
Computer Science · Key Stage 2 · The School
Read it before you run it
A computer does exactly what the instructions say, in order, every time. That means you can work out what a program will do by reading it — track each line and write down what each value is after it. Predicting first is what turns running the program into a TEST: if it does what you predicted, you understood it; if not, you have found something worth knowing.
The gap is the lesson
When the result differs from your prediction, one of two things is true: the program is wrong, or your understanding is. Both are worth finding, and you cannot tell which until you look. Change one thing, predict again, run again. Guessing and re-running without a prediction can accidentally produce the right answer while leaving you no wiser.
Reasoning to the bug
Instead of reading every line, split the program in half and check whether the values are still correct at the middle. If they are, the fault is in the second half; if not, the first. Repeat on that half. A twenty-line program takes about four checks to narrow down this way, rather than twenty. Professionals debug exactly like this.
The program: set score to 0; repeat 3 times { add 2 to score }; if score > 5 then say "win" else say "lose". PREDICT FIRST, in writing: score goes 0, 2, 4, 6; 6 is greater than 5, so it says win. NOW run it. If your prediction was wrong, you have found either a bug or a gap in your understanding, and you know exactly where to look — which is why predicting first is not a formality.