Selection (CIE IGCSE Computer Science)

Revision Note

Test Yourself
Becci Peters

Expertise

Computer Science

Selection

What is selection?

Selection is a programming concept that allows you to execute different sets of instructions based on certain conditions. There are two main types of selection statements: IF statements and CASE statements.

If Statements

IF statements allow you to execute a set of instructions if a condition is true. They have the following syntax:

IF condition

  THEN

    instructions

ENDIF

Pseudocode example:

x 5

IF x > 0

THEN

    PRINT "x is positive"

ENDIF


Python example:

x = 5

if x > 0:

    print("x is positive")

Java example:

int x = 5;

if (x > 0) {

System.out.println("x is positive");

}

Visual Basic example:

Dim x As Integer = 5

If x > 0 Then

Console.WriteLine("x is positive")

End If

IF ELSE Statements

If else statements are used to execute one set of statements if a condition is true and a different set of statements if the condition is false. They have the following syntax:

IF condition

  THEN

    Instructions

ELSE

    Instructions 

ENDIF

Pseudocode example:

x ← 5

IF x > 0

THEN

    PRINT "x is positive"

ELSE

    PRINT “x is negative”

ENDIF

Python example:

x = 5

if x > 0:

    print("x is positive")

else:

    print("x is negative")

Java example:

int x = 5;

if (x > 0) {

System.out.println("x is positive");

} else {

System.out.println("x is negative");

}

Visual Basic example:

Dim x As Integer = 5

If x > 0 Then

Console.WriteLine("x is positive")

Else

Console.WriteLine("x is negative")

End If

IF ELSE IF Statements

If else if statements are used to test multiple conditions and execute different statements for each condition. They have the following syntax:

IF condition

  THEN

    Instructions

ELSE IF condition

  THEN

    Instructions

  ELSE

    Instructions 

ENDIF

Pseudocode example:

x ←  5

IF x > 0

THEN

    PRINT "x is positive"

ELSE IF x < 0

THEN

    PRINT “x is negative”

ELSE

    PRINT “x is 0”

ENDIF

Python example:

x = 5

if x > 0:

    print("x is positive")

elif x < 0:

    print("x is negative")

else:

    print("x is 0")

Java example:

int x = 5;

if (x > 0) {

System.out.println("x is positive");

} else if (x < 0) {

System.out.println("x is negative");

} else {

System.out.println("x is 0");

}

Visual Basic example:

Dim x As Integer = 5

If x > 0 Then

Console.WriteLine("x is positive")

ElseIf x < 0 Then

Console.WriteLine("x is negative")

Else

Console.WriteLine("x is 0")

End If

Worked example

Write an algorithm using pseudocode that:

  • Inputs 3 numbers
  • Outputs the largest of the three numbers

[3]

  INPUT a, b, c  
  IF a > b AND a > c THEN PRINT a [1]
         ELSE IF b > c THEN PRINT b [1]
  ELSE PRINT c  [1]



 

 

 

Case Statements

CASE statements allow you to execute different sets of instructions based on the value of a variable. They have the following syntax:

CASE OF variable

    value1: instructions

   value2: instructions

    ...

    OTHERWISE instructions

END CASE

Pseudocode example:

CASE OF number

1: PRINT "Monday"

2: PRINT "Tuesday"

3: PRINT “Wednesday”

4: PRINT “Thursday”

5: PRINT “Friday”

6: PRINT “Saturday”

7: PRINT “Sunday”

OTHERWISE PRINT "Invalid number"

END CASE

Python example:

match (number) 

case 1:

print "Monday";

case 2:

print "Tuesday";

case 3:

print "Wednesday";

case 4:

print "Thursday";

case 5:

print "Friday";

case 6:

print "Saturday";

case 7:

print "Sunday";

case _:

print "Invalid number";

Java example:

switch (number) {

case 1:

return "Monday";

case 2:

return "Tuesday";

case 3:

return "Wednesday";

case 4:

return "Thursday";

case 5:

return "Friday";

case 6:

return "Saturday";

case 7:

return "Sunday";

default:

return "Invalid number";

}

Visual Basic example:

Select Case number

Case 1

Return "Monday"

Case 2

Return "Tuesday"

Case 3

Return "Wednesday"

Case 4

Return "Thursday"

Case 5

Return "Friday"

Case 6

Return "Saturday"

Case 7

Return "Sunday"

Case Else

Return "Invalid number"

End Select

Exam Tip

  • Make sure to include all necessary components in the selection statement: the condition, the statement(s) to execute when the condition is true, and 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

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

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

the (exam) results speak for themselves:

Did this page help you?

Becci Peters

Author: Becci Peters

Becci has been a passionate Computing teacher for over 9 years, teaching Computing across the UK helping to engage, interest and develop confidence in the subject at all levels. Working as a Head of Department and then as an educational consultant, Becci has advised schools in England, where her role was to support and coach teachers to improve Computing teaching for all. Becci is also a senior examiner for multiple exam boards covering GCSE & A-level. She has worked as a lecturer at a university, lecturing trainee teachers for Computing.