Selection (Cambridge (CIE) A Level Computer Science): Revision Note
Exam code: 9618
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 | |
---|---|
|
|
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 |
---|---|---|
|
|
|
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 | |
---|---|
|
|
An |
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 |
---|---|---|
|
|
|
You've read 0 of your 5 free revision notes this week
Unlock more, it's free!
Did this page help you?