Programming concepts — data and control structures
Programming building blocks
- Programs store data in variables and constants, each with a data type.
- They are built from three control structures.
- We finish with the totalling and counting patterns.
Variables, constants and data types
- A variable can change; a constant is fixed. Declare both before use.
- Five basic data types: integer (whole number), real (decimal), char (one character), string (text), Boolean (
TRUE/FALSE).
DECLARE score : INTEGER
CONSTANT Pi = 3.142
Practice
What is the difference between a variable and a constant?
A variable's value can change; a constant is set once and never changes.
Practice
Match each value to its data type.
Whole number = integer; decimal = real; text in quotes = string (a single char like 'A' is char).
Sequence and selection
- Sequence — steps run one after another, top to bottom.
- Selection — choose which steps run, with
IF…ELSE(orCASEfor many options):
IF score >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Practice
Which structure chooses which steps to run based on a condition?
Selection branches on a condition; sequence runs in order; iteration repeats.
Iteration and totalling
- Count-controlled (
FOR) repeats a fixed number of times. - Pre-condition (
WHILE) tests before — may run zero times. Post-condition (REPEAT…UNTIL) tests after — runs at least once. - Totalling:
total ← total + value; counting:count ← count + 1.
Practice
Which loop always runs its body at least once?
REPEAT tests after the body, so it runs at least once; WHILE tests first and may run zero times.
You've got it
Key idea
- variable changes, constant is fixed; types: integer, real, char, string, Boolean
- three structures: sequence, selection (IF/CASE), iteration (FOR/WHILE/REPEAT)
- WHILE tests before (0+ times); REPEAT tests after (1+ times)
- totalling = running total; counting = add 1 each time
Practice
Which line keeps a running total?
total ← total + value accumulates the sum; count ← count + 1 counts occurrences.