Programming Concepts
The building blocks every program is made of: data types, the three structures, and named blocks of code.
WATCH IT EXPLAINED
EXPLORE IT IN 3D
The program workshop
A workshop bench where the lesson's programs run on real things: pack typed crates, throw railway points for IF and CASE, send a train round a loop, split numbers with DIV and MOD — and watch a function send its answer down a chute.
Five data types, and how to write each one
Every variable has a data type, and the way you write a value shows its type.
You give a variable its data type when you declare it: DECLARE Name : TYPE.
| Type | What it holds | Written like |
|---|---|---|
| INTEGER | a whole number | 5 or -3 |
| REAL | a number that can have a fractional part | 4.7 or 0.5, never .5 |
| CHAR | exactly one character, in single quotes | 'A' |
| STRING | any number of characters, in double quotes, even none | "Bat" or "" |
| BOOLEAN | TRUE or FALSE | TRUE |
Watch out:A REAL always has a digit on each side of the point. And the quotes decide the type: "42" is a STRING, so you cannot do sums with it.
DECLARE Score : INTEGER DECLARE Height : REAL DECLARE Grade : CHAR DECLARE Name : STRING DECLARE Passed : BOOLEAN Score ← -3 Height ← 1.65 Grade ← 'A' Name ← "Bat" Passed ← TRUE
Variables, constants, input and output
A variable can change while the program runs; a constant is set once and never changes.
| Variable | Constant | |
|---|---|---|
| What it is | a named store | a named value |
| Can the program change it? | yes, while the program runs | never |
| Written | DECLARE Total : INTEGER | CONSTANT Name ← value |
A constant's value must be a literal (a value written out directly), such as 0.10.
Why use a constant? The program is easier to read. And if the value must change, you update only that one line.
INPUTstores what the user types in a variable.OUTPUTshows values. Separate several values with commas.
The statements run one after another, from top to bottom. That is sequence.
Selection: IF, and IF inside IF
IF chooses one of two paths, and an IF can sit inside another IF.
IF condition THEN … ELSE … ENDIF runs one of two blocks. You can leave out the ELSE part.
In exam pseudocode, THEN and ELSE go on their own lines, indented two spaces.
A nested IF is an IF inside the THEN or the ELSE of another IF. Indent each level further.
Only the lines on the chosen path run. The others are skipped.
Exam:A question will not ask you to write more than three levels of nesting.
Selection: CASE
CASE compares one variable with a list of single values, and runs the first match only.
CASE OF Variable compares one variable with a list of single values.
- The cases are tested in order.
- The first case that matches runs its statement.
- The rest are not tested at all. The program carries on after
ENDCASE. - If no case matched,
OTHERWISEruns. It must come last.
| Use CASE when | Use IF when |
|---|---|
| one variable is compared with fixed values | you test a range, such as Age < 12, or the condition needs more than one variable |
Conditions: comparisons joined by AND, OR and NOT
Comparisons give TRUE or FALSE, and AND, OR and NOT join them into bigger conditions.
A comparison (=, <, <=, >, >= or <>) always gives TRUE or FALSE.
| Operator | It is TRUE when |
|---|---|
| AND | both sides are TRUE |
| OR | at least one side is TRUE |
| NOT | it turns TRUE into FALSE, and FALSE into TRUE |
Example:Is a mark inside the range? Use AND: Mark >= 0 AND Mark <= 100. Is it outside? Use OR: Mark < 0 OR Mark > 100.
In a long condition, put brackets round each part. This makes the order clear.
DECLARE Mark : INTEGER INPUT Mark // inside the range: both must be TRUE IF Mark >= 0 AND Mark <= 100 THEN OUTPUT "inside the range" ENDIF // outside the range: one is enough IF Mark < 0 OR Mark > 100 THEN OUTPUT "outside the range" ENDIF
Iteration: three kinds of loop
FOR repeats a fixed number of times; WHILE tests first; REPEAT tests after, so it runs at least once.
| Loop | Kind | How it tests | Fewest passes |
|---|---|---|---|
FOR … NEXT | count-controlled | runs a fixed number of times | zero: FOR Count ← 1 TO 0 |
WHILE … DO … ENDWHILE | pre-condition | before every pass; keeps going while TRUE | zero, if FALSE at the first test |
REPEAT … UNTIL | post-condition | after each pass; stops when TRUE | one: it always runs at least once |
STEP -1makes a FOR loop count down.- The REPEAT condition is the stopping condition.
- A loop inside a loop is nested iteration.
Totalling and counting use loops: see Pseudocode and Trace Tables.
DECLARE Count : INTEGER DECLARE Answer : STRING FOR Count ← 1 TO 3 OUTPUT Count NEXT Count Answer ← "" WHILE Answer <> "y" DO OUTPUT "Stop? (y/n)" INPUT Answer ENDWHILE // Answer is already "y", but REPEAT still asks once REPEAT OUTPUT "Stop? (y/n)" INPUT Answer UNTIL Answer = "y"
Arithmetic: /, DIV, MOD and ^
/ gives the full answer, DIV the whole part, MOD the remainder, and ^ a power.
| Operation | Gives | Example |
|---|---|---|
/ | the full answer, fraction and all | 17 / 5 = 3.4 |
DIV(17, 5) | the whole-number part; the fraction is thrown away | 3 |
MOD(17, 5) | the remainder | 2 |
^ | a power | 2 ^ 3 = 8 |
Multiplication and division come before addition and subtraction. So 3 + 4 * 2 = 11. Use brackets to change the order.
Useful tricks:
MOD(N, 2) = 0means N is even.MOD(N, 10)is the last digit, andDIV(N, 10)drops it.
Exam:At IGCSE, write DIV and MOD as functions.
DECLARE Minutes : INTEGER DECLARE Hours : INTEGER Minutes ← 135 Hours ← DIV(Minutes, 60) OUTPUT Hours, " hours ", MOD(Minutes, 60), " minutes" IF MOD(Minutes, 2) = 0 THEN OUTPUT "even" ELSE OUTPUT "odd" ENDIF
Working with strings
Four routines handle text: LENGTH, SUBSTRING, UCASE and LCASE.
| Routine | Gives |
|---|---|
LENGTH(Text) | the number of characters |
SUBSTRING(Text, Start, Length) | the part of Text that is Length characters long, beginning at position Start |
UCASE(Text) | the text in capital letters |
LCASE(Text) | the text in small letters |
Watch out:The third value of SUBSTRING is a length, not an end position.
The first character can be position 0 or 1. The guide, and this lesson, count from 1. If you count from 0, say so.
DECLARE FullText : STRING DECLARE PartText : STRING FullText ← "IGCSE Computer Science at Cambridge" PartText ← SUBSTRING(FullText, 7, 16) OUTPUT PartText OUTPUT LENGTH(FullText)
Procedures and functions
A procedure does a job; a function does a job and returns one value.
| Procedure | Function | |
|---|---|---|
| What it is | a named block of code that does a job | does a job and returns one value |
| Header | PROCEDURE Name(Param : TYPE) | FUNCTION Name(Param : TYPE) RETURNS TYPE |
| Inside | the code, then ENDPROCEDURE | RETURN sends the value back |
| How to use it | CALL Name(Value) | inside an expression, such as OUTPUT Area(4, 3); never with CALL |
When a procedure ends, the program carries on from the line after the CALL.
A parameter is a name in the header. It receives a value at each call.
Exam:An exam subroutine has at most three parameters. Define subroutines at the start of the program.
Local and global variables
A global variable works everywhere; a local variable works only inside its own subroutine.
| Global variable | Local variable | |
|---|---|---|
| Declared | in the main program | inside a procedure or function |
| Can be used | anywhere, inside procedures and functions too | only inside that subroutine |
What if a local has the same name as a global? The subroutine uses its own local. It leaves the global alone.
Remember:Code outside a subroutine cannot use its local, so it cannot change the local by accident.
Library routines: ROUND and RANDOM
A library routine is ready-made code you use without writing it, like ROUND and RANDOM.
A library routine is ready-made: you use it without writing it.
ROUND(Value, Places)rounds to that many decimal places. SoROUND(3.14159, 2)is 3.14.RANDOM()gives a random number from 0 to 1, inclusive.
To get a whole number in a range, multiply, round, then add:
| Code | Gives |
|---|---|
ROUND(RANDOM() * 5, 0) + 1 | 1 to 6 |
ROUND(RANDOM() * 6, 0), the guide's own example | 0 to 6, not 1 to 6 |
ROUND(RANDOM() * 9, 0) + 1, from a 2025 mark scheme | 1 to 10 |
DECLARE Dice : INTEGER OUTPUT ROUND(3.14159, 2) OUTPUT ROUND(7 / 3, 1) // a whole number from 1 to 6 Dice ← ROUND(RANDOM() * 5, 0) + 1 OUTPUT "You rolled ", Dice
Writing a program someone else can maintain
Write programs that someone else can read later and change safely.
A program is read again later, often by someone else. So make it easy to maintain (to fix and change later):
- Use meaningful identifiers (names) for variables, constants, arrays, procedures and functions.
- Add comments, starting with
//, that say what each part is for. - Put jobs you repeat into procedures and functions.
- Use constants instead of unexplained numbers.
- Indent every block.
Example:The two programs below do the same job. They print the same answer for the same inputs. Only the second one tells a reader what it is doing.
// BEFORE INPUT x INPUT y z ← x * y * 0.1 OUTPUT z // AFTER // Works out the VAT on an order CONSTANT VatRate ← 0.1 DECLARE Quantity : INTEGER DECLARE UnitPrice : REAL DECLARE Vat : REAL OUTPUT "Enter the quantity" INPUT Quantity OUTPUT "Enter the price of one item" INPUT UnitPrice Vat ← Quantity * UnitPrice * VatRate OUTPUT "VAT to pay: ", Vat
RUN THE PSEUDOCODE
Write the pseudocode your syllabus uses and press Run. Assignment is the arrow — type <- and it works the same as ←. Both syllabuses are accepted, so OUTPUT and PRINT both work; where a word differs from the one the exam prints, the runner says so.
Press Run to see what it doesTotal ← 0
FOR Count ← 1 TO N
Total ← Total + Count
NEXT Count
OUTPUT TotalSTARTN = 5
| Count | Total | OUTPUT |
|---|---|---|
One row per pass through the loop. Leave a cell blank if it does not change on that pass.
KEY TERMS
CHECK YOURSELF
1.What does SUBSTRING("Ulaanbaatar", 6, 6) give?
2.Which of these is a CHAR literal?
3.What does MOD(17, 5) give?
4.A mark must be from 0 to 100. Which condition is TRUE only for a valid mark?
5.Which loop always runs its body at least once?