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.

Independent Practice: Grade boundaries

+30 XP

Read an integer mark from 0 to 100. Output A for at least 80, B for at least 60 but below 80, C for at least 40 but below 60, otherwise U.

Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.

solution.pseudo
All-Access Required

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

○ Mark 0
○ Mark 39
○ Mark 40
○ Mark 59
○ Mark 60
○ Mark 79
○ Mark 80
○ Mark 100