1. IF, ELSE and nested IF
An IF statement runs its lines only when its condition is TRUE. It is written IF...THEN...ENDIF. An `ELSE` clause holds the lines that run when the condition is FALSE.
``` IF <condition> THEN <statements> ELSE <statements> ENDIF ```
A nested IF is an IF statement inside another one. Each IF needs its own `ENDIF`. Join conditions with AND, `OR` and NOT, not with a comma.
Examiner insight
Worked example 14 marks
Examine the following pseudocode.
``` IF A = TRUE THEN IF B = TRUE THEN IF C = TRUE THEN CALL Sub1() ELSE CALL Sub2() ENDIF ENDIF ELSE IF B = TRUE THEN IF C = TRUE THEN CALL Sub4() ELSE CALL Sub3() ENDIF ELSE IF C = FALSE THEN CALL Sub3() ELSE CALL Sub4() ENDIF ENDIF ENDIF ```
A programmer wants to re-write the pseudocode as four separate IF...THEN...ENDIF statements, each containing a single CALL statement. This involves writing a single, simplified logic expression as the condition in each statement.
Write the amended pseudocode.
- 1
``` IF A AND B AND C THEN CALL Sub1() ENDIF ``` [1]
- 2
``` IF (A AND B) AND NOT C THEN CALL Sub2() ENDIF ``` [1]
- 3
``` IF (NOT A) AND (NOT C) THEN CALL Sub3() ENDIF ``` [1]
- 4
``` IF (NOT A) AND C THEN CALL Sub4() ENDIF ``` [1]