Skip to content
SPM Tuition
Computer Science chapter guide

Programming foundations

You can copy a program from the textbook but cannot yet write or predict one of your own.

Programming foundations are the small building blocks every program uses: variables, data types, decisions, loops and functions. Once you can predict what each block does, larger programs are just these blocks arranged in order.

This section sits inside the SPM Computer Science subject hub. It pairs naturally with problem solving and algorithms, which covers planning before you code.

How do the parts connect?

Each idea depends on the previous one.

  • Variables and data types store values and decide which operations are allowed.
  • Selection uses those values in a condition to choose one path.
  • Loops repeat a path while a condition holds, so the boundaries must be exact.
  • Functions wrap a set of steps with a name, taking values in and sending one back.

A fault in an early block travels upward. A wrongly typed variable can make a condition false, and that in turn makes a loop stop at the wrong place.

A small program that uses every idea

CONSTANT PASS_MARK = 40
FUNCTION result(mark)
  IF mark >= PASS_MARK THEN
    RETURN "Pass"
  ELSE
    RETURN "Fail"
  END IF
END FUNCTION

FOR i = 1 TO 3
  INPUT score
  OUTPUT result(score)
NEXT i

Enter 52, 40 and 39. The outputs are Pass, Pass and Fail. Here the constant never changes, the variable score changes on each pass, the IF chooses a path, the loop runs three times, and the function returns one answer.

If score had been stored as text, the comparison with 40 would be unreliable. That is why the first lesson is about types.

Where should you start?

Choose the row that matches your symptom.

Symptom Start with
Unsure what to store a value as Variables, constants and data types
A loop runs one time too many or too few Writing loops with correct boundaries
Lost inside a function call Tracing functions and parameters
Program runs but behaves strangely Program structure and debugging

Test the whole set with the programming foundations practice questions. The restricted pseudocode trace trainer gives extra tracing drills on your own device.

If you would like a teacher to build programs with you from the blank page, see online one-to-one Computer Science tuition.

Common questions

Which programming language does SPM Computer Science use?

The subject is taught through pseudocode and flowcharts, and schools may add a language for practice. The reasoning in these pages uses plain pseudocode, so it applies whichever language your school teaches.

What order should I learn these topics in?

Start with variables and data types, then selection, then loops, then functions. Each topic uses the one before it, so skipping ahead usually means returning to fix the earlier gap.

Why do my programs run but give the wrong answer?

The most common causes are a wrong data type, a comparison that is off by one at a boundary, or a loop that runs one pass too few or too many. Tracing with a table usually finds the line quickly.

Do I need a computer at home to practise?

Not for the reasoning skills. Reading, tracing and writing pseudocode can all be done on paper, which is also how you will be asked to show them in an examination.

If you can read a program but stall when you must write one from a blank page, one-to-one Computer Science lessons let a teacher build it with you one structure at a time.

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