Back to 课程

Computer-Science-A-level-Ocr

0% Complete
0/0 Steps
  1. 3-3-networks
    8 主题
  2. 3-2-databases
    7 主题
  3. 3-1-compression-encryption-and-hashing
    4 主题
  4. 2-5-object-oriented-languages
    7 主题
  5. 2-4-types-of-programming-language
    4 主题
  6. 2-3-software-development
    5 主题
  7. 2-2-applications-generation
    6 主题
  8. 2-1-systems-software
    8 主题
  9. 1-3-input-output-and-storage
    2 主题
  10. 1-2-types-of-processor
    3 主题
  11. 1-1-structure-and-function-of-the-processor
    1 主题
  12. structuring-your-responses
    3 主题
  13. the-exam-papers
    2 主题
  14. 8-2-algorithms-for-the-main-data-structures
    4 主题
  15. 8-1-algorithms
    10 主题
  16. 7-2-computational-methods
    11 主题
  17. 7-1-programming-techniques
    14 主题
  18. 6-5-thinking-concurrently
    2 主题
  19. 6-4-thinking-logically
    2 主题
  20. 6-3-thinking-procedurally
    3 主题
  21. 6-2-thinking-ahead
    1 主题
  22. 6-1-thinking-abstractly
    3 主题
  23. 5-2-moral-and-ethical-issues
    9 主题
  24. 5-1-computing-related-legislation
    4 主题
  25. 4-3-boolean-algebra
    5 主题
  26. 4-2-data-structures
    10 主题
  27. 4-1-data-types
    9 主题
  28. 3-4-web-technologies
    16 主题
课 Progress
0% Complete

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:

    • If statements

    • Switch case statements

  • 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 the statement will let you choose a line/lines of code to run if a condition is true or false 

  • Below are three examples of if statements:

  1. if

  2. if else

  3. 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

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 if statement checks if the value of the variable number is greater than 0. 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

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 number is greater than 0, 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

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-else statement evaluates the value of score to 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 your if statement but it might be clearer to use a switch case statement

  • You can have an if else-if statement 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

您的邮箱地址不会被公开。 必填项已用 * 标注