← IGCSE Computer Science · Cambridge · GCSE Computer Science revision
⭐ 0
Cambridge IGCSE Computer Science (0478/0984) · Topic 8 Programming
Mini-Lesson

Programming

5 missions. Guess, learn, then check. Your place is saved.

  1. Mission 1How does a game turn 135 seconds into 2:15 on its timer?
  2. Mission 2How can a few lines of code print all 100 times-table answers?
  3. Mission 3How can one name hold all 9 squares of a noughts and crosses board?
  4. Mission 4Two subroutines both use a variable called total. Why no clash?
  5. Mission 5How does an app show only the last 4 digits of your card number?
Mission 1

How does a game turn 135 seconds into 2:15 on its timer?

Make a guess: How does a game turn 135 seconds into 2:15 on its timer?

Programming · data and operators

Variables, constants, input and output

Variables and constantslives32assignmentvariableMAX_LIVES3constantvariable: value can changeconstant: value fixed while running
  • A variable is a named memory location whose value can change while the program runs.
  • A constant is a named value that cannot change while the program runs. Naming it means one place to update it, and clearer code.
  • Assignment puts a value into a variable; the first assignment is initialisation. Use meaningful identifiers like lives, not x.
  • Declare every variable with DECLARE and a data type.
Cambridge pseudocode
CONSTANT MaxLives ← 3
DECLARE Lives : INTEGER
DECLARE PlayerName : STRING
Lives ← MaxLives
OUTPUT "Enter your name"
INPUT PlayerName
OUTPUT "Good luck, ", PlayerName
Lives ← Lives - 1
OUTPUT Lives
Programming · data and operators

Data types

  • A data type tells the computer what kind of data a variable holds and which operations make sense.
Data typeHoldsExamples
Integera whole number42, -7, 0
Reala number that can have a fractional part3.5, -0.25, 12.0
Booleanone of two valuesTrue, False
Characterone single character'A', '7', '@'
Stringa sequence of characters'Hello', '07700 900123'
  • Cambridge keywords: INTEGER, REAL, CHAR, STRING, BOOLEAN. A real is always written with a digit each side of the point, e.g. 4.0.
Quick check
?A program stores a customer's phone number, such as 07700 900123. Which data type is most suitable?
Programming · data and operators

Arithmetic operators

Integer division and remainder200 seconds60606020integer division = 3200 DIV 60remainder = 20200 MOD 60
  • +, - and * work as in maths. Real division / can give a fractional answer.
  • Integer division gives only the whole-number part. The remainder (modulus) is what is left over.
Operation200 seconds ÷ 60Result
Real division200 / 603.333…
Integer divisionDIV(200, 60)3
RemainderMOD(200, 60)20
  • In Cambridge pseudocode DIV and MOD are library routines written like functions, with integer values.
Cambridge pseudocode
DECLARE Total : INTEGER
DECLARE Minutes : INTEGER
DECLARE Seconds : INTEGER
Total ← 200
Minutes ← DIV(Total, 60)    // 3
Seconds ← MOD(Total, 60)    // 20
Calculate
?A shop packs eggs in boxes of 6. It has 47 eggs. What value does the program store in spare?
Cambridge pseudocode
DECLARE Boxes : INTEGER
DECLARE Spare : INTEGER
Boxes ← DIV(47, 6)
Spare ← MOD(47, 6)
Calculate
?What value is stored in result?
Cambridge pseudocode
Result ← 2 ^ 5
Mission check

?How does a game turn 135 seconds into 2:15 on its timer?
Mission 2

How can a few lines of code print all 100 times-table answers?

Make a guess: How can a few lines of code print all 100 times-table answers?

Programming · control flow

Sequence and selection

Selection flowchartYesNoYesNoStartmark = 72mark >= 70?Output"Distinction"mark >= 40?Output "Pass"Output "Fail"Stop
  • Sequence: statements run one after another, in order.
  • Selection: a condition that is true or false decides which block of code runs.
  • Compare with <, >, <=, >=, equal to = and not equal to <>.
  • Join conditions with AND, OR, NOT.
