Selection & Decision Making (IF ... THEN ... ELSE)

Selection is a fundamental programming construct that allows an algorithm to choose alternative execution paths based on whether a conditional expression evaluates to TRUE or FALSE.


1. Single and Two-Way Selection

Syntax:

IF <Condition> THEN
    // Code executed when Condition is TRUE
ELSE
    // Code executed when Condition is FALSE
ENDIF
  • The ELSE clause is optional if no action is required when the condition is false.
  • Every IF statement must terminate with a matching ENDIF.

2. Multi-Way Nested Selection

When selecting between three or more outcomes, IF statements can be nested inside the ELSE branch:

IF Mark >= 80 THEN
    Grade <- "A"
ELSE
    IF Mark >= 70 THEN
        Grade <- "B"
    ELSE
        IF Mark >= 60 THEN
            Grade <- "C"
        ELSE
            Grade <- "U"
        ENDIF
    ENDIF
ENDIF

Notice that three opened IF statements require three matching ENDIF statements at the close.


3. Logical Operators in Conditions

Compound conditions combine multiple relational comparisons using Boolean operators:

Operator

Meaning

Example

AND

Evaluates to TRUE only if both operands are true.

(Age >= 18) AND (HasDrivingLicense = TRUE)

OR

Evaluates to TRUE if at least one operand is true.

(IsWeekend = TRUE) OR (IsHoliday = TRUE)

NOT

Inverts the logical state (TRUE becomes FALSE, FALSE becomes TRUE).

NOT (AccountLocked = TRUE)

AI Socratic Tutor

All-Access
Sign in or create an account. AI Tutor is an All-Access benefit.
Sign Up

Need guidance? Ask a question or use the hint below.

AI Socratic Tutor is an All-Access Benefit

Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.

You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.

Exam-Style Question: Complete Nested IF Branching

+35 XP

Complete the missing keywords in this nested selection construct:

PSEUDOCODE
IF Temperature > 30 THEN
    Status <- "Hot"
[Space 1]
    IF Temperature < 15 THEN
        Status <- "Cold"
    ELSE
        Status <- "Moderate"
    [Space 2]
[Space 3]
Word Bank / Available Cards (Drag into spaces or tap to place)

All-Access Required

Interactive exercises for Module 2 and beyond require an active plan.