Selection (Cambridge (CIE) A Level Computer Science): Revision Note

Exam code: 9618

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Selection

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?

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

  • They have the following syntax:

Pseudocode

IF <condition> THEN
  <statement(s)>
ENDIF
IF <condition> THEN
  <statement(s)>
ELSE
  <statement(s)>
ENDIF

Without an ELSE clause

With an ELSE clause

Nested if statements

  • Nested if statements are an if statement within an if statement

  • Nested means to be 'stored inside the other'

Example code

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

If statements in different languages

Python

VB.net

Java

weather = input("What is the weather like today? (sunny, rainy, snowy): ")

if weather == "sunny":
    print("Don't forget your sunglasses!")
elif weather == "rainy":
    print("Take an umbrella with you.")
elif weather == "snowy":
    print("Wear warm clothes!")
else:
    print("Not sure what to suggest for that kind of weather.")
Module WeatherCheck
    Sub Main()
        Dim weather As String

        Console.Write("What is the weather like today? (sunny, rainy, snowy): ")
        weather = Console.ReadLine()

        If weather = "sunny" Then
            Console.WriteLine("Don't forget your sunglasses!")
        ElseIf weather = "rainy" Then
            Console.WriteLine("Take an umbrella with you.")
        ElseIf weather = "snowy" Then
            Console.WriteLine("Wear warm clothes!")
        Else
            Console.WriteLine("Not sure what to suggest for that kind of weather.")
        End If

        Console.ReadLine() ' Keeps the console open
    End Sub
End Module
import java.util.Scanner;

public class WeatherCheck {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String weather;

        System.out.print("What is the weather like today? (sunny, rainy, snowy): ");
        weather = scanner.nextLine();

        if (weather.equals("sunny")) {
            System.out.println("Don't forget your sunglasses!");
        } else if (weather.equals("rainy")) {
            System.out.println("Take an umbrella with you.");
        } else if (weather.equals("snowy")) {
            System.out.println("Wear warm clothes!");
        } else {
            System.out.println("Not sure what to suggest for that kind of weather.");
        }

        scanner.close();
    }
}

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:

Pseudocode

CASE of <identifier>
  <value 1> : <statement1>
              <statement2>
              ...
  <value 2> : <statement1>
              <statement2>
              ...
  ...
ENDCASE 
CASE of <identifier>
  <value 1> : <statement1>
              <statement2>
              ...
  <value 2> : <statement1>
              <statement2>
              ...
  OTHERWISE : <statement1>
              <statement2>
              ...
ENDCASE 

An OTHERWISE clause can be the last clause

Example code

DECLARE Direction : STRING

OUTPUT "Enter a direction (N, S, E, W):"
INPUT Direction

CASE OF Direction
    "N" : OUTPUT "You are heading North"
    "S" : OUTPUT "You are heading South"
    "E" : OUTPUT "You are heading East"
    "W" : OUTPUT "You are heading West"
    OTHERWISE : OUTPUT "Invalid direction entered"
ENDCASE

Case statements in different languages

Python

VB.net

Java

weather = input("What is the weather like today? (sunny, rainy, snowy): ")

match weather:
    case "sunny":
        print("Don't forget your sunglasses!")
    case "rainy":
        print("Take an umbrella with you.")
    case "snowy":
        print("Wear warm clothes!")
    case _:
        print("Not sure what to suggest for that kind of weather.")
Module WeatherCheck
    Sub Main()
        Dim weather As String

        Console.Write("What is the weather like today? (sunny, rainy, snowy): ")
        weather = Console.ReadLine()

        Select Case weather
            Case "sunny"
                Console.WriteLine("Don't forget your sunglasses!")
            Case "rainy"
                Console.WriteLine("Take an umbrella with you.")
            Case "snowy"
                Console.WriteLine("Wear warm clothes!")
            Case Else
                Console.WriteLine("Not sure what to suggest for that kind of weather.")
        End Select

        Console.ReadLine() ' Keeps the console open
    End Sub
End Module
import java.util.Scanner;

public class WeatherCheck {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String weather;

        System.out.print("What is the weather like today? (sunny, rainy, snowy): ");
        weather = scanner.nextLine();

        switch (weather) {
            case "sunny":
                System.out.println("Don't forget your sunglasses!");
                break;
            case "rainy":
                System.out.println("Take an umbrella with you.");
                break;
            case "snowy":
                System.out.println("Wear warm clothes!");
                break;
            default:
                System.out.println("Not sure what to suggest for that kind of weather.");
                break;
        }

        scanner.close();
    }
}

You've read 0 of your 5 free revision notes this week

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

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

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.