Skip to content
SPM Tuition
Computer Science chapter guide

Program structure and debugging

Your program runs without an error message yet the answer on screen is still wrong.

Debugging is the skill of finding why a program behaves differently from what you intended, and fixing it with the smallest change that works. It depends on the tracing habits from programming foundations.

This section gives four lessons that take you from locating a fault to explaining it clearly.

What does this section cover?

How do the lessons connect?

First you learn where values live, because a variable in the wrong place is a fault you cannot see. Then you study the most common structural fault, a loop that stops at the wrong time.

Next you learn to name the kind of fault, and last you practise explaining the smallest change that repairs it. In an answer, explaining the fix matters as much as making it.

A short bug to orient you

This program should output the average of three marks.

FOR i = 1 TO 3
  SET total = 0
  INPUT mark
  SET total = total + mark
NEXT i
OUTPUT total / 3

Enter 60, 70 and 80. The correct average is 210 ÷ 3 = 70. The program outputs 80 ÷ 3, which is about 26.7.

A trace shows why: SET total = 0 sits inside the loop, so total resets on every pass and only the last mark survives. The fix is to move that line above the loop.

Who should start where?

If you notice Start with
A variable has a value you did not expect after a call Scope lesson
The program freezes or stops too soon Loop termination lesson
You cannot tell whether an error is a typo or a design fault Syntax versus logic lesson
You rewrite a whole program to fix one line Small correction lesson

Test yourself with the section practice questions. The restricted pseudocode trace trainer lets you rehearse tracing on your own device, and the mistake log and paper-error review keeps track of your repeat slips.

If you would like a teacher to go through your own buggy programs, see online one-to-one Computer Science tuition.

Common questions

What is the difference between a syntax error and a logic error?

A syntax error breaks the rules of the language, so the program will not run. A logic error follows the rules but does the wrong thing, so the program runs and gives an incorrect result. Logic errors are harder because nothing warns you.

Where do I start when a program gives a wrong answer?

Pick one small input where you know the correct answer, trace the program on it, and compare each variable with what you expect. The first row that differs points to the faulty line.

Should I rewrite the program if it has a bug?

Usually not. A small, explained correction shows you understood the fault, and it keeps the parts that were already right. Replacing everything hides the cause and can introduce new errors.

Is debugging tested in SPM Computer Science?

Finding and explaining errors in given pseudocode fits a subject built on algorithms. Check the current assessment details with your teacher or on the Lembaga Peperiksaan website.

If your programs run but give the wrong answer, one-to-one Computer Science lessons let a teacher trace your own code with you and teach you to locate the fault rather than rewrite everything.

  • 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.