Selection in programming is a concept enabling the execution of various instructions depending on specific conditions. It involves two main types of selection statements: IF statements and CASE statements.
IF statements enable the execution of a set of instructions if a condition is met. They follow this 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 are utilized to run a specific set of instructions when a condition is true and another set when it's false. They follow this 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
Else-if statements are employed to assess multiple conditions and execute distinct instructions for each condition. They follow this 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
CASE statements enable the execution of different sets of instructions depending on the value of a variable. They follow this 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
92 docs|30 tests
|
|
Explore Courses for Year 11 exam
|