Conditional Logic

Nested IF

Sometimes one decision is not enough. A nested IF is an IF statement placed inside another IF — letting you test a second condition only after the first one has been resolved. It is the standard way to handle grade brackets, age ranges and any multi-step decision.

7.1 What is a Nested IF?

  • A nested IF is an IF statement written inside the THEN or ELSE branch of another IF.
  • It lets you test a second condition only after the first one has been resolved.
  • Use it for grading systems, age brackets, or any decision with more than two outcomes.
  • Each IF needs its own ENDIF — count them carefully.

Key idea: A nested IF says "if A is true, then ALSO check B". The inner test only happens once the outer test has decided which branch to enter.

A Real Example — Grade Classifier

DECLARE Mark : INTEGER
INPUT Mark
IF Mark >= 75 THEN
  OUTPUT "Grade A"
ELSE
  IF Mark >= 60 THEN
    OUTPUT "Grade B"
  ELSE
    IF Mark >= 50 THEN
      OUTPUT "Grade C"
    ELSE
      OUTPUT "Grade F"
    ENDIF
  ENDIF
ENDIF

If the user enters 65, the outer condition (Mark >= 75) is FALSE, so we enter the ELSE and check the next IF. 65 >= 60 is TRUE, so "Grade B" is output. The deepest IF (for C and F) is skipped entirely.

7.2 Nested IF Syntax

The general shape of a nested IF (with the inner IF inside the ELSE branch) is:

IF <condition 1> THEN
  <statements if condition 1 is TRUE>
ELSE
  IF <condition 2> THEN
    <statements if condition 1 FALSE and condition 2 TRUE>
  ELSE
    <statements if both conditions are FALSE>
  ENDIF
ENDIF
  • IF <condition> THEN — opens each IF block.
  • ELSE — optional, separates the TRUE and FALSE branches of an IF.
  • ENDIF — closes one IF. Write the innermost ENDIF first, the outermost last.
  • Indent each nesting level by 2 spaces — for readability only, but essential for clarity.

Worked Example: Pass / Merit / Distinction

DECLARE Mark : INTEGER
INPUT Mark
IF Mark >= 75 THEN
  OUTPUT "Distinction"
ELSE
  IF Mark >= 50 THEN
    OUTPUT "Merit"
  ELSE
    OUTPUT "Fail"
  ENDIF
ENDIF

ENDIF count rule: Every IF needs its own ENDIF. If you have 3 nested IFs, you need 3 ENDIFs at the bottom — in the correct order (innermost first, outermost last).

7.3 How Nested IF Works (Tracing)

Tracing a nested IF means evaluating each condition top-to-bottom and writing down TRUE or FALSE next to it. As soon as one THEN (or ELSE) branch runs, the program jumps to after the outermost ENDIF — no other branch can run.

Worked Trace: Score = 75

DECLARE Score : INTEGER
Score <- 75
IF Score >= 90 THEN
  OUTPUT "A"
ELSE
  IF Score >= 70 THEN
    OUTPUT "B"
  ELSE
    OUTPUT "C"
  ENDIF
ENDIF
  • Score = 75. Is 75 >= 90? FALSE → enter the outer ELSE
  • Is 75 >= 70? TRUE → run the inner THEN, OUTPUT "B"
  • The inner ELSE ("C") is skipped; control jumps to after the outer ENDIF

Worked Trace: X = 5

DECLARE X : INTEGER
X <- 5
IF X > 10 THEN
  OUTPUT "A"
ELSE
  IF X > 3 THEN
    OUTPUT "C"
  ELSE
    OUTPUT "D"
  ENDIF
ENDIF

Step-by-step: X is 5. Is 5 > 10? No — so we go into the outer ELSE. Now is 5 > 3? Yes — so we run the inner THEN and OUTPUT "C". The inner ELSE ("D") is never reached.

Exam tip: When tracing nested IFs in an exam, write TRUE or FALSE next to each condition as you go. This makes it almost impossible to lose track of which branch executes.

7.4 Nesting in THEN vs ELSE

You can nest an IF inside the THEN branch (to refine a TRUE case) or inside the ELSE branch (to refine a FALSE case). Both are valid — choose the one that makes the logic clearest.

Nesting in ELSE — Grade Brackets

Grade brackets (A >= 75, B >= 60, C >= 50) are most naturally written by nesting each new test inside the ELSE of the previous one. Each ELSE rules out the previous (higher) bracket, so the next IF only needs to test the next lower threshold.

IF Mark >= 75 THEN
  OUTPUT "A"
ELSE
  IF Mark >= 60 THEN
    OUTPUT "B"
  ELSE
    IF Mark >= 50 THEN
      OUTPUT "C"
    ELSE
      OUTPUT "F"
    ENDIF
  ENDIF
