CIE IGCSE | 8.1.4B.1 Selection with IF, THEN and ELSE

Lesson objective

Use conditions to select instructions, trace IF and IF/ELSE statements, combine Boolean tests and construct nested decisions with clear boundaries.

Learn

8.1.4B.1 | SELECTION WITH IF, THEN AND ELSE

01 | WHEN A PROGRAM NEEDS TO CHOOSE

A sequence performs instructions in their specified order. But a program often needs different actions for different circumstances. A quiz should display a different message when a score reaches the pass mark. A booking system should behave differently when no places remain.

Selection chooses which instructions to execute according to a condition. It lets the algorithm respond to its data instead of following exactly the same path for every input.

An IF statement makes that choice explicit. The program evaluates a condition, follows the appropriate instructions and then continues.

The program is not guessing what you intended. Your comparison, branch contents and placement must describe the rule precisely.

02 | A CONDITION HAS A BOOLEAN RESULT

A condition is an expression evaluated as TRUE or FALSE. For example, Score >= 50 asks whether the current score is at least fifty.

Assume valid whole-number scores from zero to one hundred
ScoreScore >= 50Meaning
49FALSEBelow the pass mark
50TRUEExactly the pass mark is included
75TRUEAbove the pass mark

The condition checks Score; it does not change it. Assignment uses the arrow, while equality comparisons use equals.

“At least fifty” includes fifty, so use >=. “More than fifty” excludes fifty, so use >. Small differences in wording can change the branch at a boundary.

03 | IF AND THEN WITHOUT ELSE

An IF statement does not have to include an alternative branch. Use it when an action is needed only if a condition holds.

DECLARE Score : INTEGER
Score ← 75
IF Score >= 50
  THEN
    OUTPUT "Pass"
ENDIF
OUTPUT "Review complete"

IF introduces the condition. THEN introduces the instructions executed when it is true. ENDIF closes the selection.

At seventy-five the algorithm outputs Pass, then Review complete. At forty-nine it skips the Pass output and still outputs Review complete.

A false condition does not stop the entire program. Without ELSE, the selected body is skipped and execution continues after ENDIF.

04 | ADD ELSE FOR AN ALTERNATIVE

When the task needs one outcome for true and another for false, use IF with ELSE.

IF Score >= 50
  THEN
    OUTPUT "Pass"
  ELSE
    OUTPUT "Not yet passed"
ENDIF
OUTPUT "Review complete"

Assume Score is declared and has a valid value. A true condition executes the THEN branch and skips ELSE. A false condition executes ELSE and skips THEN.

Within this IF/ELSE statement, one branch runs for a particular evaluation, not both. Afterwards both paths rejoin and Review complete is output.

ELSE means the condition was false. It is not another condition and does not need an extra comparison written after it.

05 | TRACE THE BRANCH AND THE COMMON CODE

The common output occurs regardless of the selected branch
ScoreCondition resultSelected branch outputOutput after ENDIF
49FALSENot yet passedReview complete
50TRUEPassReview complete
75TRUEPassReview complete

Record only the statements that actually execute. Do not output both branch messages merely because both are visible in the pseudocode.

Look carefully at indentation and closing keywords. An output inside THEN is conditional. The same output after ENDIF runs on either path.

Selection changes the path through instructions; sequence still determines the order along that path.

06 | TRY THE DECISION AT ITS BOUNDARY

Predict the branch before entering a score. Try forty-nine, fifty and fifty-one, then compare the condition with the selected output.

:

THEN | TRUE

Pass

Not evaluated yet

ELSE | FALSE

Not yet passed

Not evaluated yet

The instruction after ENDIF has not run yet.

The demo checks its form input. The score classification itself uses the rule Score >= 50. After either branch, the common instruction runs.

07 | CHOOSE THE COMPARISON TO MATCH THE RULE

These are Cambridge pseudocode comparison operators
OperatorMeaningExample true case
=Equal toScore = 50 when Score is 50
<>Not equal toScore <> 50 when Score is 49
>Greater thanScore > 50 when Score is 51
>=Greater than or equal toScore >= 50 when Score is 50
<Less thanScore < 50 when Score is 49
<=Less than or equal toScore <= 50 when Score is 50

For “below fifty”, use < 50, not <= 50. For “no more than fifty”, use <= 50. Translating the wording correctly is part of constructing the algorithm.

Comparisons can also involve suitable text or character values, such as MenuChoice = 'A'. Use values appropriate to the declared type rather than comparing a number with unrelated text.

08 | AND REQUIRES BOTH CONDITIONS

Some rules have more than one requirement. A club accepts ages from thirteen to seventeen inclusive. The age must meet both its lower and upper limit.