Cambridge pseudocode
DECLARE Mark : INTEGER
Mark ← 72
IF Mark >= 70
  THEN
    OUTPUT "Distinction"
  ELSE
    IF Mark >= 40
      THEN
        OUTPUT "Pass"
      ELSE
        OUTPUT "Fail"
    ENDIF
ENDIF
Quick check
?What is output when this code runs?
Cambridge pseudocode
DECLARE Temp : INTEGER
DECLARE Wind : INTEGER
Temp ← 18
Wind ← 35
IF Temp > 15 AND NOT (Wind > 30)
  THEN
    OUTPUT "Picnic"
  ELSE
    OUTPUT "Stay in"
ENDIF
Programming · control flow

CASE statements

  • A CASE statement picks one branch by comparing one variable with a list of values.
  • Cases are checked in order; the first match runs, then the statement ends.
  • OTHERWISE runs if no case matches.
  • It is neater than a long chain of IFs when you test one variable for exact values.
Cambridge pseudocode
DECLARE Day : STRING
INPUT Day
CASE OF Day
  "Sat" : OUTPUT "Weekend"
  "Sun" : OUTPUT "Weekend"
  OTHERWISE OUTPUT "Weekday"
ENDCASE
Programming · control flow

Count-controlled loops

  • Iteration repeats a block of code.
  • A count-controlled loop repeats a fixed number of times. A counter variable goes up by 1 each time unless a step is given.
Cambridge pseudocode
DECLARE Count : INTEGER
FOR Count ← 1 TO 5
    OUTPUT Count * 3
NEXT Count

All output 3, 6, 9, 12, 15.

Programming · control flow

Condition-controlled loops

  • A condition-controlled loop repeats until a condition changes, so you need not know the number of passes.
  • A pre-condition (WHILE) loop tests the condition at the start: it may run zero times.
  • A post-condition loop, REPEAT … UNTIL, tests at the end, so it always runs at least once.
Cambridge pseudocode
DECLARE Total : INTEGER
Total ← 1
WHILE Total < 50 DO
    Total ← Total * 2
ENDWHILE
OUTPUT Total

All output 64.

Your answer
?What is output when this code runs?
Cambridge pseudocode
DECLARE N : INTEGER
N ← 3
WHILE N < 40 DO
    N ← N * 2
ENDWHILE
OUTPUT N
Programming · control flow

Nesting

Nested loopsrowcoloutput111122133212224236313326339inner loop:3 passesinner loop:3 passesinner loop:3 passesouter loop:3 passes
  • Nesting puts one construct inside another: an IF inside a loop, a loop inside an IF, or a loop inside a loop.
  • With nested loops, the inner loop runs in full for every pass of the outer loop.
  • Total passes of the inner block = outer passes × inner passes.
  • You write at most three levels of nesting.
Cambridge pseudocode
DECLARE Row : INTEGER
DECLARE Col : INTEGER
FOR Row ← 1 TO 3
    FOR Col ← 1 TO 3
        OUTPUT Row * Col
    NEXT Col
NEXT Row

Output: 1, 2, 3, 2, 4, 6, 3, 6, 9 (9 values).

Calculate
?How many values does this code output?
Cambridge pseudocode
DECLARE I : INTEGER
DECLARE J : INTEGER
FOR I ← 1 TO 4
    FOR J ← 1 TO 6
        OUTPUT I + J
    NEXT J
NEXT I
Mission check

?How can a few lines of code print all 100 times-table answers?
Mission 3

How can one name hold all 9 squares of a noughts and crosses board?

Make a guess: How can one name hold all 9 squares of a noughts and crosses board?

Programming · data structures

Arrays

