Computer Science AS OCR
-
1-1-structure-and-function-of-the-processor as5 主题
-
1-2-types-of-processor as3 主题
-
1-3-input-output-and-storage as2 主题
-
2-1-systems-software as8 主题
-
2-3-software-development as5 主题
-
2-4-types-of-programming-language as4 主题
-
3-1-compression-encryption-and-hashing as3 主题
-
3-2-databases as3 主题
-
3-3-networks as8 主题
-
3-4-web-technologies as13 主题
-
html as
-
css as
-
css-styling as
-
javascript as
-
variables-and-constants-in-javascript as
-
outputs-in-javascript as
-
selection-in-javascript- as
-
for-loops-in-javascript- as
-
while-loops-in-javascript- as
-
strings-in-javascript- as
-
operators-in-javascript- as
-
nested-statements-in-javascript as
-
functions-and-procedures-in-javascript as
-
html as
-
4-1-data-types as8 主题
-
4-2-data-structures as4 主题
-
4-3-boolean-algebra as1 主题
-
5-1-computing-related-legislation as4 主题
-
5-2-moral-and-ethical-issues as9 主题
-
6-1-thinking-abstractly as3 主题
-
6-2-thinking-ahead as1 主题
-
6-3-thinking-procedurally as3 主题
-
6-4-thinking-logically as2 主题
-
6-5-thinking-concurrently as2 主题
-
7-1-programming-techniques as9 主题
-
8-1-standard-algorithms-and-big-o-notation as8 主题
selection-in-javascript- as
Exam code:H046
Selection in JavaScript
-
Programming code is made up of constructs which control the flow of a program
-
Constructs tell the computer the order in which to carry out the statements/lines of code
-
There are 3 programming constructs:
-
Sequence
-
Selection
-
Iteration
-
-
Selection is selecting a line/lines of code to run depending on whether a condition is true or false
-
There are two ways to write selection statements:
-
Ifstatements -
Switch casestatements
-
-
When writing a condition there will need to be a comparison operator. They are listed below:
|
Operator |
Description |
Example (where x=5) |
Returns |
|---|---|---|---|
|
== |
equal to |
x == 8 |
false |
|
x == 5 |
true |
||
|
x == “5” |
true |
||
|
=== |
equal value and equal type |
x === 5 |
true |
|
x === “5” |
false |
||
|
!= |
not equal |
x != 8 |
true |
|
!== |
not equal value or not equal type |
x !== 5 |
false |
|
x !== “5” |
true |
||
|
x !== 8 |
true |
||
|
> |
greater than |
x > 8 |
false |
|
< |
less than |
x < 8 |
true |
|
>= |
greater than or equal to |
x >= 8 |
false |
|
<= |
less than or equal to |
x <= 8 |
true |
IF Statements in JavaScript
-
An
if thestatement will let you choose a line/lines of code to run if a condition is true or false -
Below are three examples of
ifstatements:
-
if -
if else -
if else if else
Syntax of an if statement
The syntax of an if the statement consists of the if keyword, followed by a condition enclosed in brackets, and a code block that is executed if the condition evaluates to true:
if (condition) { // Code to be executed if the condition is true}
Pseudocode example of an if statement

Example in JavaScript: checking if a number is positive
const number = 5;
if (number > 0) { console.log('The number is positive.');}
-
In this example, the
ifstatement checks if the value of the variablenumberis greater than0. If the condition is true, the message'The number is positive.'is output to the browser
Syntax of an if-else statement
The if the statement can be extended with an else clause to specify an alternative block of code that is executed when the condition evaluates to false:
if (condition) { // Code to be executed if the condition is true} else {
// Code to be executed if the condition is false
}
Pseudocode example of an if-else statement

Example in JavaScript: Checking if a number is positive or negative
const number = -3;
if (number > 0) { console.log('The number is positive.');} else { console.log('The number is not positive.');}
-
In this example, if the value of
numberis greater than0, the message'The number is positive.'is output. Otherwise, the message'The number is not positive.'is output
Syntax of an If Else-If Else statement
The else if the clause specifies additional conditions to check if the initial if condition is false. This allows the handling of multiple scenarios in a more complex decision-making process:
if (condition) { // Code to be executed if the condition is true} else if (condition) {
// Code to be executed if the condition is true} else {
// Code to be executed if the condition is false
}
Pseudocode example of an If Else-If Else statement

Example in JavaScript: Grading a score
const score = 85;
if (score >= 90) { console.log('Excellent!');} else if (score >= 80) { console.log('Good.');} else if (score >= 70) { console.log('Fair.');} else { console.log('Needs improvement.');}
-
In this example, the
if-else if-elsestatement evaluates the value ofscoreto determine the corresponding message based on the score range
Examiner Tips and Tricks
-
You can use as many
else ifs as you want to within yourifstatement but it might be clearer to use aswitch casestatement -
You can have an
if else-ifstatement without an end as the catch-all condition at the end
How do I write my condition?
-
Think of it like writing a yes/no question
-
E.g.
-
Is the number bigger than 10?
-
Is the number between 50 and 100?
-
Is the answer Paris?
-
-
If the question can’t be answered with yes/no then it needs to be rewritten in this way
Is it possible to use more than one operator?
-
Yes! Using one operator is most common but sometimes 2 are needed. More than 2 could be used but it gets more complicated. This involves using boolean operators
-
Imagine a program where the user has to enter a number based on the role of a dice. The number needs to be between 1 and 6
-
The yes/no question could be is the number between 1 and 6 – but it would be structured slightly differently in the code
-
IF number >=1 AND number <=6 then -
The first check is if the number is greater than or equal to 1
-
The second check is if the number is less than or equal to 6
-
The final check is if both sides are True
Responses