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.
Author
Becci PetersExpertise
Computer Science
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 allow you to execute a set of instructions if a condition is true. They have the following syntax:
IF condition
THEN
instructions
ENDIF
x← 5
IF x > 0
THEN
PRINT "x is positive"
ENDIF
x = 5
if x > 0:
print("x is positive")
int x = 5;
if (x > 0) {
System.out.println("x is positive");
}
Dim x As Integer = 5
If x > 0 Then
Console.WriteLine("x is positive")
End If
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
x ← 5
IF x > 0
THEN
PRINT "x is positive"
ELSE
PRINT “x is negative”
ENDIF
x = 5
if x > 0:
print("x is positive")
else:
print("x is negative")
int x = 5;
if (x > 0) {
System.out.println("x is positive");
} else {
System.out.println("x is negative");
}
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 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
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
x = 5
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is 0")
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");
}
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
Write an algorithm using pseudocode that:
[3]
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
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
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";
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";
}
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
to absolutely everything:
Did this page help you?
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.