Pseudocode Core Structures (Cambridge Standard): IF, ELSE, ELSE IF And Nested Selection Logic (Copy)
IF, ELSE, ELSE IF And Nested Selection Logic (Cambridge Standard – O Level 2210 + IGCSE 0478)
Purpose Of Selection Structures In Cambridge Pseudocode
- Selection structures are used to:
- Make decisions based on conditions
- Control which parts of an algorithm execute
- Cambridge Paper 2 heavily tests:
- Correct condition logic
- Correct placement of selection
- Correct handling of alternatives
- Selection logic directly affects:
- Algorithm correctness
- Trace table accuracy
- Section B modification success
Core Selection Structures Recognised By Cambridge
- IF … THEN … ENDIF
- IF … THEN … ELSE … ENDIF
- IF … THEN … ELSE IF … ELSE … ENDIF
- Nested IF structures (IF inside IF)
- All are:
- Language-independent
- Fully acceptable in both 2210 and 0478
Basic IF Statement (Single-Path Selection)
Structure
- IF condition THEN
- statements
- ENDIF
Meaning
- Statements execute only if the condition is TRUE
- If condition is FALSE:
- Nothing inside runs
- Control moves on
Example (Cambridge-Safe)
- IF score >= 50 THEN
- OUTPUT “Pass”
- ENDIF
- No ELSE branch
- Used when:
- Action is required only in one case
Common Mistakes With Basic IF
- Forgetting ENDIF
- Writing multiple statements without indentation
- Using assignment instead of comparison in condition
IF … ELSE Structure (Two-Path Selection)
Structure
- IF condition THEN
- statements when TRUE
- ELSE
- statements when FALSE
- ENDIF
Meaning
- Exactly one branch executes
- Condition fully partitions outcomes
Example
- IF option = 1 THEN
- OUTPUT “Add record”
- ELSE
- OUTPUT “Invalid option”
- ENDIF
Examiner Expectations
- ELSE must handle:
- All remaining cases
- ELSE branch must:
- Be logically opposite of IF
- Do not:
- Leave ELSE empty
Common IF–ELSE Errors
- Writing two separate IFs instead of IF–ELSE
- Forgetting ELSE when two outcomes exist
- Outputting in both branches unintentionally
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 O Level And IGCSE Computer Science Full Scale Course
ELSE IF Structure (Multiple Conditions)
Structure
- IF condition1 THEN
- statements
- ELSE IF condition2 THEN
- statements
- ELSE IF condition3 THEN
- statements
- ELSE
- statements
- ENDIF
Meaning
- Conditions are checked top to bottom
- First TRUE condition executes
- Remaining conditions are skipped
Example
- IF score >= 80 THEN
- grade ← “A”
- ELSE IF score >= 60 THEN
- grade ← “B”
- ELSE IF score >= 40 THEN
- grade ← “C”
- ELSE
- grade ← “F”
- ENDIF
Ordering Rule (Very Important)
- Conditions must be ordered:
- From most restrictive to least restrictive
- Wrong order:
- Produces incorrect logic
- Loses marks even if syntax looks correct
Common ELSE IF Errors
- Overlapping conditions in wrong order
- Missing final ELSE when required
- Treating ELSE IF as a separate IF
Nested IF Structures (IF Inside IF)
What Nested Selection Means
- An IF statement placed inside another IF or ELSE block
- Used when:
- A decision depends on a previous decision
Structure
- IF condition1 THEN
- IF condition2 THEN
- statements
- ELSE
- statements
- ENDIF
- IF condition2 THEN
- ENDIF
Example
- IF score >= 50 THEN
- IF attendance >= 75 THEN
- OUTPUT “Pass”
- ELSE
- OUTPUT “Fail due to attendance”
- ENDIF
- IF attendance >= 75 THEN
- ELSE
- OUTPUT “Fail due to marks”
- ENDIF
When To Use Nested IF Instead Of ELSE IF
- Use nested IF when:
- Second condition is relevant only if first condition is TRUE
- Use ELSE IF when:
- Conditions are independent alternatives
Examiner Trap
- Using ELSE IF when nesting is required
- This changes logic and loses marks
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 O Level And IGCSE Computer Science Full Scale Course
Conditions In IF Statements (Cambridge Rules)
Allowed Condition Types
- Relational:
- =, ≠, <, >, ≤, ≥
- Logical:
- AND, OR, NOT
- Boolean variables:
- IF found THEN
Examples
- IF score >= 0 AND score <= 100 THEN
- IF NOT valid THEN
- IF found = TRUE THEN
Best Practice
- Use brackets for clarity:
- IF (score >= 0 AND score <= 100) THEN
- Avoid long ungrouped conditions
Selection Logic And Operator Precedence
- Arithmetic evaluated first
- Then relational
- Then logical
- Always use brackets when mixing AND and OR
Example:
- IF (a > b AND c > d) OR (e = f) THEN
Selection Logic And Validation
Validation Pattern
- INPUT value
- IF value < min OR value > max THEN
- OUTPUT “Invalid”
- ELSE
- process value
- ENDIF
Repeated Validation Pattern
- INPUT value
- WHILE value < min OR value > max DO
- OUTPUT “Invalid”
- INPUT value
- ENDWHILE
Examiner Expectation
- Validation must be:
- Explicit
- Traceable
- Silent rejection loses marks
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 O Level And IGCSE Computer Science Full Scale Course
Selection Logic And Trace Tables
- Each IF condition:
- Affects execution path
- Trace tables must show:
- Which branch executed
- Which variables changed
Trace Safety Rules
- Do not:
- Skip conditions
- Ensure:
- Only one branch executes
- Nested IFs:
- Must be traced step-by-step
Common Selection Mistakes That Lose Marks
- Using assignment instead of comparison
- Missing ENDIF
- Wrong condition order
- Overlapping ELSE IF conditions
- Changing variable values in conditions
- Over-nesting without need
Selection Logic In Section B Modifications
- Section B often asks:
- Add condition
- Restrict condition
- Replace condition
- Use:
- Minimal change approach
- Do not:
- Rewrite entire selection structure
Example Section B Modification
Original:
- IF score >= 50 THEN
- OUTPUT “Pass”
- ELSE
- OUTPUT “Fail”
- ENDIF
Modified:
- IF score >= 50 AND attendance >= 75 THEN
- OUTPUT “Pass”
- ELSE
- OUTPUT “Fail”
- ENDIF
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 O Level And IGCSE Computer Science Full Scale Course
Best-Practice Selection Strategy For Paper 2
- Keep conditions simple
- Use ELSE IF for alternatives
- Use nested IF for dependent checks
- Order conditions carefully
- Always close with ENDIF
- Match logic exactly to pre-release wording
Final Quality Checklist
- All IFs have matching ENDIF
- ELSE IF order is correct
- Nested IF used only when logically required
- Conditions are clear and bracketed
- No assignment used inside conditions
- Output occurs in correct branch only
Final Lock-In Rules
- IF controls logic flow
- ELSE handles remaining cases
- ELSE IF handles ordered alternatives
- Nested IF handles dependent decisions
- Correct selection logic = major Paper 2 marks