An arrayscores12071923441520indexOUTPUT scores[1]
  • An array is a data structure: it stores many values under one identifier. Each value is an element.
  • All elements have the same data type, and the array has a fixed length (static).
  • An index picks out one element. Bounds are declared, e.g. ARRAY[1:5]; the first index is usually 1.
Cambridge pseudocode
DECLARE Scores : ARRAY[1:5] OF INTEGER
Scores[1] ← 12
Scores[2] ← 7
Scores[3] ← 9
Scores[4] ← 15
Scores[5] ← 4
OUTPUT Scores[2]      // 7
Scores[4] ← 20        // 15 becomes 20
Programming · data structures

Looping through an array

  • Use a variable as the index in a count-controlled loop to visit every element in turn.
  • The counter runs from the lower bound to the upper bound.
Cambridge pseudocode
DECLARE Temps : ARRAY[1:5] OF INTEGER
DECLARE Total : INTEGER
DECLARE Index : INTEGER
// Temps holds 18, 21, 17, 22, 19
Total ← 0
FOR Index ← 1 TO 5
    Total ← Total + Temps[Index]
NEXT Index
OUTPUT Total

All output 97.

Your answer
?What is output when this code runs?
Cambridge pseudocode
DECLARE Nums : ARRAY[1:5] OF INTEGER
DECLARE Index : INTEGER
FOR Index ← 1 TO 5
    Nums[Index] ← Index * 3
NEXT Index
OUTPUT Nums[2] + Nums[4]
Programming · data structures

2D arrays

A 2D arraycolumnrow00121X2Oboard[1,1]
  • A two-dimensional (2D) array is like a table: one identifier, with rows and columns.
  • Each element needs two indexes: Board[Row, Column] (row first here).
  • A noughts and crosses board is a 3 × 3 2D array of characters.
Cambridge pseudocode
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
Board[2,2] ← 'X'     // centre square
Board[3,1] ← 'O'     // bottom-left square
Quick check
?Marks is declared as ARRAY[1:3, 1:4] OF INTEGER and holds the values in this table (row first). What is Marks[2,3]?
5829
7163
410110
Your answer
?Nested loops visit every element of a 2D array: the outer loop picks a row, the inner loop each column in that row. What is output?
Cambridge pseudocode
DECLARE Grid : ARRAY[1:3, 1:4] OF INTEGER
DECLARE Row : INTEGER
DECLARE Column : INTEGER
FOR Row ← 1 TO 3
    FOR Column ← 1 TO 4
        Grid[Row, Column] ← Row * 10 + Column
    NEXT Column
NEXT Row
OUTPUT Grid[2, 3] + Grid[3, 1]
Mission check

?How can one name hold all 9 squares of a noughts and crosses board?
Mission 4

Two subroutines both use a variable called total. Why no clash?

Make a guess: Two subroutines both use a variable called total. Why no clash?

Programming · subroutines

Subroutines

Calling a proceduremain programwelcome()welcome()end of programprocedurewelcomeshows thewelcome message1234
  • A subroutine is a named, out-of-line block of code. It runs when you call it by its name, then control returns to the line after the call.
  • A procedure carries out a task. A function carries out a task and returns a value.
Cambridge pseudocode
PROCEDURE Welcome
    OUTPUT "Welcome to the quiz"
ENDPROCEDURE

CALL Welcome
CALL Welcome
Programming · subroutines

Parameters and return values

  • A parameter passes data into a subroutine when it is called. Values are matched to parameters in order.
  • A function sends its result back with return. The returned value replaces the call, so store it or use it straight away.
  • Give each parameter a data type, and the function a RETURNS type. There are up to three parameters. Procedures are called with CALL; functions are used inside an expression.
Cambridge pseudocode
FUNCTION Area(Width : INTEGER, Height : INTEGER) RETURNS INTEGER
    DECLARE Result : INTEGER
    Result ← Width * Height
    RETURN Result
ENDFUNCTION

