Learning Objectives
  • Write single-branch and two-branch IF statements.
  • Use conditions to choose which statements execute.
  • Construct multi-way decisions using ELSE and nested IF where suitable.
  • Trace selection accurately.
Key Terms
Selection
Choosing which statement or block to execute according to a condition.
IF statement
A selection structure that executes code when a Boolean condition is true.
ELSE clause
The alternative branch executed when the IF condition is false.
Branch
One possible path through a selection structure.
Nested IF
An IF statement contained inside another selection or loop.
Summary diagram
Summary Of The Main Ideas In This Lesson
Single-Branch IF

A single-branch IF executes its statements only when the condition is true. If the condition is false, control continues after ENDIF. This is useful for optional actions such as applying a bonus or displaying a warning.

The condition must evaluate to BOOLEAN. It may be a direct Boolean variable or a relational and logical expression.

Indentation shows which statements belong to the branch. Although indentation does not replace keywords, it makes the structure much easier to follow.

IF Temperature < 0
  THEN
  OUTPUT "Freezing conditions"
ENDIF
IF With ELSE

An ELSE clause provides exactly one alternative when the condition is false. One branch executes, never both.

Use a two-branch IF when the program must choose between two mutually exclusive outcomes, such as pass/fail or accepted/rejected.

Statements after ENDIF execute regardless of which branch was selected.

IF Mark >= 50
  THEN
  OUTPUT "Pass"
  ELSE
  OUTPUT "Fail"
ENDIF
More Than One Statement In A Branch

A branch can contain several statements. For example, a discount branch may calculate a reduced price and output a message. Every statement in the block should be indented consistently.

Do not close the IF after the first statement if additional statements belong to the same branch. ENDIF marks the end of the whole structure.

Ranges And Ordered Decisions

Grades and bands often require several conditions. The order matters. If the highest threshold is tested first, lower thresholds can be tested in later ELSE branches without repeating the upper limit.

For example, if Mark >= 80 then grade A; else if Mark >= 70 then grade B. The second test is reached only when Mark is below 80, so it correctly represents 70 to 79.

Always cover invalid input separately when the scenario requires validation. A grade algorithm should not silently assign a grade to -5 or 150.

Trace A Two-Branch IF
Mark Condition Mark >= 50 Executed Output
78 TRUE Pass
50 TRUE Pass
49 FALSE Fail
Worked Examples
Delivery Charge

Question: Delivery is free for orders of at least 50; otherwise it costs 5. Write the selection.

  1. Compare OrderTotal with 50 using >=.
  2. Assign 0 in the true branch.
  3. Assign 5 in the ELSE branch.

Answer: IF OrderTotal >= 50 THEN Delivery <- 0 ELSE Delivery <- 5 ENDIF.

Largest Of Two Values

Question: Output the larger of A and B, treating equal values as B.

  1. Test whether A > B.
  2. Output A when true.
  3. Otherwise output B.

Answer: IF A > B THEN OUTPUT A ELSE OUTPUT B ENDIF.

Examination Guidance
  • Use THEN, ELSE and ENDIF in the Cambridge style.
  • Check whether boundaries are inclusive.
  • Explain which branch executes for a particular value.
  • Use indentation to make nested structures clear.
  • Validate before applying decisions when input may be invalid.
Common Mistakes
  • Using two separate IF statements when exactly one branch should execute.
  • Testing thresholds in the wrong order.
  • Forgetting ENDIF.
  • Using assignment instead of comparison in the condition.
  • Assuming ELSE has its own condition.
Knowledge Check

1. What is selection?

Answer: Choosing a path according to a condition.

2. When does ELSE execute?

Answer: When the IF condition is false.

3. Can both branches of one IF-ELSE execute?

Answer: No.

4. What happens after ENDIF?

Answer: Execution continues with the next statement.

5. Why does threshold order matter?

Answer: An earlier true condition prevents later branches from being tested.