Pseudocode Foundations & Conventions: Cambridge AS Level Pseudocode Rules And Conventions (Copy)
Cambridge AS Level Pseudocode Rules And Conventions
Purpose And Exam Context (Paper 2 Focus)
- Pseudocode is the only accepted answer style for Paper 2
- Marks are awarded for:
- Correct logic
- Clear structure
- Proper use of Cambridge-style constructs
- Syntax precision matters less than logic, but conventions matter a lot
- Answers must be:
- Language-neutral
- Step-by-step
- Easy for an examiner to trace
Core Structural Rules (Must-Follow)
| Rule | What To Do | Why It Matters |
|---|---|---|
| One action per line | One instruction on each line | Prevents ambiguity |
| Indentation | Indent all inner blocks | Shows logic clearly |
| Block closure | Close every IF / LOOP / PROC | Missing END loses marks |
| Readable flow | Top to bottom logic | Examiner tracing |
Assignment, Comparison And Operators
| Purpose | Correct Usage | Example |
|---|---|---|
| Assignment | ← | Total ← Total + Value |
| Comparison | = | IF Age = 18 THEN |
| Not equal | <> | IF Score <> 0 THEN |
| Logical AND | AND | IF A > 0 AND B < 5 |
| Logical OR | OR | IF Choice = 1 OR Choice = 2 |
| Logical NOT | NOT | IF NOT Found THEN |
Variable Declaration And Initialisation
| Type | Declaration Example | Initialisation Example |
|---|---|---|
| INTEGER | DECLARE Count : INTEGER | Count ← 0 |
| REAL | DECLARE Avg : REAL | Avg ← 0 |
| STRING | DECLARE Name : STRING | Name ← “” |
| BOOLEAN | DECLARE Found : BOOLEAN | Found ← FALSE |
- Always initialise:
- Counters
- Totals
- Flags
- Uninitialised variables = logic error
Constants (Fixed Values)
| Use Case | Example |
|---|---|
| Thresholds | CONSTANT PassMark ← 50 |
| Limits | CONSTANT MaxSize ← 30 |
- Use constants when a value never changes
- Improves readability and avoids magic numbers
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions And 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Input And Output Conventions
| Operation | Correct Pseudocode |
|---|---|
| Input | INPUT Age |
| Output text only | OUTPUT “Invalid input” |
| Output variable | OUTPUT “Total = “, Total |
- Always label outputs
- Never use PRINT / READ (language-specific)
IF Selection Structures (Fully Guided)
Simple IF
IF Age >= 18 THEN
OUTPUT "Eligible"
ENDIF
IF–ELSE
IF Score >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Nested IF
IF Age >= 18 THEN
IF HasID = TRUE THEN
OUTPUT "Allowed"
ELSE
OUTPUT "ID Required"
ENDIF
ELSE
OUTPUT "Underage"
ENDIF
CASE Selection (Menu-Style Logic)
| When To Use CASE | Why |
|---|---|
| Menu options | Cleaner than nested IF |
| Fixed choices | Easier to trace |
INPUT Choice
CASE Choice OF
1 : OUTPUT "Add"
2 : OUTPUT "Delete"
3 : OUTPUT "Search"
OTHERWISE : OUTPUT "Invalid"
ENDCASE
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions And 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Loop Structures (Decision Guide)
| Loop Type | Use When |
|---|---|
| FOR | Number of repetitions known |
| WHILE | Stop condition unknown |
| REPEAT UNTIL | Must run at least once |
FOR Loop (Count-Controlled)
Total ← 0
FOR i ← 1 TO 10
INPUT Value
Total ← Total + Value
ENDFOR
OUTPUT "Total = ", Total
WHILE Loop (Pre-Condition)
Count ← 0
WHILE Count < 5 DO
INPUT Num
Count ← Count + 1
ENDWHILE
- Condition checked before loop runs
REPEAT UNTIL Loop (Post-Condition)
REPEAT
INPUT Age
UNTIL Age >= 0 AND Age <= 120
- Loop runs at least once
- UNTIL condition ends the loop
Procedures (Task-Based Logic)
| Feature | Procedure |
|---|---|
| Returns value | No |
| Used for | Tasks / updates |
PROCEDURE DisplayResult(Mark : INTEGER)
IF Mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDPROCEDURE
CALL DisplayResult(Score)
Functions (Value-Returning Logic)
| Feature | Function |
|---|---|
| Returns value | Yes |
| Used in | Expressions |
FUNCTION GetAverage(Total : INTEGER, Count : INTEGER) RETURNS REAL
RETURN Total / Count
ENDFUNCTION
Average ← GetAverage(Sum, N)
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions And 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Arrays (1D And 2D)
| Type | Declaration |
|---|---|
| 1D | DECLARE Marks : ARRAY[1:30] OF INTEGER |
| 2D | DECLARE Grid : ARRAY[1:10,1:10] OF INTEGER |
Traversing 1D Array
FOR i ← 1 TO 30
Total ← Total + Marks[i]
ENDFOR
Traversing 2D Array
FOR r ← 1 TO 10
FOR c ← 1 TO 10
OUTPUT Grid[r,c]
ENDFOR
ENDFOR
Linear Search (Flag-Controlled Pattern)
Found ← FALSE
i ← 1
WHILE i <= N AND Found = FALSE DO
IF A[i] = Target THEN
Found ← TRUE
ELSE
i ← i + 1
ENDIF
ENDWHILE
IF Found THEN
OUTPUT "Found at ", i
ELSE
OUTPUT "Not Found"
ENDIF
Records (Structured Data)
| Use | Store mixed data types |
|---|
RECORD StudentType
Name : STRING
Age : INTEGER
Score : INTEGER
ENDRECORD
DECLARE Student : StudentType
Student.Name ← "Ali"
Student.Score ← 85
Text File Handling (AS Scope)
OPENFILE DataFile FOR READ
WHILE NOT EOF(DataFile) DO
READFILE DataFile, Line
OUTPUT Line
ENDWHILE
CLOSEFILE DataFile
| Common Errors | Fix |
|---|---|
| No EOF check | Always use WHILE NOT EOF |
| File not closed | Always CLOSEFILE |
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions And 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Common Exam Traps And Fixes
| Trap | How To Avoid |
|---|---|
| Missing ENDIF / ENDFOR | Match blocks visually |
| Wrong loop choice | Decide before writing |
| No initialisation | Set all counters & flags |
| Unlabelled output | Always include text |
| Over-complex logic | Prefer clear step logic |
Safe Exam Skeleton (Reusable)
DECLARE variables
Initialise variables
INPUT values
Process logic
OUTPUT results
