Skip to content
SPM Tuition
Computer Science · Program structure and debugging

Explaining a small correction, not a rewrite

You fix a faulty program by rewriting all of it and cannot say what was actually wrong.

A good correction changes as little as possible and explains why the change works. Finding the exact line shows understanding, while rewriting everything only shows that you can write a different program.

This lesson is part of program structure and debugging. It uses the classification skill from distinguishing a syntax error from a logically wrong result.

What is the four-part frame?

Write four short statements.

  1. Where: name the line or the condition.
  2. What: say what it does now.
  3. Why: show the wrong result it causes on one input.
  4. Check: show the corrected line giving the right result.

If an answer carries several marks, cover all four parts, including the last one.

Worked example: sum of even numbers

The program should add the even numbers from 1 to 10. That is 2 + 4 + 6 + 8 + 10 = 30.

SET sum = 0
FOR i = 1 TO 10
  IF i MOD 2 = 1 THEN
    SET sum = sum + i
  END IF
NEXT i
OUTPUT sum

Trace it: the IF is true for i = 1, 3, 5, 7 and 9, so the sum is 1 + 3 + 5 + 7 + 9 = 25. The output is 25, not 30.

Part Explanation
Where The condition i MOD 2 = 1
What It is true for odd numbers
Why The program adds 1 + 3 + 5 + 7 + 9 = 25
Check With = 0, it adds 2 + 4 + 6 + 8 + 10 = 30

The correction is one character: change 1 to 0.

Why not just rewrite it?

A student might delete the program and write a new loop FOR i = 2 TO 10 STEP 2. That also gives 30, so it is not wrong, but it does not answer the question.

Approach What the marker sees
Whole rewrite A new program, no sign the fault was found
One-character fix with four parts The fault located, explained and checked

Most of the original program was correct: the initial value, the loop, the addition and the output. Keeping those lines is the point.

Check yourself

This program should print 5, 4, 3, 2, 1 but prints 1, 2, 3, 4, 5. Give the smallest correction and explain it.

FOR i = 1 TO 5
  OUTPUT i
NEXT i
Answer

Where: the FOR line. What: it counts up from 1 to 5. Why: so the output is 1, 2, 3, 4, 5. Correction: write FOR i = 5 TO 1 STEP -1, keeping the OUTPUT line.

Check: i takes 5, 4, 3, 2, 1 in turn, so the output is 5, 4, 3, 2, 1.

What to study next

Test the whole set with the program structure and debugging practice questions. Use the mistake log and paper-error review to record which faults you find hardest to explain.

If you want a teacher to check your explanations, see online one-to-one Computer Science tuition.

Common questions

Why is a small correction better than a rewrite?

It shows that you found the exact fault, it keeps the parts that were already right, and it avoids adding new errors. A full rewrite may work but does not show that you understood what went wrong.

What should an explanation of a correction include?

State where the fault is, what the line does wrongly, why that gives the wrong result with an example input, and how you checked that the corrected line works.

What if the program has more than one fault?

Fix and explain one fault at a time, re-testing after each. If you change two lines together and the output is still wrong, you cannot tell which change helped.

How do I find the faulty line quickly?

Trace the program on a small input with a known answer, and compare each variable with what you expect. The first row where they differ points to the line.

If you rewrite programs instead of repairing them, a one-to-one Computer Science teacher can practise the four-part explanation with you on new faulty code.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.