8.1 Programming concepts

Test yourself on Programming concepts 14 questions — drag-to-order, code completion, matching and multiple choice.
Start the quiz

Declarations and Data Types

Watch this lesson Video 8.1.1 · 7:16 · The five data types, variables against constants, and why declaring things matters

1. Data Types

Every piece of data must have a type. This tells the computer how much memory to allocate and what operations are possible.

INTEGER: Whole numbers (e.g., 10, -5).
REAL: Numbers with decimals (e.g., 15.50, -0.5).
CHAR: A single character (e.g., 'A', '$').
STRING: Text (e.g., "Hello World").
BOOLEAN: Logic values (TRUE or FALSE).

2. Variables, Constants, and Assignment

  • Variable: A named memory location where the value can change during program execution.
  • Constant: A value that remains fixed throughout the program (e.g., PI). A constant is given its value with the same arrow as a variable — , never =. (A Level 9618 writes CONSTANT Discount = 0.10; this syllabus does not.)
DECLARE StudentName : STRING
DECLARE StudentAge : INTEGER
CONSTANT Discount ← 0.10

// Use the assignment arrow ← (not =)
StudentName ← "John Doe"
StudentAge ← 16

3. Arrays (1D and 2D)

Arrays store multiple items of the same data type under one name using an index.

// 1D Array: List of 10 student names
DECLARE Names : ARRAY[1:10] OF STRING
Names[1] ← "Alice"

// 2D Array: Grid (e.g., 3 students, 4 marks each)
DECLARE Marks : ARRAY[1:3, 1:4] OF INTEGER
Marks[1, 1] ← 85

4. Operators

Type Operators Notes
Arithmetic + , - , * , / Standard math operations.
Comparison = , <> , < , > , <= , >= Note: <> means "Not Equal To".
Logical AND , OR , NOT Used to combine Boolean conditions.
Integer Math DIV , MOD DIV is whole quotient; MOD is remainder.

5. Library Routines

Common built-in functions you are expected to use in IGCSE pseudocode:

  • LENGTH(String): Returns the number of characters.
  • SUBSTRING(String, Start, Length): Extracts a part of a string.
  • ROUND(Number, Decimals): Rounds to specified decimal places.
  • UPPER(String) / LOWER(String): Changes text case.

6. Procedures and Functions (Subroutines)

A procedure performs a task. A function performs a task and returns a value.

// Procedure with 2 parameters
PROCEDURE CalculateArea(Length : REAL, Width : REAL)
  OUTPUT Length * Width
ENDPROCEDURE

// Function with 2 parameters
FUNCTION FindMax(Num1 : INTEGER, Num2 : INTEGER) RETURNS INTEGER
  IF Num1 > Num2 THEN
    RETURN Num1
  ELSE
    RETURN Num2
  ENDIF
ENDFUNCTION
⚠️ Exam Note: In pseudocode, always use the Left Arrow for assignment. Do not use = for assignment; = is only used for comparing values (e.g., in an IF statement).

Basic Constructs

Watch this lesson Video 8.1.2 · 7:06 · Sequence, selection and iteration — and which loop to reach for

0. The Three Basic Constructs

Every program ever written, in any language, is built from just three control structures. The syllabus names all three, and this lesson covers the second and third in detail — but the first is the one students forget to mention, precisely because it seems too obvious to be worth a name.

  • Sequence — instructions are carried out one after another, in the order they are written. No decisions, no repetition. This is the default: unless something tells it otherwise, a program runs straight down the page.
  • Selection — a decision is made, and different instructions run depending on the outcome (IF, CASE).
  • Iteration — a set of instructions is repeated (FOR, WHILE, REPEAT).
Why sequence deserves a name. Because order changes meaning. Reading a value before using it in a calculation works; doing it the other way round does not. Many logic errors are not wrong instructions at all — they are correct instructions placed in the wrong sequence, which is why tracing a program line by line finds bugs that reading it does not.

1. Selection

Selection allows the program to choose different paths based on a condition.

