Code Guide: IF, ELSE IF And ELSE Decision-Making Programs (Copy)
IF, ELSE IF And ELSE Decision-Making Programs
Core Decision-Making Rules (Exam-Critical)
- IF is used for a single condition
- ELSE IF is used when multiple conditions are checked in order
- ELSE handles all remaining cases
- Conditions are checked top to bottom
- Once a condition is TRUE:
- Remaining ELSE IF conditions are skipped
- Every decision must use:
- Correct comparison operators
- Correct logical operators
- Clear, non-overlapping conditions
Comparison And Logical Operators (Quick Recall)
Comparison Operators
| Meaning | Operator | Example |
|---|---|---|
| Equal | = | IF x = 5 THEN |
| Not equal | <> | IF choice <> "Y" THEN |
| Greater than | > | IF mark > 70 THEN |
| Less than | < | IF temp < 0 THEN |
| Greater or equal | >= | IF score >= 50 THEN |
| Less or equal | <= | IF age <= 18 THEN |
Logical Operators
| Operator | Meaning | Example |
|---|---|---|
| AND | Both true | IF x >= 1 AND x <= 10 THEN |
| OR | Either true | IF opt = 1 OR opt = 2 THEN |
| NOT | Negation | IF NOT valid THEN |
Example 1: Simple IF Decision
Task
- Input a number
- Output “Positive” if number > 0
INPUT num
IF num > 0 THEN
OUTPUT "Positive"
ENDIF
Revision Point
- IF alone is valid when no alternative action is required
Example 2: IF–ELSE Decision
Task
- Input mark
- Output Pass or Fail (pass mark = 50)
INPUT mark
IF mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Revision Point
- ELSE covers all remaining cases
- No condition is written with ELSE
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
Example 3: IF–ELSE IF–ELSE (Multiple Conditions)
Task
- Input score
- Output grade:
- A for ≥ 80
- B for ≥ 65
- C for ≥ 50
- F otherwise
INPUT score
IF score >= 80 THEN
OUTPUT "A"
ELSE IF score >= 65 THEN
OUTPUT "B"
ELSE IF score >= 50 THEN
OUTPUT "C"
ELSE
OUTPUT "F"
ENDIF
Revision Point
- Conditions must be ordered highest to lowest
- Prevents unreachable branches
Example 4: Age Category Program
Task
- Input age
- Categorise user
INPUT age
IF age < 13 THEN
OUTPUT "Child"
ELSE IF age < 20 THEN
OUTPUT "Teenager"
ELSE IF age < 60 THEN
OUTPUT "Adult"
ELSE
OUTPUT "Senior"
ENDIF
Revision Point
- Upper bounds handled implicitly
- Cleaner than repeating ranges
Example 5: Range Validation Using IF
Task
- Input temperature
- Check if valid (0–100)
INPUT temp
IF temp >= 0 AND temp <= 100 THEN
OUTPUT "Valid"
ELSE
OUTPUT "Invalid"
ENDIF
Revision Point
- Range checks always use AND
- OR would break 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
Example 6: Nested IF Statements
Task
- Input username and password
- Validate login
INPUT username
INPUT password
IF username = "admin" THEN
IF password = "1234" THEN
OUTPUT "Access Granted"
ELSE
OUTPUT "Wrong Password"
ENDIF
ELSE
OUTPUT "Unknown User"
ENDIF
Revision Point
- Nested IF used when second condition depends on first
- Indentation is crucial for clarity
Example 7: Using OR In Decisions
Task
- Input option
- Accept option 1 or 2
INPUT option
IF option = 1 OR option = 2 THEN
OUTPUT "Valid Option"
ELSE
OUTPUT "Invalid Option"
ENDIF
Revision Point
- OR allows multiple acceptable values
- Do not repeat IF blocks unnecessarily
Example 8: Using NOT Operator
Task
- Input valid flag
- Output status
INPUT valid
IF NOT valid THEN
OUTPUT "Invalid Data"
ELSE
OUTPUT "Valid Data"
ENDIF
Revision Point
- NOT reverses the condition
- Improves readability in some cases
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
Example 9: Decision With Calculations
Task
- Input basic salary
- Add allowance based on salary
INPUT salary
IF salary < 30000 THEN
allowance ← salary * 0.10
ELSE
allowance ← salary * 0.05
ENDIF
totalPay ← salary + allowance
OUTPUT totalPay
Revision Point
- Calculations often appear inside IF blocks
- Output usually comes after decision
Example 10: Preventing Division By Zero
Task
- Input two numbers
- Divide only if denominator ≠ 0
INPUT a
INPUT b
IF b <> 0 THEN
result ← a / b
OUTPUT result
ELSE
OUTPUT "Division Not Possible"
ENDIF
Revision Point
- Always guard division operations
- Common examiner trap
High-Frequency Decision Logic Mistakes
Mistake 1: Overlapping Conditions
- Wrong:
IF mark >= 50 THEN
ELSE IF mark >= 80 THEN
- Correct:
- Higher condition first
Mistake 2: Impossible Conditions
- Wrong:
IF x < 10 AND x > 20 THEN
- Condition can never be true
Mistake 3: Using OR Instead Of AND In Ranges
- Wrong:
IF age >= 18 OR age <= 60 THEN
- Always TRUE
Mistake 4: Missing ELSE
- Leaves undefined output paths
- Examiner may deduct 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 AS Level Computer Science Full Scale Course
Decision-Making Templates (Exam-Ready)
Template 1: Pass / Fail
IF mark >= passMark THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Template 2: Multi-Grade
IF value >= high THEN
ELSE IF value >= mid THEN
ELSE
ENDIF
Template 3: Range Validation
IF value >= min AND value <= max THEN
ELSE
ENDIF
Template 4: Safe Division
IF divisor <> 0 THEN
result ← a / divisor
ENDIF
Final Decision-Making Checklist
- Conditions ordered correctly?
- No overlapping or impossible logic?
- Correct operators used?
- ELSE included where required?
- Output reachable in all cases?
If yes — logic is examiner-proof.
