Selection (Cambridge International AS Computer Science)
Exam board Cambridge International · ASUpdated 2025-05-05
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
Spec point —
11.2.2
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 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 OTHERWISE clause can be the last clause |
Example code
Case statements in different languages
| Python | VB.net | Java |
|---|---|---|
Was this revision note helpful?
Build on this topic