Docs

Topic 8 · Programming

Operators, library routines, and strings

Arithmetic operators, MOD and DIV, ROUND and RANDOM, and string handling with LENGTH, SUBSTRING, UCASE, and LCASE.

Lesson 4 of 7Syllabus 8.1updated October 31, 2026

Arithmetic operators

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
^raise to the power7 ^ 249
MOD(7, 2)remainder after divisionMOD(7, 2)1
DIV(7, 2)whole number divisionDIV(7, 2)3

MOD and DIV

DIV gives how many whole times one number goes into another, and MOD gives what is left over. Together they split a number apart.

A common use: a number is even when MOD(Number, 2) = 0. Another: turning minutes into hours and minutes.

Pseudocode
DECLARE TotalMinutes : INTEGER
INPUT TotalMinutes
OUTPUT DIV(TotalMinutes, 60), " hours and ", MOD(TotalMinutes, 60), " minutes"

ROUND and RANDOM

ROUND(Value, Places) rounds a real number to a number of decimal places. RANDOM() returns a random number from 0 to 1, which you can scale up.

Pseudocode
DECLARE Price : REAL
Price <- 19.987
OUTPUT ROUND(Price, 2)
OUTPUT "Random whole number from 0 to 100: ", ROUND(RANDOM() * 100, 0)

Strings are numbered characters

A string is a sequence of characters. In Cambridge pseudocode the first character is at position 1. Click a character to see its position.

Positions in "COMPUTER"Interactive
12345678
SUBSTRING(Word, 4, 1) = P
Click a cell to see its index.

String handling

RoutineReturnsExample with "Computer"Result
LENGTH(S)The number of charactersLENGTH(Word)8
SUBSTRING(S, Start, Length)Part of the stringSUBSTRING(Word, 4, 3)"put"
UCASE(S)The string in upper caseUCASE(Word)"COMPUTER"
LCASE(S)The string in lower caseLCASE(Word)"computer"
Pseudocode
DECLARE FirstName : STRING
DECLARE LastName : STRING
DECLARE Username : STRING
INPUT FirstName
INPUT LastName
Username <- LCASE(SUBSTRING(FirstName, 1, 1)) & LCASE(LastName)
OUTPUT "Your username is ", Username
OUTPUT "It has ", LENGTH(Username), " characters"

Exam tip. & joins (concatenates) two strings: "Ada" & "Lovelace" gives "AdaLovelace".

Key terms

MOD
Gives the remainder after integer division.
DIV
Gives the whole-number result of a division.
Library routine
A ready-made subroutine provided by the language, such as ROUND or RANDOM.
Concatenation
Joining strings together.

Check yourself

What are MOD(23, 5) and DIV(23, 5)?Show answer

MOD = 3, DIV = 4 (23 = 4 × 5 + 3).

What is SUBSTRING("Keyboard", 4, 5)?Show answer

"board".

Write a condition that is TRUE when N is a multiple of 3.Show answer

MOD(N, 3) = 0.