Pseudocode Foundations & Conventions: Indentation And Structure In Pseudocode (Copy)
Indentation And Structure In Pseudocode
Why Indentation And Structure Matter In Exams
- Indentation visually represents logical hierarchy
- Examiner uses indentation to:
- Identify blocks
- Match IF with ENDIF
- Trace loops accurately
- Poor indentation can make correct logic look incorrect
Core Indentation Rules (Non-Negotiable)
| Rule | Correct Practice | Examiner Expectation |
|---|---|---|
| Block indentation | Indent all statements inside a block | Clear nesting |
| Consistent spacing | Same indentation width throughout | Visual consistency |
| One block = one indent | Each new block adds one indent level | Easy tracing |
| End alignment | END statements align with starters | Structure clarity |
Basic Structured Layout (Skeleton Pattern)
DECLARE variables
Initialise variables
INPUT values
IF condition THEN
statements
ENDIF
OUTPUT results
- Straight vertical flow
- No sideways logic
- Easy to follow under time pressure
Indentation In IF Structures
Incorrect (logic becomes unclear)
IF Age >= 18 THEN
OUTPUT "Allowed"
ELSE
OUTPUT "Denied"
ENDIF
Correct (examiner-friendly)
IF Age >= 18 THEN
OUTPUT "Allowed"
ELSE
OUTPUT "Denied"
ENDIF
- Indentation shows:
- Which statements belong to IF
- Which belong to ELSE
Nested IF Indentation (Critical Skill)
IF Age >= 18 THEN
IF HasID = TRUE THEN
OUTPUT "Entry Allowed"
ELSE
OUTPUT "ID Required"
ENDIF
ELSE
OUTPUT "Underage"
ENDIF
| Indentation Level | Meaning |
|---|---|
| Level 0 | Main program |
| Level 1 | First IF |
| Level 2 | Nested IF |
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
Indentation In CASE Structures
Correct CASE Structure
CASE Option OF
1 : OUTPUT "Add"
2 : OUTPUT "Delete"
3 : OUTPUT "Search"
OTHERWISE : OUTPUT "Invalid"
ENDCASE
| Component | Alignment Rule |
|---|---|
| CASE | Starts at base level |
| Options | Indented once |
| ENDCASE | Aligned with CASE |
- Each option clearly separated
- No ambiguity about scope
Indentation In FOR Loops
Correct Structure
FOR i ← 1 TO 5
INPUT Value
Total ← Total + Value
ENDFOR
| Part | Alignment |
|---|---|
| FOR | Base level |
| Loop body | Indented |
| ENDFOR | Same level as FOR |
Nested Loops (2D Structures)
FOR row ← 1 TO 3
FOR col ← 1 TO 4
OUTPUT Grid[row, col]
ENDFOR
ENDFOR
- Each loop adds one indent level
- END statements close in reverse order
Indentation In WHILE Loops
WHILE Count < 3 DO
INPUT Value
Count ← Count + 1
ENDWHILE
- Everything controlled by WHILE is indented
- Condition clearly separated from body
Indentation In REPEAT UNTIL Loops
REPEAT
INPUT Age
UNTIL Age >= 0 AND Age <= 120
| Keyword | Alignment |
|---|---|
| REPEAT | Base level |
| Body | Indented |
| UNTIL | Same level as REPEAT |
- UNTIL aligns with REPEAT
- Condition is visually distinct
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
Procedures And Functions: Structural Discipline
PROCEDURE Structure
PROCEDURE DisplayResult(Mark : INTEGER)
IF Mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDPROCEDURE
| Rule | Purpose |
|---|---|
| Procedure body indented | Shows scope |
| ENDPROCEDURE aligned | Clear closure |
FUNCTION Structure
FUNCTION GetAverage(Total : INTEGER, Count : INTEGER) RETURNS REAL
RETURN Total / Count
ENDFUNCTION
- RETURN must be indented inside function
- ENDFUNCTION aligned with FUNCTION
Indentation In Array Processing
FOR i ← 1 TO 10
Total ← Total + Marks[i]
ENDFOR
- Array access inside loop body
- Index clearly associated with loop
Indentation In Linear Search (Exam-Standard)
Found ← FALSE
i ← 1
WHILE i <= N AND Found = FALSE DO
IF Data[i] = Target THEN
Found ← TRUE
ELSE
i ← i + 1
ENDIF
ENDWHILE
| Indentation Level | Purpose |
|---|---|
| Loop body | Controlled repetition |
| IF inside loop | Conditional match |
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
Structural Alignment Of END Keywords
| Starter | Correct End |
|---|---|
| IF | ENDIF |
| CASE | ENDCASE |
| FOR | ENDFOR |
| WHILE | ENDWHILE |
| PROCEDURE | ENDPROCEDURE |
| FUNCTION | ENDFUNCTION |
- END keyword must align vertically with its starter
- Misalignment suggests missing or incorrect block
Common Indentation Errors And Fixes
| Error | Why It’s Bad | Correct Fix |
|---|---|---|
| No indentation | Logic unclear | Indent blocks |
| Over-indentation | False nesting | One level only |
| END misaligned | Block mismatch | Align with starter |
| Mixed spacing | Visual confusion | Be consistent |
Examiner-Friendly Visual Checklist
| Checkpoint | Status |
|---|---|
| Blocks clearly indented | ✓ |
| Nested logic visible | ✓ |
| END keywords aligned | ✓ |
| One action per line | ✓ |
| No compressed logic | ✓ |
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
