Score and score refer to the same identifier.IF, FOR, TRUE, PROCEDURE).totalScore, maxAttempts).| Statement | Purpose |
|---|---|
| DECLARE identifier : type | Creates a variable of the given type. |
| CONSTANT identifier ← value | Creates a constant with the specified value. |
| Type | Typical values | Literal syntax |
|---|---|---|
| INTEGER | Whole numbers | 5, -12, 0 |
| REAL | Numbers with a decimal point | 3.14, -0.5, 2.0 |
| CHAR | Single character | 'A', '9', '#' |
| STRING | Sequence of characters | "Hello", "IGCSE" |
| BOOLEAN | True / False values | TRUE, FALSE |
| Category | Operators | Example |
|---|---|---|
| Arithmetic | + – * / ^ DIV MOD | 5 + 3, 10 DIV 3 |
| Relational | = < <= > >= <> | score >= 50 |
| Logical (Boolean) | AND OR NOT | passed AND attended |
| Aspect | Global variable | Local variable |
|---|---|---|
| Declaration location | Outside any routine | Inside a routine or block |
| Lifetime | Program start → program end | Routine entry → routine exit |
| Visibility | All routines can access | Only the defining routine can access |
| Memory usage | Allocated once | Allocated each call (stack frame) |
| Typical use | Configuration data, shared counters, flags | Temporary calculations, loop counters, parameters |
The default execution order – statements are performed one after another from top to bottom.
Use a variable to accumulate a total or to count how many times a condition is true.
DECLARE total : INTEGER
total ← 0 // initialise
FOR i ← 1 TO n DO
total ← total + value[i] // totalling
ENDFOR
OUTPUT "Total = ", total
| Function | Purpose | Example |
|---|---|---|
| LEN(str) | Returns the length of a STRING. | LEN("Hello") → 5 |
| SUBSTRING(str, start, length) | Extracts a part of a STRING. | SUBSTRING("IGCSE", 2, 3) → "GCS" |
| UPPER(str) / LOWER(str) | Converts case. | UPPER("abc") → "ABC" |
| CONCAT(str1, str2) | Joins two strings. | CONCAT("Hi ", "Bob") → "Hi Bob" |
DECLARE scores : ARRAY[1:30] OF INTEGER // 1‑D, lower bound 1 DECLARE matrix : ARRAY[1:5, 1:5] OF REAL // 2‑D array
scores[i]matrix[row, col]
DECLARE total : INTEGER
total ← 0
FOR i ← 1 TO 30 DO
total ← total + scores[i] // totalling an array
ENDFOR
OUTPUT "Sum = ", total
PROCEDURE CALC_AVG(scores : ARRAY[1:n] OF INTEGER, n : INTEGER) // no return value
DECLARE sum, i : INTEGER
sum ← 0
FOR i ← 1 TO n DO
sum ← sum + scores[i]
ENDFOR
DECLARE avg : REAL
avg ← sum / n
OUTPUT "Average = ", avg
ENDPROCEDURE
FUNCTION MAX(a : INTEGER, b : INTEGER) : INTEGER
IF a > b THEN
RETURN a
ELSE
RETURN b
ENDIF
ENDFUNCTION
Only the basic statements required by the syllabus are shown.
OPEN "scores.txt" FOR READ AS infile
DECLARE line : STRING
DECLARE total, count : INTEGER
total ← 0
count ← 0
REPEAT
READLINE infile INTO line
IF NOT EOF(infile) THEN
DECLARE score : INTEGER
score ← INTEGER(line) // convert STRING to INTEGER
total ← total + score
count ← count + 1
ENDIF
UNTIL EOF(infile)
CLOSE infile
IF count > 0 THEN
DECLARE avg : REAL
avg ← total / count
OUTPUT "Average score = ", avg
ELSE
OUTPUT "No data found."
ENDIF
TRUE or FALSE.AND, OR, NOT.| A | B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE | FALSE |
| TRUE | FALSE | FALSE | TRUE | FALSE |
| FALSE | TRUE | FALSE | TRUE | TRUE |
| FALSE | FALSE | FALSE | FALSE | TRUE |
INPUT studentMark // reads a value into the variable studentMark OUTPUT "Result: ", result // displays text followed by the value of result
The variable used with INPUT or OUTPUT must be declared in a scope that is visible at that point in the program.
ROUND(x) – rounds a REAL to the nearest INTEGER.RANDOM(a, b) – returns a random INTEGER between a and b inclusive.LEN(str) – length of a STRING.SUBSTRING(str, start, length) – extracts part of a STRING.UPPER(str) / LOWER(str) – case conversion.global
counter = 0 # global variable
def increment():
global counter # refer to the global variable
counter = counter + 1 # modify it
for i in range(5):
increment()
print(counter) # prints 5
public class Counter {
static int total = 0; // global (class‑level) variable
public static void addOne() {
total = total + 1; // can be used directly
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
addOne();
}
System.out.println(total); // prints 5
}
}
CONSTANT PASS_MARK ← 40
DECLARE passCount : INTEGER
passCount ← 0 // global counter
PROCEDURE CHECK_PASS(score : INTEGER)
IF score >= PASS_MARK THEN
passCount ← passCount + 1 // updates the global counter
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDPROCEDURE
DECLARE s : INTEGER
INPUT s
CALL CHECK_PASS(s)
OUTPUT "Number of passes = ", passCount
globalTotal vs localTemp).DECLARE or CONSTANT causes a syntax error in the exam.
x = 10
def foo():
x = 5
print(x)
foo()
print(x)
x inside foo is local; the global x remains 10.)
"marks.txt", calculate the average, and output it. Use appropriate scope for variables.total being accessed and updated by two separate procedures (ADD and DISPLAY), contrasted with a local variable temp that exists only inside ADD.DECLARE/CONSTANT syntax.Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.