Code Guide: Nested Selection Programs With Multiple Conditions (Copy)
Nested Selection Programs With Multiple Conditions
What Nested Selection Means
- Nested selection is when an IF statement is placed inside another IF / ELSE block
- Used when:
- The second decision depends on the first decision being true (or false)
- You need to apply multiple rules in layers
- Typical exam wording that signals nesting:
- “If … then check whether …”
- “Only if …, then …”
- “If eligible, then decide which type…”
When To Use Nested IF (Vs ELSE IF Chains)
| Situation | Best Structure | Why |
|---|---|---|
| Second decision only matters after first is TRUE | Nested IF | Cleaner + matches wording |
| Multiple independent ranges (grades, bands) | ELSE IF chain | Ordered conditions are clearer |
| Validation + categorisation | Nested IF | First validate, then categorise |
| Two separate factors both matter (e.g., age + membership) | Nested IF | Layered logic is natural |
Core Rules (Exam-Critical)
- A nested IF must be:
- Correctly indented
- Clearly inside the correct branch
- Logically reachable
- Conditions must be:
- Non-overlapping (or ordered properly)
- Reachable
- Written using correct AND / OR
- Always think:
- “What must be true for the inner IF to even run?”
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
Pattern 1: Eligibility Check Then Category (Most Common Exam Pattern)
Scenario
- A person is eligible only if age ≥ 18
- If eligible, check whether they are a senior (age ≥ 60)
Pseudocode
INPUT age
IF age >= 18 THEN
OUTPUT "Eligible"
IF age >= 60 THEN
OUTPUT "Senior Eligible"
ELSE
OUTPUT "Adult Eligible"
ENDIF
ELSE
OUTPUT "Not Eligible"
ENDIF
Why This Is Nested (And Correct)
- The “senior vs adult” decision only makes sense if the person is already eligible
Pattern 2: Validation Then Processing (High Mark Saver)
Scenario
- Input mark (must be 0 to 100)
- If valid, assign grade
- If invalid, output error
Pseudocode
INPUT mark
IF mark >= 0 AND mark <= 100 THEN
IF mark >= 80 THEN
OUTPUT "A"
ELSEIF mark >= 65 THEN
OUTPUT "B"
ELSEIF mark >= 50 THEN
OUTPUT "C"
ELSE
OUTPUT "F"
ENDIF
ELSE
OUTPUT "Invalid Mark"
ENDIF
Why This Scores Well
- Outer IF protects the grading logic
- Inner chain handles categorisation only after validation
Pattern 3: Two-Factor Decision (Membership + Spending)
Scenario
- Discount depends on:
- Membership status
- Total bill value
Pseudocode
INPUT member
INPUT bill
IF member = TRUE THEN
IF bill >= 5000 THEN
discount ← bill * 0.20
ELSE
discount ← bill * 0.10
ENDIF
ELSE
IF bill >= 5000 THEN
discount ← bill * 0.05
ELSE
discount ← 0
ENDIF
ENDIF
finalBill ← bill - discount
OUTPUT finalBill
Exam Takeaway
- Nested selection handles combinations cleanly without messy multi-condition IF lines
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
Pattern 4: Multiple Conditions Inside One Decision (AND / OR Inside Nested IF)
Scenario
- User can login only if username is correct
- If username correct, password must be correct AND account must be active
Pseudocode
INPUT username
INPUT password
INPUT active
IF username = "admin" THEN
IF password = "1234" AND active = TRUE THEN
OUTPUT "Login Success"
ELSE
OUTPUT "Login Failed"
ENDIF
ELSE
OUTPUT "Unknown User"
ENDIF
Key Rule
- Put the most “gating” check first (username)
- Only then check more detailed conditions (password + active)
Pattern 5: Avoiding Impossible Conditions Using Nesting
Common Student Mistake (Bad Logic)
- Trying to force everything into one condition:
IF age >= 18 AND age < 13 THEN(impossible)
Correct Nested Logic
- First decide age band, then sub-band:
INPUT age
IF age < 13 THEN
OUTPUT "Child"
ELSE
IF age < 20 THEN
OUTPUT "Teen"
ELSE
OUTPUT "Adult"
ENDIF
ENDIF
Pattern 6: Nested Selection With Flags (Common With Search / Detection)
Scenario
- Scan 10 inputs
- If any value is negative, set error flag
- If error flag TRUE, decide severity
Pseudocode
error ← FALSE
FOR i ← 1 TO 10
INPUT num
IF num < 0 THEN
error ← TRUE
ENDIF
NEXT i
IF error = TRUE THEN
INPUT critical
IF critical = TRUE THEN
OUTPUT "Critical Error"
ELSE
OUTPUT "Minor Error"
ENDIF
ELSE
OUTPUT "No Error"
ENDIF
Why Nested
- The severity check only runs if an error exists
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 Templates (Copy-Paste Safe)
Template A: Eligibility Then Category
IF eligibleCondition THEN
IF categoryCondition THEN
OUTPUT "Category 1"
ELSE
OUTPUT "Category 2"
ENDIF
ELSE
OUTPUT "Not Eligible"
ENDIF
Template B: Validation Then Processing
IF validCondition THEN
IF mainCondition THEN
OUTPUT "Case 1"
ELSEIF otherCondition THEN
OUTPUT "Case 2"
ELSE
OUTPUT "Case 3"
ENDIF
ELSE
OUTPUT "Invalid Input"
ENDIF
Template C: Two-Factor Combination (4 outcomes)
IF factorA THEN
IF factorB THEN
OUTPUT "A and B"
ELSE
OUTPUT "A only"
ENDIF
ELSE
IF factorB THEN
OUTPUT "B only"
ELSE
OUTPUT "Neither"
ENDIF
ENDIF
Trace Table Practice (Nested IF)
Pseudocode
INPUT age
IF age >= 18 THEN
IF age >= 60 THEN
OUTPUT "Senior"
ELSE
OUTPUT "Adult"
ENDIF
ELSE
OUTPUT "Minor"
ENDIF
Test Values + Outputs
| age | Outer IF (age >= 18) | Inner IF (age >= 60) | Output |
|---|---|---|---|
| 12 | FALSE | — | Minor |
| 30 | TRUE | FALSE | Adult |
| 70 | TRUE | TRUE | Senior |
Common Examiner-Reported Mistakes (And Fixes)
Mistake 1: Wrong Nesting Location
- Bug pattern:
- Inner IF accidentally placed outside outer IF
- Fix:
- Ensure inner IF is indented and inside the correct branch
Mistake 2: Unreachable Inner Conditions
- Example:
- Outer IF:
mark >= 80 - Inner IF:
mark >= 50(always true inside outer)
- Outer IF:
- Fix:
- Inner checks must add new information, not repeat weaker conditions
Mistake 3: ELSE IF Ordering Incorrect
- Example (wrong order):
- Check
mark >= 50beforemark >= 80
- Check
- Fix:
- Order from highest threshold to lowest
Mistake 4: Wrong AND / OR
- Invalid range check:
mark < 0 AND mark > 100
- Correct:
- Invalid condition uses OR
- Valid condition uses AND:
mark >= 0 AND mark <= 100
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
High-Yield Practice Mini Programs (Nested, Multi-Condition)
Program 1: Shipping Cost Based On Weight And Location
INPUT weight
INPUT international
IF weight <= 0 THEN
OUTPUT "Invalid Weight"
ELSE
IF international = TRUE THEN
IF weight <= 5 THEN
cost ← 2000
ELSE
cost ← 4000
ENDIF
ELSE
IF weight <= 5 THEN
cost ← 800
ELSE
cost ← 1500
ENDIF
ENDIF
OUTPUT cost
ENDIF
Program 2: Exam Result With Distinction Rule
- Pass mark: 50
- Distinction: 80+
- If invalid mark, output error
INPUT mark
IF mark >= 0 AND mark <= 100 THEN
IF mark >= 80 THEN
OUTPUT "Distinction"
ELSE
IF mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDIF
ELSE
OUTPUT "Invalid Mark"
ENDIF
Program 3: Username Check Then Role-Based Access
INPUT username
INPUT role
IF username = "admin" THEN
IF role = "A" THEN
OUTPUT "Full Access"
ELSEIF role = "B" THEN
OUTPUT "Limited Access"
ELSE
OUTPUT "No Access"
ENDIF
ELSE
OUTPUT "Unknown User"
ENDIF
Final Checklist Before Writing Nested Selection In Exams
- Did you choose nesting because the inner decision depends on the outer decision?
- Are all conditions reachable and ordered correctly?
- Did you avoid impossible conditions?
- Did you cover all outcomes with ELSE where needed?
- Is indentation clean and unambiguous?
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