ENDIF

Nesting in THEN — Refining a TRUE Case

If you only want to check a sub-condition when the outer IF is TRUE, nest inside THEN.

IF Age >= 18 THEN
  IF HasLicence THEN
    OUTPUT "Can drive"
  ELSE
    OUTPUT "Needs a licence first"
  ENDIF
ELSE
  OUTPUT "Too young to drive"
ENDIF

Rule of thumb: Nesting in ELSE is ideal for ordered ranges (grade brackets, tax bands). Nesting in THEN is ideal when a sub-test only makes sense once the outer condition is TRUE (age + licence).

7.5 Nested IF vs CASE...OF

Both nested IF and CASE...OF can express multi-way decisions, but they shine in different situations.

FeatureNested IFCASE...OF
Tests how many variables?Many (one per level)One only
Best forRanges and complex conditionsExact value matching
Compound conditions (AND/OR)?Yes — easyNo — labels are exact values
Readability with many branchesGets deeply indentedStays flat — one line per case
End keywordENDIF (one per IF)ENDCASE (one per CASE)

Same Logic, Two Styles

Using Nested IF (ranges)

IF Mark >= 75 THEN
  OUTPUT "A"
ELSE
  IF Mark >= 50 THEN
    OUTPUT "B"
  ELSE
    OUTPUT "C"
  ENDIF
ENDIF

Using CASE...OF (exact values)

CASE OF Grade
  'A' : OUTPUT "Excellent"
  'B' : OUTPUT "Good"
  'C' : OUTPUT "Pass"
  OTHERWISE OUTPUT "Fail"
ENDCASE

Rule of thumb: Use nested IF for ranges and multi-variable logic. Use CASE...OF for one variable with several exact values. Any CASE can be rewritten as nested IF and vice versa.

7.6 Common Mistakes & Key Points

Here are the most common mistakes with nested IFs — and the key points to remember.

Every IF needs exactly one ENDIF — count them as you write.
ENDIFs close innermost first, outermost last.
Only ONE branch of a nested IF runs for a given input.
Nesting in ELSE is best for ordered ranges (grades, tax bands).
Nesting in THEN is best for sub-tests on a TRUE case.
Indentation does not change execution but is essential for readability.
For ranges or multiple variables, nested IF beats CASE...OF.
For one variable against exact values, CASE...OF is cleaner.
When tracing, write TRUE/FALSE next to each condition.
Avoid 5+ levels of nesting — refactor with CASE, AND/OR, or subroutines.

Watch out: The single most common mistake is a missing or mismatched ENDIF. For every IF you open, count one ENDIF — and make sure they close in the right order (innermost first).

Exam tip: If a question shows thresholds (>= 75, >= 60, >= 50), reach for nested IF. If it shows named distinct values (1, 2, 3, "A", "B"), reach for CASE...OF.

Question Bank

Test yourself — 12 questions including True/False and code-tracing problems.

0/12 answered

Question 1Multiple Choice

What is a nested IF statement?

Question 2True / False

Each IF in a nested structure needs its own ENDIF.

Question 3Multiple Choice

DECLARE Score : INTEGER
Score <- 75
IF Score >= 90 THEN
  OUTPUT "A"
ELSE
  IF Score >= 70 THEN
    OUTPUT "B"
  ELSE
    OUTPUT "C"
  ENDIF
ENDIF

Question 4True / False

Only ONE branch of a properly nested IF ever runs for a given input.

Question 5Multiple Choice

How many ENDIF statements are needed for a 3-level nested IF?

Question 6True / False

A nested IF can be placed inside the THEN branch OR the ELSE branch.

Question 7Multiple Choice

When is nested IF usually preferred over CASE...OF?

Question 8Multiple Choice

DECLARE Mark : INTEGER
Mark <- 55
IF Mark >= 75 THEN
  OUTPUT "A"
ELSE
  IF Mark >= 50 THEN
    OUTPUT "B"
  ELSE
    OUTPUT "C"
  ENDIF
ENDIF

Question 9True / False

Forgetting one ENDIF in a nested IF causes a syntax error.

Question 10Multiple Choice

Where should you nest an IF if you want to refine the TRUE case of the outer IF?

Question 11True / False

A nested IF can test multiple different variables (one per level).

Question 12Multiple Choice

DECLARE X : INTEGER
X <- 5
IF X > 10 THEN
  OUTPUT "A"
ELSE
  IF X > 3 THEN
    OUTPUT "C"
  ELSE
    OUTPUT "D"
  ENDIF
ENDIF

Answer all 12 questions to enable submission.