IF Age >= 13 AND Age <= 17
  THEN
    OUTPUT "Age within club range"
  ELSE
    OUTPUT "Age outside club range"
ENDIF

Assume Age is a declared INTEGER holding a whole-number age. Sixteen makes both comparisons true. Twelve fails the lower comparison, and eighteen fails the upper comparison.

AND makes the combined condition true only when both parts are true. Using OR here would admit ages outside the interval, because an age can satisfy one bound while violating the other.

Write the full comparisons. Age >= 13 AND <= 17 omits the value being compared in its second part.

09 | OR AND NOT EXPRESS DIFFERENT RULES

OR is useful when either condition is enough. An outside-range test can use Age < 13 OR Age > 17. An age below the minimum or above the maximum is outside the permitted interval.

IF Age < 13 OR Age > 17
  THEN
    OUTPUT "Age outside club range"
ENDIF

NOT reverses a Boolean result. If Found is a declared Boolean search flag, IF NOT Found selects instructions when no match was found.

IF NOT Found
  THEN
    OUTPUT "Item not found"
ENDIF

Use parentheses when grouping more complex conditions so the intended relationships are clear. For now, practise simple complete comparisons before combining many tests.

Remember that a BOOLEAN variable already holds a logical value. IF PaymentComplete can directly test that value; IF PaymentComplete = TRUE expresses the same decision explicitly.

10 | TWO SEPARATE IF STATEMENTS ARE TWO DECISIONS

Do not assume two IF statements behave like one IF/ELSE. Each separate IF evaluates its own condition.

IF Score >= 50
  THEN
    OUTPUT "Pass"
ENDIF
IF Score < 80
  THEN
    OUTPUT "Practise for a higher score"
ENDIF

At seventy-five, both conditions are true, so both messages appear. That can be correct if the program should congratulate a pass and offer further practice.

In an IF/ELSE version using Score >= 50, the ELSE message appears only below fifty. It cannot also appear at seventy-five.

Choose independent IF statements when independent actions may both be needed. Choose IF/ELSE when the condition should select one of two alternatives.

11 | NEST DECISIONS FOR THREE OUTCOMES

A nested IF is an IF inside a branch of another IF. Suppose scores of at least eighty get Distinction; scores from fifty to seventy-nine get Pass; lower scores get Not yet passed.

IF Score >= 80
  THEN
    OUTPUT "Distinction"
  ELSE
    IF Score >= 50
      THEN
        OUTPUT "Pass"
      ELSE
        OUTPUT "Not yet passed"
    ENDIF
ENDIF

The second IF is reached only when the outer condition is false, so Score is already below eighty. It then checks whether Score is at least fifty.

Only one category is output
ScoreOuter >= 80Inner >= 50Output
85TRUENot reachedDistinction
75FALSETRUEPass
45FALSEFALSENot yet passed

Each IF needs its matching ENDIF. The inner ENDIF closes the pass decision; the outer ENDIF closes the distinction decision. Indentation helps show which statements belong to each branch.

12 | TEST THE MOST RESTRICTIVE CATEGORY FIRST

In the nested classification above, checking Score >= 50 first and immediately outputting Pass would also catch eighty-five. If the distinction check were only in that ELSE branch, eighty-five would never reach it.

For overlapping thresholds, check the higher category first, or use another arrangement that explicitly represents non-overlapping ranges. Do not rely on the output labels to make the conditions exclusive.

Test values forty-nine, fifty, seventy-nine and eighty. They expose mistakes at both category boundaries. Middle values alone may miss an incorrect > instead of >= comparison.

These thresholds are illustrative practice rules, not an official exam grade boundary.

13 | READ A COMPLETE INPUT AND SELECTION ALGORITHM

This program receives a valid whole-number score from zero to one hundred and outputs one category, followed by a common closing message. Assume input is valid; this example classifies it without a validation retry.

DECLARE Score : INTEGER
OUTPUT "Enter a whole-number score from 0 to 100"
INPUT Score
IF Score >= 80
  THEN
    OUTPUT "Distinction"
  ELSE
    IF Score >= 50
      THEN
        OUTPUT "Pass"
      ELSE
        OUTPUT "Not yet passed"
    ENDIF
ENDIF
OUTPUT "Review complete"

Input fifty leads through the outer ELSE to the inner THEN. The program outputs Pass, then Review complete.

A prompt describing the range is not validation. If the requirements also demand rejection and retry of invalid inputs, add that handling before classifying the accepted score.

Follow all required outputs in a trace, including the prompt and closing message, rather than recording only the selected category.

14 | AVOID THESE SELECTION MISTAKES

