5. Common Operations
Docs / Pseudocode / Common Operations
CAIE 0478 & 9618 Common Operations

5. Common Operations & Built-in Functions

Cambridge pseudocode I/O routines, arithmetic (DIV/MOD), relational operators, Boolean logic, and unified string & numeric built-in functions.

Standard input/output commands, arithmetic operators, relational and Boolean logic, and unified string & numeric built-in functions.

5.1 Input & Output

PSEUDOCODE Run in Sandbox
// runnable
// Input and output demo
DECLARE Answer, Score, Lives : INTEGER
Score <- 10
Lives <- 3
INPUT Answer
OUTPUT "Your answer: ", Answer
OUTPUT Score
OUTPUT "You have ", Lives, " lives left"

5.2 Arithmetic Operators

Operator

Description

+

Addition

-

Subtraction

*

Multiplication

/

Division (result is always REAL)

DIV

Integer division (quotient only)

MOD

Modulus (remainder after division)

5.3 Relational Operators

Operator

Description

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

=

Equal to

<>

Not equal to

The result of relational operations is always of data type BOOLEAN.

5.4 Logic Operators

The logic operators are AND, OR, and NOT. Operands and results are always BOOLEAN. Use parentheses to make order of operations explicit.

5.5 String Functions & Operations

Function Signature

Description

Example

Syllabus

LENGTH(s : STRING) RETURNS INTEGER

Returns string length

LENGTH("Happy Days") -> 10

Both

MID(s : STRING, x : INTEGER, y : INTEGER) RETURNS STRING

Returns y chars from position x

MID("ABCDEFGH", 2, 3) -> "BCD"

9618

RIGHT(s : STRING, x : INTEGER) RETURNS STRING

Returns rightmost x chars

RIGHT("ABCDEFGH", 3) -> "FGH"

9618

LEFT(s : STRING, x : INTEGER) RETURNS STRING

Returns leftmost x chars

LEFT("CAMBRIDGE", 3) -> "CAM"

0478

SUBSTRING(s : STRING, x : INTEGER, y : INTEGER) RETURNS STRING

Extracts y chars from position x

SUBSTRING("HELLO", 2, 3) -> "ELL"

0478

LCASE(c : CHAR) RETURNS CHAR

Returns lower-case equivalent

LCASE('W') -> 'w'

Both

UCASE(c : CHAR) RETURNS CHAR

Returns upper-case equivalent

UCASE('h') -> 'H'

Both

& (concatenation)

Joins two strings

"Summer" & " Pudding" -> "Summer Pudding"

Both

5.6 Numeric Functions

Function Signature

Description

Example

Syllabus

INT(x : REAL) RETURNS INTEGER

Returns integer part of x

INT(27.5415) -> 27

Both

RAND(x : INTEGER) RETURNS REAL

Random real in range 0 to x (exclusive)

RAND(87) may return 35.43

9618

NUM_TO_STR(x) RETURNS STRING

Converts number to string

NUM_TO_STR(42) -> "42"

0478

STR_TO_NUM(s : STRING) RETURNS NUMERIC

Parses string to number

STR_TO_NUM("42") -> 42

0478

ROUND(x : REAL, places : INTEGER) RETURNS REAL

Rounds to the given number of decimal places

ROUND(7.246, 2) -> 7.25

0478

RANDOM() RETURNS REAL

Random real between 0 and 1

RANDOM() may return 0.64

0478