Library Routines & Operators
Watch this lesson
Video 8.1.3 ·
7:00 · The operators you compute with, and the routines you are given for free
1. The Three Categories of Operators
A. Arithmetic Operators
These are used to perform mathematical calculations on numerical data types (Integer and Real).
| Operator | Function | Example |
|---|---|---|
| + | Addition | 10 + 5 = 15 |
| - | Subtraction | 10 - 5 = 5 |
| * | Multiplication | 10 * 5 = 50 |
| / | Division | 10 / 4 = 2.5 |
| DIV | Integer Division (Quotient only) | 10 DIV 4 = 2 |
| MOD | Modulo (Remainder only) | 10 MOD 4 = 2 |
| ^ | Raised to the power of | 2 ^ 3 = 8 |
B. Relational (Comparison) Operators
These compare two values and return a Boolean result (TRUE or FALSE). Used in IF statements and Loops.
| Operator | Function | Example |
|---|---|---|
| = | Equal to | X = 10 |
| <> | Not equal to | X <> 10 |
| > | Greater than | X > 10 |
| < | Less than | X < 10 |
| >= | Greater than or equal to | X >= 10 |
| <= | Less than or equal to | X <= 10 |
C. Boolean (Logical) Operators
Used to combine multiple conditions together to form complex logic.
| Operator | Function | Example |
|---|---|---|
| AND | True only if BOTH are true | (X > 5) AND (X < 10) |
| OR | True if AT LEAST ONE is true | (Ans = 'Y') OR (Ans = 'y') |
| NOT | Reverses the Boolean value | NOT (X = 10) |
2. Official Library Routines (String Handling)
The following are the only string functions recognized in the Cambridge marking schemes:
| Routine | Function |
|---|---|
| LENGTH(String) | Returns the length of the string as an integer. |
| LCASE(String) | Converts all characters to lowercase. |
| UCASE(String) | Converts all characters to uppercase. |
| SUBSTRING(s, start, length) | Extracts a portion of string s starting at start for length. |
| LEFT(String, n) | Returns the first n characters of the string. |
| RIGHT(String, n) | Returns the last n characters of the string. |
// Example usage in an algorithm
DECLARE MyText : STRING
MyText ← "Cambridge"
OUTPUT LENGTH(MyText) // Result: 9
OUTPUT SUBSTRING(MyText, 1, 3) // Result: "Cam"
3. Numeric & Random Routines
| Routine | Function |
|---|---|
| INT(n) | Returns the integer part of value n. |
| ROUND(n, d) | Rounds n to d decimal places. |
| RANDOM() | Returns a random real number between 0 and 1 inclusive. |
📝 Exam Note (Very Important):
In Cambridge pseudocode, the Assignment Operator is an arrow: ←.
Using a single equals sign (=) for assignment is a common mistake; it should only be used for comparison.