?How does a game turn 135 seconds into 2:15 on its timer?
The timer keeps the time as one integer number of seconds. Integer division by 60 (DIV) gives the whole minutes, 2, and the remainder (MOD) gives the seconds left over, 15. Real division would give 2.25, which is not 2 minutes 25 seconds.
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
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
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?
The program uses nested count-controlled loops. The outer loop counts the rows 1 to 10, and for every row the inner loop counts the columns 1 to 10 and outputs row × column. That gives 10 × 10 = 100 answers from a handful of lines.
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 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.
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 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]?
5
8
2
9
7
1
6
3
4
10
11
0
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?
Store the board as a 2D array: one identifier, 3 rows × 3 columns, so 9 elements of the same data type. Each square is reached with two indexes, row then column, counting from the lower bound, usually 1.
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
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
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?
Each total is a local variable. It is created when its subroutine runs, can only be used inside that subroutine, and disappears when the subroutine ends. So the two variables never meet, which is why using local variables is good practice.
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
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.
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?
The card number is stored as a string. The program finds its length, works out that the last four characters start at length − 4 when counting from 0 (length − 3 when counting from 1), and takes that substring to show after the stars.
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). 🎉
Mission 1How does a game turn 135 seconds into 2:15 on its timer?
Mission 2How can a few lines of code print all 100 times-table answers?
Mission 3How can one name hold all 9 squares of a noughts and crosses board?
Mission 4Two subroutines both use a variable called total. Why no clash?
Mission 5How does an app show only the last 4 digits of your card number?