DECLARE RoomArea : INTEGER
RoomArea ← Area(4, 5)
OUTPUT RoomArea    // 20
Your answer
?What is output when this code runs?
Cambridge pseudocode
FUNCTION Change(Paid : INTEGER, Price : INTEGER) RETURNS INTEGER
    RETURN Paid - Price
ENDFUNCTION

OUTPUT Change(20, 13) * 2
Programming · subroutines

Local and global variables

Local and globalmain programaddBonusnewScore15localbonus5globalreadable insideaddBonusnewScore?local: exists only while addBonus runs
  • A local variable is declared inside a subroutine: it only exists while that subroutine runs and is only usable inside it.
  • A global variable is declared in the main program and can be used anywhere.
  • Good practice: nothing outside can change a local variable, and its name can be reused elsewhere.
  • A variable DECLAREd inside a subroutine is local; one declared in the main program is global.
Cambridge pseudocode
FUNCTION AddBonus(Score : INTEGER) RETURNS INTEGER
    DECLARE NewScore : INTEGER    // local
    NewScore ← Score + Bonus
    RETURN NewScore
ENDFUNCTION

DECLARE Bonus : INTEGER           // global
Bonus ← 5
OUTPUT AddBonus(10)               // 15
OUTPUT NewScore                   // error: NewScore does not exist here
Quick check
?A subroutine calcTax declares a local variable tax and returns its value. After calling calcTax, the main program tries to output tax. What happens?
Programming · subroutines

Why use subroutines?

  • Reuse: write code once and call it many times, so there is less to write and check.
  • Decomposition: each subroutine solves one part and can be written and tested on its own.
  • Maintenance: fix or improve the code in one place.
  • Library routines are ready-made subroutines, e.g. ROUND, RANDOM, DIV, MOD.
Quick check
?Which task should be written as a function rather than a procedure?
Mission check

?Two subroutines both use a variable called total. Why no clash?
Mission 5

How does an app show only the last 4 digits of your card number?

Make a guess: How does an app show only the last 4 digits of your card number?

Programming · strings, files and random numbers

Strings have positions

String positionscard40912293142536475869710811912013114215length = 16
  • A string is a sequence of characters. Each character has a position, its index.
  • Cambridge usually counts the first character as position 1, but a question may use 0, so check.
  • The length of a string is how many characters it holds, spaces included.
  • A card number is stored as a string, not an integer: it is never used in arithmetic.
Cambridge pseudocode
DECLARE Card : STRING
Card ← "4929123456789012"
OUTPUT LENGTH(Card)
// outputs 16
Programming · strings, files and random numbers

Taking a substring

Taking a substringcard40912293142536475869710811912013114215****9012start index = 16 − 4 = 12result
  • A substring is part of a string, picked out by position.
  • SUBSTRING(string, start, length) takes length characters from position start.
  • Using the length makes the code work for any card length.
Cambridge pseudocode
DECLARE Card : STRING
DECLARE Last4 : STRING
Card ← "4929123456789012"
Last4 ← SUBSTRING(Card, LENGTH(Card) - 3, 4)
OUTPUT "****", Last4
Your answer
?Trace this code. What is output?
Cambridge pseudocode
DECLARE Word : STRING
Word ← "computer"
OUTPUT SUBSTRING(Word, 4, 3)
Programming · strings, files and random numbers

More string tools

  • UCASE("Happy") gives "HAPPY"; LCASE('W') gives 'w'.
  • Use these to compare text whatever case it was typed in.
Programming · strings, files and random numbers

Random numbers

  • Games, quizzes and simulations need unpredictable values, such as a dice roll.
  • RANDOM() gives a number from 0 to 1, so multiply, round and add to get the range you need.
Cambridge pseudocode
DECLARE Dice : INTEGER
Dice ← ROUND(RANDOM() * 5, 0) + 1
OUTPUT Dice
Calculate
?How many different values can this code output?
Cambridge pseudocode
OUTPUT ROUND(RANDOM() * 4, 0) + 5
Programming · strings, files and random numbers

Reading and writing files