IF ... THEN ... ELSE

Used for binary choices (Yes/No).

IF Mark >= 50 THEN
  OUTPUT "Pass"
ELSE
  OUTPUT "Fail"
ENDIF
CASE ... OF

Efficient for multiple discrete choices (e.g., a menu).

CASE Choice OF
  1 : CALL AddRecord()
  2 : CALL DeleteRecord()
  3 : CALL ViewRecord()
  OTHERWISE OUTPUT "Invalid Choice"
ENDCASE

2. Iteration (Loops)

Iteration is used to repeat a block of code multiple times.

FOR ... TO ... NEXT

Count-controlled: Used when you know exactly how many times the loop should run.

FOR Count ← 1 TO 10
  OUTPUT "Iteration: ", Count
NEXT Count
REPEAT ... UNTIL

Post-condition: The condition is checked at the end. The loop always runs at least once.

REPEAT
  OUTPUT "Enter a positive number"
  INPUT Num
UNTIL Num > 0
WHILE ... DO ... ENDWHILE

Pre-condition: The condition is checked at the start. If the condition is false initially, the loop never runs.

WHILE Answer <> "Exit" DO
  INPUT Answer
ENDWHILE

3. Comparison Table: Which Loop to Use?

Loop Type Best Used For... Min. Iterations
FOR Fixed number of repetitions. Defined by range
REPEAT Validation (checking input). 1
WHILE Reading files or unknown repetitions. 0
⚠️ Exam Note: Make sure to close your constructs! Every IF needs an ENDIF, every WHILE needs an ENDWHILE, and every CASE needs an ENDCASE.

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).

OperatorFunctionExample
+Addition10 + 5 = 15
-Subtraction10 - 5 = 5
*Multiplication10 * 5 = 50
/Division10 / 4 = 2.5
DIVInteger Division (Quotient only)10 DIV 4 = 2
MODModulo (Remainder only)10 MOD 4 = 2
^Raised to the power of2 ^ 3 = 8

B. Relational (Comparison) Operators

These compare two values and return a Boolean result (TRUE or FALSE). Used in IF statements and Loops.

OperatorFunctionExample
=Equal toX = 10
<>Not equal toX <> 10
>Greater thanX > 10
<Less thanX < 10
>=Greater than or equal toX >= 10
<=Less than or equal toX <= 10

C. Boolean (Logical) Operators

Used to combine multiple conditions together to form complex logic.

OperatorFunctionExample
ANDTrue only if BOTH are true(X > 5) AND (X < 10)
ORTrue if AT LEAST ONE is true(Ans = 'Y') OR (Ans = 'y')
NOTReverses the Boolean valueNOT (X = 10)

2. Official Library Routines (String Handling)

The following are the only string functions recognized in the Cambridge marking schemes:

RoutineFunction
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

RoutineFunction
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.

Procedures, Functions and Variable Scope

Watch this lesson Video 8.1.4 · 8:18 · Subroutines, what they return, what they can see, and what they can change

1. Why Use Subroutines?

A subroutine is a named block of code that performs one specific task and can be called from anywhere in a program. Cambridge pseudocode has two kinds: procedures and functions.

Subroutines are used because they:

  • avoid repeating the same code in several places
  • make a program easier to read, test and maintain
  • allow one task to be written and tested independently of the rest of the program
  • let a large problem be broken down (decomposed) into smaller parts

2. Procedures vs Functions

This distinction is examined regularly, and the difference is exactly one thing: whether a value is returned.

 ProcedureFunction
Returns a value?NoYes — always returns exactly one value
KeywordsPROCEDURE … ENDPROCEDUREFUNCTION … RETURNS … ENDFUNCTION
How it is calledCALL Name(…)Used inside an expression, e.g. x ← Name(…)
Typical usePerform an action, e.g. display a menuCalculate and hand back a result, e.g. an area

A procedure

PROCEDURE DisplayMenu() OUTPUT "1. Add a record" OUTPUT "2. Delete a record" OUTPUT "3. Quit" ENDPROCEDURE // Called like this: CALL DisplayMenu()

