Conditional Logic

IF...THEN...ELSE

Decision-making is what makes a program smart. The IF...THEN...ELSE statement lets your code choose between different actions based on whether a condition is TRUE or FALSE — the foundation of all conditional logic in CIE pseudocode.

5.1 Understanding IF Statements

An IF statement is a control structure that allows a program to make a decision. It checks a condition — an expression that is either TRUE or FALSE — and runs different code depending on the result. This is how programs decide what to do next.

Key idea: A condition is always evaluated as either TRUE or FALSE — never maybe, never both. The IF statement simply asks "is this TRUE?" and branches accordingly.

The Basic Structure

IF <condition> THEN
  <statements to run if TRUE>
ENDIF

A Real Example

DECLARE Age : INTEGER
INPUT Age
IF Age >= 18 THEN
  OUTPUT "You can vote."
ENDIF

In this example, the program asks the user for their age. If the value entered is 18 or more, the condition Age >= 18 is TRUE and the message "You can vote." is displayed. If the age is less than 18, the condition is FALSE and the program simply skips the OUTPUT line — nothing is printed.

Comparison Operators Used in Conditions

OperatorMeaningExample (TRUE when...)
=Equal toX = 5 — X is exactly 5
!=Not equal toX != 5 — X is anything other than 5
>Greater thanX > 5 — X is bigger than 5
<Less thanX < 5 — X is smaller than 5
>=Greater than or equal toX >= 5 — X is 5 or bigger
<=Less than or equal toX <= 5 — X is 5 or smaller

Remember: In CIE pseudocode the equality operator is a single = sign (not == like in Python or Java). And the assignment operator is <-.

5.2 The THEN Clause

The THEN clause contains the statements that run when the condition is TRUE. In CIE pseudocode, the THEN keyword must follow the condition on the same line, and the statements belonging to it are indented underneath.

Syntax

IF <condition> THEN
  <statement 1>
  <statement 2>
  ...
ENDIF

You can put as many statements as you like inside the THEN clause. All of them run only if the condition is TRUE. If the condition is FALSE, the whole block is skipped and the program jumps to the line after ENDIF.

Example: Multiple Statements in THEN

DECLARE Score : INTEGER
INPUT Score
IF Score >= 50 THEN
  OUTPUT "Congratulations!"
  OUTPUT "You passed the exam."
  OUTPUT "Your score was " + Score
ENDIF

If the user enters 72, all three OUTPUT statements execute. If they enter 30, none of them run — the program flow jumps straight from the IF line to after ENDIF.

Indentation tip: Always indent the statements inside THEN by 2 spaces. This does not change how the program runs, but it makes it crystal clear which lines belong to the THEN branch — and examiners love well-laid-out code.

5.3 The ELSE Clause

The ELSE clause is OPTIONAL. It provides an alternative set of statements that run when the IF condition is FALSE. Together, IF/THEN/ELSE lets you handle both possible outcomes of a decision.

Syntax

IF <condition> THEN
  <statements for TRUE>
ELSE
  <statements for FALSE>
ENDIF

Example: Pass or Fail

DECLARE Mark : INTEGER
INPUT Mark
IF Mark >= 50 THEN
  OUTPUT "Pass"
ELSE
  OUTPUT "Fail"
ENDIF

Now the program always outputs something. If Mark is 60, the condition is TRUE so "Pass" is printed. If Mark is 40, the condition is FALSE so the ELSE branch runs and "Fail" is printed instead.

IF...THEN...ELSE vs IF...THEN

FeatureIF...THEN onlyIF...THEN...ELSE
Has an ELSE branch?NoYes
When condition is TRUERuns THEN blockRuns THEN block
When condition is FALSESkips (nothing runs)Runs ELSE block
Best used whenAction is optionalTwo distinct outcomes

Rule of thumb: Use ELSE whenever you want the program to do something in both the TRUE and FALSE cases. Without ELSE, the program silently does nothing when the condition fails.

5.4 Nested IF Statements

Sometimes one decision is not enough. A nested IF is an IF statement placed inside the THEN or ELSE branch of another IF. It lets you test a second condition only after the first one has been resolved — perfect for grading systems, age brackets, or any time you have more than two possible outcomes.

Structure of a Nested IF

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

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

Notice how each ENDIF closes the innermost IF first. Trace the logic with Mark = 65:

  • 65 >= 75? FALSE → enter the ELSE branch
  • 65 >= 60? TRUE → run inner THEN, OUTPUT "Grade B"
  • The remaining nested IFs are skipped

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

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.

5.5 Key Points Summary

Let's recap the most important ideas about IF...THEN...ELSE before you tackle the Question Bank.

IF statements let a program make decisions based on a TRUE/FALSE condition.
Every IF must begin with IF, include THEN, and end with ENDIF.
The THEN block runs only when the condition is TRUE.
The ELSE block (optional) runs when the condition is FALSE.
Without ELSE, the program does nothing if the condition is FALSE.
Comparison operators: =, !=, >, <, >=, <= (note: single = for equality).
A nested IF is an IF inside another IF — used for multi-way decisions.
Each IF needs its own ENDIF — count them carefully when nesting.

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

Question Bank

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

0/12 answered

Question 1Multiple Choice

What is the purpose of an IF...THEN...ELSE statement in pseudocode?

Question 2True / False

In CIE pseudocode, every IF statement must be closed with the ENDIF keyword.

Question 3Multiple Choice

Which of the following is the correct CIE syntax for an IF statement?

Question 4True / False

The ELSE clause is mandatory in every IF statement.

Question 5Multiple Choice

What is a "nested IF" statement?

Question 6Multiple Choice

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

Question 7Multiple Choice

Which comparison operator means "not equal to" in CIE pseudocode?

Question 8True / False

In a nested IF, the inner IF can have its own ELSE clause.

Question 9Multiple Choice

IF Marks >= 50 THEN
  OUTPUT "Pass"
ELSE
  OUTPUT "Fail"
ENDIF

Question 10Multiple Choice

Why is indentation important in IF statements?

Question 11True / False

An IF statement without an ELSE clause will cause an error if the condition is FALSE.

Question 12Multiple Choice

IF Temperature < 15 THEN
  OUTPUT "Cold"
ELSE
  IF Temperature >= 25 THEN
    OUTPUT "Hot"
  ELSE
    OUTPUT "Mild"
  ENDIF
ENDIF

Answer all 12 questions to enable submission.