Selection (Cambridge (CIE) IGCSE Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

What is Selection?

  • Selection is when the flow of a program is changed, depending on a set of conditions

  • The outcome of this condition will then determine which lines or block of code is run next

  • Selection is used for validation, calculation and making sense of a user's choices

  • There are two ways to write selection statements:

    • if... then... else...

    • case...

If Statements

What is an If statement?

  • As If statements allow you to execute a set of instructions if a condition is true

  • They have the following syntax:


    IF <condition>
    THEN
    <statement>
    ENDIF

  • A detailed look at a Python IF statement:

if-else-if-else-statement-pseudocode-computer-science-revision-notes

Example

Concept

Pseudocode

Python

IF-THEN-ELSE

IF Answer ← "Yes"

THEN

OUTPUT "Correct"

ELSE

OUTPUT "Wrong"

ENDIF

if answer == "Yes":

print("Correct")

elif answer == "No":

print("Wrong")

else:

print("Error")

Nested Selection

What is nested selection?

  • Nested selection is a selection statement within a selection statement, e.g. an If inside of another If

  • Nested means to be 'stored inside the other'

Example

Pseudocode

IF Player2Score > Player1Score

THEN

IF Player2Score > HighScore

THEN

OUTPUT Player2, " is champion and highest scorer"

ELSE

OUTPUT Player2, " is the new champion"

ENDIF

ELSE

OUTPUT Player1, " is still the champion"

IF Player1Score > HighScore

THEN

OUTPUT Player1, " is also the highest scorer"

ENDIF

ENDIF

Python

// Prompt the user to enter a number
print("Enter a number: ")
test_score = int(input())

// Outer statement to check the test_score is above 40

if test_score > 40:

// Inner statement to assign the result from the test

    if test_score > 70:

    result = "Distinction"

    elif test_score > 55:

        result = "Merit"

    elif test_score > 40:

        result = "Pass

else:

result = "Fail"

// Output the result

print(result)

Case Statements

What is a case statement?

  • A case statement can mean less code but it only useful when comparing multiple values of the same variable

  • If statements are more flexible and are generally used more in languages such as Python

  • The format of a CASE statement is:

    CASE OF <identifier>
    <value 1> : <statement>
    <value 2>: <statement>
    ....
    OTHERWISE <statement>
    ENDCASE

Concept

Pseudocode

Python

CASE

INPUT Move

CASE OF Move

'W' : Posistion ← Posistion - 10

'E' : Posistion ← Posistion + 10

'A' : Posistion ← Posistion - 1

'D' : Posistion ← Posistion + 10

OTHERWISE OUTPUT "Beep"

ENDCASE

match day:

case "Sat":

print("Saturday")

case "Sun":

print("Sunday")

case _:

print("Weekday")

Examiner Tips and Tricks

  • Make sure to include all necessary components in the selection statement:

    • the condition,

    • the statement(s) to execute when the condition is true

    • any optional statements to execute when the condition is false

  • Use proper indentation to make the code easier to read and understand

  • Be careful with the syntax of the programming language being used, as it may differ slightly between languages

  • Make sure to test the selection statement with various input values to ensure that it works as expected

Worked Example

Write an algorithm using pseudocode that:

  • Inputs 3 numbers

  • Outputs the largest of the three numbers

[3]

Exemplar answer

Psuedocode

INPUT A

INPUT B

INPUT C

IF A > B

THEN

IF A > C

THEN

OUTPUT A

ELSE

OUTPUT C

ENDIF

ELSE

OUTPUT B

ENDIF

Python

if A > B AND A > C:

print(A)
elif B > C:

print(B)
else:

print(C)

Last updated:

You've read 0 of your 10 free revision notes

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.