A function

FUNCTION AreaOfRectangle(Width : INTEGER, Height : INTEGER) RETURNS INTEGER DECLARE Area : INTEGER Area ← Width * Height RETURN Area ENDFUNCTION // Called inside an expression: DECLARE MyArea : INTEGER MyArea ← AreaOfRectangle(5, 3) // MyArea is now 15

3. Parameters

A parameter is a value passed into a subroutine so that the same subroutine can work on different data each time it is called.

TermMeaning
ParameterThe variable named in the subroutine definition, e.g. Width
ArgumentThe actual value supplied when the subroutine is called, e.g. 5

A subroutine may take no parameters at all (as with DisplayMenu()), or several.

Syllabus limit: procedures and functions may have up to three parameters. You will never be asked to write one with more than three.
// A procedure with two parameters PROCEDURE PrintTimes(Message : STRING, Count : INTEGER) FOR i ← 1 TO Count OUTPUT Message NEXT i ENDPROCEDURE CALL PrintTimes("Hello", 3) // outputs Hello three times

3b. Passing Parameters: By Value and By Reference

When an argument is passed to a subroutine there are two ways it can travel, and the syllabus names both. The difference is whether the subroutine can change the caller's variable.

  • By value — a copy of the data is passed. The subroutine works on the copy, so any change it makes is discarded when the subroutine ends. The original variable is untouched.
  • By reference — the location of the original variable is passed. The subroutine works on the original itself, so any change it makes persists after the subroutine ends.
By value By reference
What is passed A copy of the value The address of the original
Original changed? No Yes
Use it when The subroutine only needs to read the value The subroutine must update the caller's variable
Worked contrast. A variable Score holds 10 and is passed to a subroutine that doubles its parameter. Passed by value, the subroutine's copy becomes 20, and after the call Score is still 10. Passed by reference, the subroutine changes the original, and after the call Score is 20. Same subroutine, same argument, different result — and the only difference is how the parameter was passed.

Why by value is the safer default: a subroutine that cannot alter the caller's variables cannot cause an unexpected change somewhere else in the program. Passing by reference is more efficient for large data such as an array, since nothing is copied, but it gives the subroutine the power to modify data its caller may not expect to change.

4. Local and Global Variables

Where a variable is declared decides which parts of the program can see and use it. This is called the variable's scope.

 Local variableGlobal variable
DeclaredInside a subroutineOutside every subroutine, in the main program
Can be used byOnly that subroutineThe whole program, including all subroutines
ExistsOnly while the subroutine is runningFor as long as the program runs
DECLARE Total : INTEGER // GLOBAL - visible everywhere Total ← 0 PROCEDURE AddScore(Score : INTEGER) DECLARE Bonus : INTEGER // LOCAL - only exists in here Bonus ← 10 Total ← Total + Score + Bonus ENDPROCEDURE CALL AddScore(5) OUTPUT Total // 15 - Total is global, so this works // OUTPUT Bonus would FAIL - Bonus is local to AddScore

Why local variables are preferred

  • They cannot be changed accidentally by another part of the program.
  • The same variable name can be reused safely in different subroutines.
  • They free up memory when the subroutine finishes.
  • A subroutine using only local variables and parameters can be tested on its own.

5. Nested Statements

A statement is nested when it is placed inside another statement of the same kind — a loop inside a loop, or an IF inside an IF.

// Nested iteration: printing a 3 x 4 grid of stars FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT "*" NEXT Column NEXT Row // Nested selection IF Age >= 13 THEN IF Age <= 19 THEN OUTPUT "Teenager" ENDIF ENDIF
Syllabus limit: you will not be required to write more than three levels of nested statements.

The inner loop completes in full for every single pass of the outer loop. In the grid example the outer loop runs 3 times and the inner loop runs 4 times per pass, so OUTPUT happens 3 × 4 = 12 times.

6. Exam Focus