Running both branches: an IF/ELSE selects one branch for its evaluated condition.

Stopping after false: execution continues after ENDIF, whether a THEN body ran or not.

Missing equality: “at least” and “inclusive” require the boundary to be included.

AND confused with OR: an inside-range check needs both bounds; an outside-range check can accept either violation.

Overlapping independent tests: two separate IFs may both execute.

Nested statements not closed: match each IF with its own ENDIF and indent its contents.

Classification mistaken for validation: choosing a category does not automatically reject an invalid input.

15 | EXPLAIN THE PATH WITH A REASON

For input fifty: “The outer distinction condition is false because fifty is below eighty, so the program reaches the inner IF. Its condition is true because fifty meets the inclusive pass threshold, therefore Pass is output.”

For common code: “Review complete is after the outer ENDIF, so it runs regardless of the selected category.”

Use the exact condition and current value to explain a branch. A statement such as “it chooses the correct message” hides the reasoning students need to demonstrate.

The next lesson introduces CASE selection, another way to express suitable choices. Keep these habits: evaluate the condition, follow the selected path and continue at the correct point.

MATCH THE SELECTION IDEA TO ITS ROLE

Terminology

Terminology

Selection

Choosing instructions according to a condition.

Condition

An expression evaluated as TRUE or FALSE.

IF

The keyword introducing a conditional decision.

THEN

Introduces instructions executed when the IF condition is true.

ELSE

Introduces the alternative instructions executed when the IF condition is false.

ENDIF

Closes an IF statement.

Branch

A path of instructions associated with a decision outcome.

Common code

Instructions after a decision that are reached regardless of its selected branch.

Comparison

An operation comparing values and producing a Boolean result.

Inclusive boundary

A limit whose equality case is included.

AND

Produces true only when both combined conditions are true.

OR

Produces true when at least one combined condition is true.

NOT

Reverses a Boolean result.

Nested IF

An IF statement inside a branch of another IF statement.

Independent IF statements

Separate decisions that each evaluate their own condition.

Boolean flag

A variable holding a two-state logical status.

Boundary test

A test chosen around a decision or accepted-data limit.

Classification

Assigning an input to an outcome category according to rules.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Which statements about IF/ELSE are correct?
2. An IF without ELSE has a false condition. What happens?
3. Which condition matches at least fifty?
4. Which condition checks whole-number ages inside thirteen to seventeen inclusive?
5. Which is an outside-range condition for those ages?
6. Two separate IF statements test Score >= 50 and Score < 80. At seventy-five, which happens?
7. The nested classification tests >= 80 first, then >= 50 in ELSE. Score is eighty-five. Which are correct?
8. Where should a closing message go if required after every category?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Define selection and describe the role of a condition.

2. Explain what happens for Score forty-nine in IF Score >= 50 THEN output Pass ELSE output Not yet passed ENDIF.

3. Compare IF without ELSE and IF/ELSE for a false condition.

4. Explain why Score >= 50 rather than Score > 50 matches at least fifty.

5. Explain why an inside-range check uses AND and an outside-range check can use OR.

6. Describe the outputs for separate IFs Score >= 50 and Score < 80 at seventy-five.

7. Trace the nested distinction/pass classification for scores forty-nine, fifty, seventy-nine and eighty.

8. Explain why Review complete after the outer ENDIF executes for every category.

BUILD IT | A DISCOUNT DECISION

A user supplies a valid non-negative purchase total. Totals of at least 100 receive a ten-percent discount; smaller totals receive none. Output the amount to pay, then Shopping complete on either path. Write a complete algorithm and test totals 99, 100 and 120. Assume valid input.

Reveal one possible solution
DECLARE PurchaseTotal : REAL
DECLARE AmountToPay : REAL
OUTPUT "Enter the purchase total"
INPUT PurchaseTotal
IF PurchaseTotal >= 100.0
  THEN
    AmountToPay ← PurchaseTotal * 0.90
  ELSE
    AmountToPay ← PurchaseTotal
ENDIF
OUTPUT "Amount to pay: ", AmountToPay
OUTPUT "Shopping complete"

Purchase totals ninety-nine, one hundred and one hundred twenty produce calculated amounts ninety-nine, ninety and one hundred eight. The boundary of one hundred receives the discount. Both branches assign AmountToPay before the common output reads it.

Explain: multiplying by 0.90 retains ninety percent of the total, giving a ten-percent discount. Shopping complete is outside the IF, therefore it runs for either outcome. Currency formatting is a separate display concern.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 8.1.4B.1 Selection with IF, THEN and ELSE is coming soon. Complete the matching task and written questions in your exercise book.