Cost And Management Accounting Formats (AS Level): Marginal Costing Contribution Statement Format (Copy)
Nested Selection Logic With Worked Examples
Purpose Of Nested Selection
- Nested selection is used when:
- One decision depends on the result of another
- Conditions are hierarchical, not independent
- Examiner checks:
- Logical dependency
- Correct nesting and indentation
- Correct ENDIF matching
What Nested Selection Means
- An IF statement inside another IF or ELSE
- Inner condition is evaluated only if outer condition allows it
- Used when:
- Access control
- Validation steps
- Tiered decision-making
Basic Nested IF Structure
IF condition1 THEN
IF condition2 THEN
statements
ELSE
statements
ENDIF
ELSE
statements
ENDIF
- Inner IF only runs when outer IF condition is TRUE
- Each IF must have its own ENDIF
Worked Example 1: Age And ID Check
| Condition | Meaning |
|---|---|
| Age >= 18 | Legal age |
| HasID = TRUE | Identity verified |
IF Age >= 18 THEN
IF HasID = TRUE THEN
OUTPUT "Access Granted"
ELSE
OUTPUT "ID Required"
ENDIF
ELSE
OUTPUT "Underage"
ENDIF
Execution Logic
- If Age < 18 → “Underage”
- If Age ≥ 18 AND HasID = TRUE → “Access Granted”
- If Age ≥ 18 AND HasID = FALSE → “ID Required”
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
Worked Example 2: Login Validation System
| Variable | Data Type |
|---|---|
| Username | STRING |
| Password | STRING |
| Locked | BOOLEAN |
IF Locked = FALSE THEN
IF Username = "Admin" THEN
IF Password = "1234" THEN
OUTPUT "Login Successful"
ELSE
OUTPUT "Incorrect Password"
ENDIF
ELSE
OUTPUT "Unknown User"
ENDIF
ELSE
OUTPUT "Account Locked"
ENDIF
Why Nested IF Is Correct Here
- Password check only makes sense if:
- Account is not locked
- Username is correct
- Prevents unnecessary checks
Nested IF Vs ELSE IF (Critical Distinction)
| Feature | Nested IF | ELSE IF |
|---|---|---|
| Dependency | Yes | No |
| Evaluation | Conditional | Sequential |
| Best for | Step-by-step checks | Grading / ranges |
Worked Example 3: Exam Entry Eligibility
| Requirement | Condition |
|---|---|
| Fee paid | FeePaid = TRUE |
| Attendance | Attendance >= 75 |
IF FeePaid = TRUE THEN
IF Attendance >= 75 THEN
OUTPUT "Eligible For Exam"
ELSE
OUTPUT "Attendance Short"
ENDIF
ELSE
OUTPUT "Fee Not Paid"
ENDIF
- Attendance only checked if fee is paid
- Logical dependency clearly shown
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
Deep Nesting (Three-Level Example)
IF Age >= 18 THEN
IF HasTicket = TRUE THEN
IF HasSeat = TRUE THEN
OUTPUT "Entry Allowed"
ELSE
OUTPUT "Seat Not Assigned"
ENDIF
ELSE
OUTPUT "Ticket Required"
ENDIF
ELSE
OUTPUT "Age Restriction"
ENDIF
Execution Flow
- Age < 18 → Stop immediately
- Age ≥ 18 → Check ticket
- Ticket present → Check seat
- Seat present → Entry allowed
Indentation Rules (Examiner Focus)
| Rule | Explanation |
|---|---|
| Each level indented | Shows hierarchy |
| ENDIF aligned | Matches IF |
| No skipped indentation | Prevents confusion |
Nested IF Inside ELSE Block
IF Score >= 50 THEN
OUTPUT "Pass"
ELSE
IF Score >= 40 THEN
OUTPUT "Compartment"
ELSE
OUTPUT "Fail"
ENDIF
ENDIF
- ELSE can contain another IF
- Common grading adjustment 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
Nested Selection Inside Loops (Very Common)
FOR i ← 1 TO 5
IF Marks[i] >= 50 THEN
IF Marks[i] >= 80 THEN
OUTPUT "Distinction"
ELSE
OUTPUT "Pass"
ENDIF
ELSE
OUTPUT "Fail"
ENDIF
ENDFOR
| Mark Range | Output |
|---|---|
| ≥ 80 | Distinction |
| 50–79 | Pass |
| < 50 | Fail |
Simplifying Nested Logic (When Possible)
Over-Nested (Avoid)
IF A = TRUE THEN
IF B = TRUE THEN
OUTPUT "Yes"
ENDIF
ENDIF
Cleaner (Same Logic)
IF A = TRUE AND B = TRUE THEN
OUTPUT "Yes"
ENDIF
- Only simplify if dependency is not required
- Examiner prefers clarity over cleverness
Common Student Mistakes In Nested Selection
| Mistake | Why Wrong |
|---|---|
| Missing ENDIF | Breaks structure |
| Wrong indentation | Logic unclear |
| Using ELSE IF instead | Changes logic |
| Checking inner condition first | Illogical flow |
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
Examiner-Safe Nested Selection Template
IF outerCondition THEN
IF innerCondition THEN
statements
ELSE
statements
ENDIF
ELSE
statements
ENDIF
- Clear dependency
- Easy to trace
- Maximum structure marks secured
