Selection: IF and CASE Cambridge IGCSE Computer Science revision

Not started

Learn it

In plain words

A program that does exactly the same thing every time is not much use. Selection lets it choose: test a condition, and run one set of statements or another depending on the answer.

5 things to know

  1. Sequence means statements are carried out one after another, in order. Selection means choosing which statements to carry out.
  2. IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF. The statements after THEN run when the condition is true. Those after ELSE run when it is false.
  3. The ELSE part can be left out if nothing needs to happen when the condition is false.
  4. CASE OF tests one variable against several values. Each value has its own statement, and OTHERWISE covers anything that does not match. It ends with ENDCASE.
  5. A nested IF is an IF statement inside another one. It is used when a second decision depends on the first.

Worked example

Age holds 15. What is output by: IF Age >= 18 THEN OUTPUT "Adult" ELSE IF Age >= 13 THEN OUTPUT "Teen" ELSE OUTPUT "Child" ENDIF ENDIF?

  1. First condition: is 15 >= 18? No. So the ELSE part runs.
  2. Second condition: is 15 >= 13? Yes. So "Teen" is output.
  3. The final ELSE is skipped.

Tips and tricks

  • Every IF needs its ENDIF and every CASE OF needs its ENDCASE. Count them, especially when one is nested inside another.
  • Use CASE when one variable is compared with a list of values. Use IF for ranges, or when two different variables are tested.
6 questions, about 2 minutes.

It lands in your notebook with its questions as flashcards.

Selection: IF and CASE: 6 questions and answers

These are the quiz’s questions. Do the quiz first, then come back here for the ones that got you.

  1. What is selection?
    • repeating statements
    • choosing which statements to run, depending on a condition (the answer)
    • storing a list of values
    • running statements in order

    IF and CASE are selection statements.

  2. Number holds 8. What does IF Number > 10 THEN OUTPUT "Big" ELSE OUTPUT "Small" ENDIF output?
    • Big
    • Small (the answer)
    • nothing
    • 8

    8 > 10 is false, so the ELSE part runs.

  3. Which keyword ends an IF statement?
    • NEXT
    • UNTIL
    • ENDIF (the answer)
    • ENDCASE

    ENDCASE ends a CASE statement.

  4. In a CASE statement, what is OTHERWISE for?
    • to end the statement
    • to say what happens when no value matches (the answer)
    • to repeat the statement
    • to declare a variable

    It catches every value not listed.

  5. What is a nested IF?
    • an IF with no ELSE
    • an IF statement inside another IF statement (the answer)
    • two IF statements side by side
    • an IF inside a comment

    The inner decision depends on the outer one.

  6. Score holds 50. What does IF Score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF output?
    • Pass (the answer)
    • Fail
    • nothing
    • 50

    50 >= 50 is true.

Still stuck on this one?Ask in the Papermunch Discord, or help someone else who is. Discord is for ages 13 and up.Join the server

Things you can type

Or go straight to

Or browse a shelf