The difference is the return value, not the length or complexity. Candidates often write that "a function is bigger" or "a procedure is simpler". Neither earns the mark. A function returns a value; a procedure does not.
Call them correctly. A procedure is called with CALL. A function is not — it appears inside an expression, because its returned value has to go somewhere. Writing CALL AreaOfRectangle(5, 3) as a standalone statement discards the result and loses marks.
Keep parameter and argument straight. The parameter is in the definition; the argument is the value passed in the call. Also match them in order and in number — a subroutine defined with two parameters must be called with two arguments.
Trace nested loops carefully. When completing a trace table for nested iteration, finish the entire inner loop before advancing the outer loop counter. Advancing both together is the single most common error in nested-loop trace questions.

Quick self-check

  • State the one difference between a procedure and a function.
  • Write a function that takes two parameters and returns the larger of them.
  • Explain why Bonus in the example above cannot be output from the main program.
  • State the maximum number of parameters a subroutine may have in this syllabus.
  • How many times does OUTPUT run if the outer loop is 1 to 5 and the inner is 1 to 3?

Worked Examples

Watch this lesson Video 8.1.5 · 6:45 · Two full programs, assembled from patterns you already know

Example 1: Temperature Monitoring System

The Scenario: A greenhouse requires a program to record 24 hourly temperature readings. The program must:
  • Store the readings in a 1D array.
  • Calculate and output the average temperature.
  • Identify and output the highest and lowest temperatures recorded.
DECLARE Temp : ARRAY[1:24] OF REAL
DECLARE Total, Average, Max, Min : REAL
DECLARE i : INTEGER

Total ← 0

// 1. Input loop with Validation
FOR i ← 1 TO 24
  REPEAT
    OUTPUT "Enter temperature for hour ", i
    INPUT Temp[i]
  UNTIL Temp[i] > -50 AND Temp[i] < 60
  Total ← Total + Temp[i]
NEXT i

// 2. Initialize Max and Min with the first value in the array
Max ← Temp[1]
Min ← Temp[1]

// 3. Search Loop
FOR i ← 2 TO 24
  IF Temp[i] > Max THEN Max ← Temp[i] ENDIF
  IF Temp[i] < Min THEN Min ← Temp[i] ENDIF
NEXT i

Average ← Total / 24

OUTPUT "Average: ", Average
OUTPUT "Highest: ", Max, " Lowest: ", Min

Example 2: School Grades (2D Arrays)

The Scenario: A teacher manages marks for 30 students across 3 subjects (Maths, Science, English). The program must:
  • Use a 2D array to store the marks.
  • Count how many students scored an average of > 80 (Distinction).
  • Allow a teacher to search for a student's marks by entering their ID (1-30).
DECLARE Marks : ARRAY[1:30, 1:3] OF INTEGER
DECLARE StudentID, DistCount, Row, Col : INTEGER
DECLARE StudentSum : REAL

DistCount ← 0

// Populate the 2D Array
FOR Row ← 1 TO 30
  StudentSum ← 0
  FOR Col ← 1 TO 3
    INPUT Marks[Row, Col]
    StudentSum ← StudentSum + Marks[Row, Col]
  NEXT Col

  // Check for Distinction
  IF (StudentSum / 3) > 80 THEN
    DistCount ← DistCount + 1
  ENDIF
NEXT Row

// Search Functionality
OUTPUT "Enter Student ID (1-30):"
INPUT StudentID
OUTPUT "Maths: ", Marks[StudentID, 1]
OUTPUT "Science: ", Marks[StudentID, 2]
OUTPUT "English: ", Marks[StudentID, 3]
OUTPUT "Total distinctions: ", DistCount
15-Mark Strategy Checklist
  • Initialization: Did you set totals to 0 and max/min variables?
  • Input Prompts: Did you use OUTPUT before every INPUT?
  • Validation: Did you use REPEAT...UNTIL for range checks?
  • End Tags: Did you close every FOR, IF, and WHILE?
  • Efficiency: Did you use the correct data types (REAL for averages)?