IGCSE Computer Science - Programming | Data Types, Arrays, File Handling, Maintainable Code
Chapter 8 ยท Paper 2

Programming

Variables, data types, selection, iteration, operators, strings, procedures, functions, arrays, file handling and maintainable code.

Based on Cambridge IGCSE / O-Level CS Syllabus 0478/2210 (2026โ€“2028)

1 Data Types & Variables

Click or hover over each card to see its description and examples.

INTEGER
Whole numbers only. No decimals.

e.g. 5, -3, 0, 100
REAL
Numbers with a decimal/fractional part.

e.g. 3.14, -2.5, 0.0
CHAR
A single character in single quotes.

e.g. 'A', '5', '@', ' '
STRING
Zero or more characters in double quotes.

e.g. "Hello", "42", ""
BOOLEAN
Only two possible values:

TRUE or FALSE

"35" is a STRING, not an INTEGER โ€” the speech marks make it text. You cannot perform arithmetic on it directly.

Declarations
// Variables โ€” value can change during execution
DECLARE Score     : INTEGER
DECLARE Average   : REAL
DECLARE FirstName : STRING
DECLARE Grade     : CHAR
DECLARE Passed    : BOOLEAN

// Constants โ€” value cannot change during execution
CONSTANT MaxScore โ† 100
CONSTANT PI       โ† 3.14159

2 Input & Output

Pseudocode
INPUT  Name
INPUT  Score
OUTPUT "Your score is: ", Score
OUTPUT "Hello, ", Name, "!"

3 Selection โ€” IF & CASE

IF without ELSE
IF Score >= 50
  THEN
    OUTPUT "Pass"
ENDIF
IF with ELSE and nested IF
IF Score >= 70
  THEN
    OUTPUT "Distinction"
  ELSE
    IF Score >= 50
      THEN
        OUTPUT "Pass"
      ELSE
        OUTPUT "Fail"
    ENDIF
ENDIF

Use CASE when testing one variable against multiple specific values. It is cleaner than a long chain of IF/ELSE IF statements.

CASE OF
CASE OF Grade
  'A' : OUTPUT "Excellent"
  'B' : OUTPUT "Good"
  'C' : OUTPUT "Average"
  OTHERWISE : OUTPUT "Below Average"
ENDCASE

4 Iteration

Used when the number of iterations is known in advance. The loop variable is automatically incremented.

// Basic FOR loop
FOR i โ† 1 TO 5
    OUTPUT i
NEXT i
// Outputs: 1  2  3  4  5

// With STEP (counting backwards by 2)
FOR i โ† 10 TO 1 STEP -2
    OUTPUT i
NEXT i
// Outputs: 10  8  6  4  2

The condition is checked before each iteration. If the condition is false at the start, the body never executes.

WHILE Number > 0 DO
    OUTPUT Number
    Number โ† Number - 1
ENDWHILE

The body executes at least once โ€” the condition is checked after each iteration. Continues until the condition becomes TRUE.

REPEAT
    OUTPUT "Enter password: "
    INPUT Password
UNTIL Password = "Secret123"

Key difference: WHILE checks condition first (may run 0 times). REPEAT UNTIL checks condition last (runs at least once).

5 Operators

Arithmetic Operators
  • +   addition
  • -   subtraction
  • *   multiplication
  • /   division (real result)
  • ^   power of
  • MOD   remainder after division
  • DIV   integer quotient
Logical & Comparison
  • =   equal to
  • <>   not equal to
  • <   <=   >   >=
  • AND   both conditions true
  • OR   at least one true
  • NOT   inverts a Boolean value
Library Routines
MOD(10, 3)       // returns 1  (10 mod 3 = remainder)
DIV(10, 3)       // returns 3  (integer division)
ROUND(3.567, 2)  // returns 3.57
RANDOM()         // returns a random number 0.0 to 1.0 inclusive

6 String Handling

FunctionDescriptionExampleResult
LENGTH(s)Number of characters in string sUCASE(s)LCASE(s)SUBSTRING(s, start, len)

The first character of a string is at position 1 in Cambridge pseudocode (not 0). Always check the question wording if it specifies otherwise.

7 Procedures & Functions

Procedure
  • A named, reusable block of code
  • Does NOT return a value
  • Called using CALL
  • Can take 0 or more parameters
  • Used for actions (display, save)
Function
  • A named, reusable block of code
  • Returns a single value
  • Called within an expression (no CALL)
  • Uses RETURN to send back a value
  • Used for calculations
Procedure Example
PROCEDURE DisplayGreeting(Name : STRING)
    OUTPUT "Hello, ", Name
ENDPROCEDURE

CALL DisplayGreeting("Arj")   // outputs: Hello, Arj
Function Example
FUNCTION Square(n : INTEGER) RETURNS INTEGER
    RETURN n * n
ENDFUNCTION

Result โ† Square(5)    // Result = 25
OUTPUT Square(4)        // outputs: 16

Local vs Global Variables

Local VariableGlobal Variable
Declared inside} --
Accessible from} --
Lifetime} --
Preferred?} --

8 Arrays โ€” 1D & 2D

An array is a fixed-length data structure that stores multiple elements of the same data type, accessed using an index number.

1D Array
// Declare: 5 integers, index 1 to 5
DECLARE Scores : ARRAY[1:5] OF INTEGER

Scores[1] โ† 85
Scores[2] โ† 72

// Read all values with a loop
FOR i โ† 1 TO 5
    OUTPUT Scores[i]
NEXT i
2D Array (grid/table)
// Declare: 3ร—3 grid of characters
DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR

Grid[1,1] โ† 'X'
Grid[2,3] โ† 'O'

// Nested loop to process entire grid
FOR row โ† 1 TO 3
    FOR col โ† 1 TO 3
        Grid[row, col] โ† ' '
    NEXT col
NEXT row

9 File Handling

CommandPurpose
OPENFILE "file.txt" FOR READ} --
OPENFILE "file.txt" FOR WRITE} --
OPENFILE "file.txt" FOR APPEND} --
READFILE "file.txt", Variable} --
WRITEFILE "file.txt", Variable} --
CLOSEFILE "file.txt"} --
EOF("file.txt")} --
Reading a file with EOF
DECLARE Line : STRING
OPENFILE "data.txt" FOR READ
WHILE NOT EOF("data.txt") DO
    READFILE "data.txt", Line
    OUTPUT Line
ENDWHILE
CLOSEFILE "data.txt"

10 Maintainable Code

Maintainable code is easy to read, understand and modify by any programmer โ€” not just the original author.

TechniqueDescriptionExample
Meaningful identifiers} --
Comments} --
Procedures/Functions} --
Constants} --

For full marks on maintainability questions, mention all four: meaningful identifiers, comments, procedures/functions, and constants.

Scroll to Top