Form 2 · Chapter 3

Coding Structure

Coding structure is how a program is organised, using variables to store data and sequence, selection and loops to control the flow.

Variables and constants

A variable is a named box in memory that stores a value which can change while the program runs. A constant is similar, but its value never changes. We give each a clear name, such as score or price, so the code is easy to read.

Key idea

Programs are built from three control structures: sequence, selection and repetition (loops). Data flows in through input and out through output.

The three structures

Sequence runs instructions in the order they are written. Selection chooses between paths using a condition, for example:

IF age >= 18 THEN
  OUTPUT "Adult"
ELSE
  OUTPUT "Minor"
ENDIF

Repetition (a loop) repeats instructions. A loop that counts from 1 to 5 saves you from writing the same line five times, and it can repeat a fixed number of times or keep going until a condition is met. These three structures can be nested inside one another, for example a selection placed inside a loop, which lets a short program handle quite complex tasks.

Example

A loop that greets three times:

FOR i FROM 1 TO 3
  OUTPUT "Hello"
NEXT i

Operators

An operator is a symbol that acts on data. Arithmetic operators like + and - do maths, while comparison operators like > and = test conditions used inside selection and loops. A whole program is really just these pieces working together: values are stored in variables, changed with operators, and guided by sequence, selection and repetition until the task is done. Choosing clear names and the right structure keeps a program tidy and much easier to fix or improve later on.

Stuck on this topic? A verified JomKelas tutor can walk you through it.

Find a verified tutor