Detailed Pseudocode Guidelines
These guidelines are based on Cambridge IGCSE Computer Science (0478) syllabus 2026-2028 pseudocode conventions from the assessment details section (pages 35-49). They focus on practical writing: structuring logic, choosing correct control flow, and building exam-style solutions with clear, readable pseudocode.
Important notation note
Official Cambridge examples use a left-arrow assignment symbol. In this compiler/editor, write assignment as <-.
Editor support
Settings has an Exam syntax control for Cambridge IGCSE, Cambridge A Level, IB, OCR, and AQA. Completions follow the board you pick, including DIV, MOD, LENGTH, and the extra routines that board uses.
Writing Workflow
Use this process every time you solve a question to avoid logic errors.
- 1Read the problem and identify required inputs and outputs.
- 2Declare every variable/array with correct data type.
- 3Initialize counters/totals/flags before loops.
- 4Choose selection and loop types based on the problem conditions.
- 5Write processing logic in small, readable blocks.
- 6Output final results after processing.
- 7Dry-run with sample values and edge cases.
// ==================================================
// EXAM-STYLE PSEUDOCODE STRUCTURE
// ==================================================
// 1) Declarations and constants
DECLARE Index : INTEGER
DECLARE Value : INTEGER
DECLARE Total : INTEGER
CONSTANT MaxItems <- 5
// 2) Initialisation
Total <- 0
// 3) Input + processing
FOR Index <- 1 TO MaxItems
INPUT Value
Total <- Total + Value
NEXT Index
// 4) Output
OUTPUT "Total = ", TotalCore Syntax Rules
Formatting and Names
- Keywords in UPPER CASE: IF, FOR, WHILE, PROCEDURE.
- Identifiers in PascalCase and starting with capital letter.
- No underscore in identifier names.
- Use comments with //.
Data Types and Constants
- INTEGER, REAL, CHAR, STRING, BOOLEAN.
- DECLARE Name : TYPE for variables.
- CONSTANT Name <- literal for constants.
- Constant value must be a literal, not an expression.
Operators
- Arithmetic: + - * / ^
- Integer division routines: DIV(a,b), MOD(a,b)
- Relational: = < <= > >= <>
- Logical: AND OR NOT
Common Library Routines
- LENGTH(StringValue) returns the number of characters in a string.
- LCASE(StringOrChar) and UCASE(StringOrChar) change case.
- SUBSTRING(StringValue, Start, Length) uses a 1-based start position.
- ROUND(RealValue, Places) rounds a real number to a chosen number of decimal places.
- RANDOM() returns a random number between 0 and 1 inclusive.
Official Routine Syntax
LENGTH("Happy Days")
LCASE('W')
UCASE("Happy")
SUBSTRING("Happy Days", 1, 5)
ROUND(15.6789, 2)
RANDOM()
Value <- ROUND(RANDOM() * 6, 0)Cambridge specifically defines ROUND(Value, Places) and RANDOM(), and both routines are available in this editor's autocomplete.
Loop Logic Deep Dive
Students lose marks by choosing the wrong loop type or by writing loops that never terminate. Understand the condition timing and state changes in every cycle.
FOR Loop
- Use FOR when iteration count is known.
- Bounds are inclusive, so FOR I <- 1 TO 5 runs 5 times.
- STEP can be positive or negative.
WHILE Loop
- Condition checked before loop body.
- Can execute zero times if initial condition is FALSE.
- Update variables inside loop so condition can become FALSE.
REPEAT UNTIL
- Loop body executes before condition check.
- Always executes at least once.
- Use UNTIL valid condition for input validation patterns.
Termination Checklist
- Does each iteration change state?
- Can condition eventually become FALSE (WHILE) or TRUE (UNTIL)?
- Are bounds correct (no off-by-one mistakes)?
- Have you initialized counters/totals before the loop?
Reusable Coding Patterns
Counted Loop with FOR (Fixed Number of Repeats)
When to use: You know exactly how many times the loop must run.
- Set loop variable start and end values.
- Run body once for each value in the range.
- Update totals/counters each cycle.
- Exit automatically when range is complete.
DECLARE Count : INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR Count <- 1 TO 10
Total <- Total + Count
NEXT Count
OUTPUT "Total = ", TotalLogic walkthrough
- Before loop: Total = 0
- Count=1 -> Total=1
- Count=2 -> Total=3
- Count=3 -> Total=6
- ...
- Count=10 -> Total=55 then loop ends
Pre-condition Loop with WHILE (Unknown Repeats)
When to use: Repeat while a condition remains TRUE.
- Condition is checked before each iteration.
- If condition is FALSE at start, loop runs zero times.
- Body must change state so condition can eventually become FALSE.
DECLARE Number : INTEGER
INPUT Number
WHILE Number > 9 DO
Number <- Number - 9
ENDWHILE
OUTPUT NumberLogic walkthrough
- Input 28
- 28 > 9 true -> Number becomes 19
- 19 > 9 true -> Number becomes 10
- 10 > 9 true -> Number becomes 1
- 1 > 9 false -> exit
Post-condition Loop with REPEAT UNTIL (Validation Loop)
When to use: User must do something at least once, then repeat until valid.
- Body executes first, then condition is tested.
- Best for input validation and menu retries.
- Condition should represent the 'valid/finished' state.
DECLARE Password : STRING
REPEAT
OUTPUT "Enter password"
INPUT Password
UNTIL Password = "Secret"Logic walkthrough
- If first entry is wrong, loop repeats.
- If first entry is correct, still valid because one run is guaranteed.
Nested FOR Loops (Tables / Grids / 2D Arrays)
When to use: Process rows and columns or all combinations of two ranges.
- Outer loop controls each row/item group.
- Inner loop completes all columns/items for current outer value.
- Reset row-level totals before starting inner loop.
DECLARE Row : INTEGER
DECLARE Column : INTEGER
DECLARE RowTotal : INTEGER
DECLARE GrandTotal : INTEGER
DECLARE Amount : ARRAY[1:5, 1:4] OF INTEGER
GrandTotal <- 0
FOR Row <- 1 TO 5
RowTotal <- 0
FOR Column <- 1 TO 4
RowTotal <- RowTotal + Amount[Row, Column]
NEXT Column
OUTPUT "Row ", Row, " total = ", RowTotal
GrandTotal <- GrandTotal + RowTotal
NEXT Row
OUTPUT "Grand total = ", GrandTotalLogic walkthrough
- Row 1 processes columns 1..4, then outputs row 1 total.
- Row 2 starts fresh with RowTotal reset to 0.
- After final row, GrandTotal contains sum of all cells.
Fully Worked Programs
These are full exam-style answers showing declarations, control flow, and output.
Program A: Grade Counter with Selection + Loop
Objective: Read 5 marks, count how many are passes (>= 50), and show class average.
DECLARE Index : INTEGER
DECLARE Mark : INTEGER
DECLARE Total : INTEGER
DECLARE PassCount : INTEGER
DECLARE Average : REAL
Total <- 0
PassCount <- 0
FOR Index <- 1 TO 5
OUTPUT "Enter mark ", Index
INPUT Mark
Total <- Total + Mark
IF Mark >= 50
THEN
PassCount <- PassCount + 1
ENDIF
NEXT Index
Average <- Total / 5
OUTPUT "Passes = ", PassCount
OUTPUT "Average = ", AverageHow the logic works
- This is count-controlled because there are exactly 5 marks.
- Total accumulates all marks; PassCount tracks a condition inside the loop.
- IF branch is optional per iteration; only true marks increment PassCount.
- Average is computed once, after loop completes.
Sample run
- Input marks: 42, 50, 74, 21, 90
- Total = 277
- PassCount = 3
- Average = 55.4
Program B: Input Validation Menu with REPEAT UNTIL
Objective: Accept only menu choices 1 to 4.
DECLARE Choice : INTEGER
REPEAT
OUTPUT "1.View 2.Add 3.Delete 4.Exit"
INPUT Choice
UNTIL Choice >= 1 AND Choice <= 4
OUTPUT "Accepted choice: ", ChoiceHow the logic works
- REPEAT UNTIL is ideal because user must be prompted at least once.
- Condition states what valid means, not what invalid means.
- Using AND ensures both lower and upper bounds are respected.
Sample run
- Input: 8 -> invalid, repeats
- Input: 0 -> invalid, repeats
- Input: 3 -> valid, exits
Program C: Search in Array with Flag
Objective: Find whether a target name exists in StudentNames[1:30].
DECLARE StudentNames : ARRAY[1:30] OF STRING
DECLARE Index : INTEGER
DECLARE Target : STRING
DECLARE Found : BOOLEAN
INPUT Target
Found <- FALSE
FOR Index <- 1 TO 30
IF StudentNames[Index] = Target
THEN
Found <- TRUE
ENDIF
NEXT Index
IF Found = TRUE
THEN
OUTPUT "Found"
ELSE
OUTPUT "Not found"
ENDIFHow the logic works
- Found starts FALSE and flips TRUE when match appears.
- This variant checks all items; examiners usually accept this clear style.
- Final IF uses the flag to output one message.
Sample run
- Target = "Ali" and appears at index 7 -> Found becomes TRUE
- Output: Found
Program D: Procedure + Function Together
Objective: Show reuse: a procedure for display and a function for calculation.
PROCEDURE PrintLine(Count : INTEGER)
DECLARE Index : INTEGER
FOR Index <- 1 TO Count
OUTPUT "-"
NEXT Index
ENDPROCEDURE
FUNCTION SumSquare(A : INTEGER, B : INTEGER) RETURNS INTEGER
RETURN A * A + B * B
ENDFUNCTION
DECLARE Answer : INTEGER
CALL PrintLine(10)
Answer <- SumSquare(3, 4)
OUTPUT "Answer = ", AnswerHow the logic works
- Procedure is called using CALL because it is a full statement.
- Function returns a value, so it appears in an expression.
- Keeping display logic and calculation logic separate improves clarity.
Sample run
- PrintLine outputs 10 dashes.
- SumSquare(3,4) returns 25.
- Output: Answer = 25
Trace Tables and Dry Runs
In exams, a fast dry-run catches most logical mistakes. Track key variables after each iteration.
DECLARE N : INTEGER
DECLARE Fact : INTEGER
DECLARE I : INTEGER
INPUT N
Fact <- 1
FOR I <- 1 TO N
Fact <- Fact * I
NEXT I
OUTPUT Fact| I | Fact before | Fact after Fact <- Fact * I |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 6 |
| 4 | 6 | 24 |
| 5 | 24 | 120 |
For N = 5, expected output is 120. If your trace table and output disagree, debug the loop boundaries and initial value.
File Handling Guide
Declare file name and data variables.
OPENFILE with FOR READ or FOR WRITE.
Use READFILE/WRITEFILE operations.
CLOSEFILE when finished.
DECLARE FileName : STRING
DECLARE LineText : STRING
FileName <- "Scores.txt"
OPENFILE FileName FOR READ
READFILE FileName, LineText
OUTPUT "First line was: ", LineText
CLOSEFILE FileNameAvoid opening the same file in both READ and WRITE simultaneously, and always close files even in short algorithms.
Exam Command Words
| Command Word | Meaning in Answers |
|---|---|
| Calculate | Work out from given facts, figures or information. |
| Compare | Identify and comment on similarities and differences. |
| Define | Give the precise meaning. |
| Demonstrate | Show how, or give an example. |
| Describe | State points, characteristics and main features. |
| Evaluate | Judge quality, importance, amount or value. |
| Explain | Give reasons, show relationships, and support with evidence. |
| Give | Produce an answer from source material or recall. |
| Identify | Name, select or recognise. |
| Outline | Set out main points. |
| Show (that) | Provide structured evidence leading to a result. |
| State | Express in clear terms. |
| Suggest | Apply knowledge to give valid proposals or considerations. |