Filesprogramscores.txtAnn,35Ben,28Zara,42openwritereadcloseWhen the program endsRAMline–variable emptiedscores.txt3 lines keptfile keeps its contents
  • Variables are lost when a program ends. A file in secondary storage keeps data, such as high scores, for next time.
  • Open the file, read or write it, then close it so the data is saved and the file is freed.
  • Open a file FOR READ or FOR WRITE; WRITE creates a new file, so any existing data is lost.
Cambridge pseudocode
DECLARE Line : STRING
Line ← "Zara,42"
OPENFILE "Scores.txt" FOR WRITE
WRITEFILE "Scores.txt", Line
CLOSEFILE "Scores.txt"
OPENFILE "Scores.txt" FOR READ
READFILE "Scores.txt", Line
OUTPUT Line
CLOSEFILE "Scores.txt"
Quick check
?A game keeps its high-score table in a variable. Every time the game closes, the scores vanish. What fixes this?
Mission check

?How does an app show only the last 4 digits of your card number?
Recap

Big ideas

  • A variable is a named memory location whose value can change; a constant keeps one value while the program runs.
  • Choose the data type that fits the data: integer, real, Boolean, character or string.
  • Integer division gives the whole-number part; the remainder (modulus) gives what is left over.
  • Sequence runs statements in order; selection uses a true/false condition to choose a branch (IF or CASE).
  • Count-controlled loops repeat a fixed number of times; condition-controlled loops repeat until a condition changes, tested at the start or the end.
  • In nested loops the inner loop runs fully for every pass of the outer loop.
  • An array holds many values of one data type under one identifier; an index picks out an element (from the declared lower bound, usually 1).
  • A 2D array is a table of rows and columns, so each element needs two indexes.
  • A subroutine is a named block of code called by its name; a function returns a value and a procedure does not.
  • Parameters pass data in, in order; return passes a result back to the call.
  • Local variables exist only inside their subroutine while it runs, so the same name can be reused safely; global variables can be used anywhere.
  • A string is a sequence of characters, each with an index; the length is how many characters it has. Positions usually start at 1.
  • A substring picks out part of a string by position.
  • Files keep data after a program ends: open, read or write, close.
  • Random number functions give a value in a range, including both ends.
🏆

Mini-lesson complete!

⭐⭐⭐

You've worked through Programming for Cambridge IGCSE Computer Science (0478/0984). 🎉

  1. Mission 1How does a game turn 135 seconds into 2:15 on its timer?
  2. Mission 2How can a few lines of code print all 100 times-table answers?
  3. Mission 3How can one name hold all 9 squares of a noughts and crosses board?
  4. Mission 4Two subroutines both use a variable called total. Why no clash?
  5. Mission 5How does an app show only the last 4 digits of your card number?

Your stars: 0 / 0

Next: test yourself in the Verify stage.

📣 Smashed it? Share your score

Challenge a mate to beat your stars, or show a parent how you got on.

→ Back to IGCSE Computer Science · Cambridge

Specification points and sources for this lesson are listed at the bottom of the page.

Sources (5)

The specification points and sources behind this lesson.

  1. 1 Cambridge IGCSE 0478 syllabus 2026–2028 (v6), 8.1.1–8.1.4(f), 8.1.7; section 4 pseudocode
  2. 2 Cambridge IGCSE 0478 syllabus 2026–2028 (v6), 8.1.4(a)–(c), (f), 8.1.5; section 4 pseudocode
  3. 3 Cambridge IGCSE 0478 syllabus 2026–2028 (v6), 8.2.1–8.2.3; section 4 Arrays
  4. 4 Cambridge IGCSE 0478 syllabus 2026–2028 (v6), 8.1.6(a)–(c), 8.1.7; section 4 Procedures and functions
  5. 5 Cambridge 0478 syllabus 2026–2028, 8.1.4(e), 8.1.7, 8.3; section 4 pseudocode (string operations, ROUND/RANDOM, file handling)