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

Programming Constructs

What is a programming construct?

  • A programming construct determines the order in which lines of code are executed

  • There are three programming constructs. These are

    • Sequence

    • Iteration

    • Branching (also known as selection)

Sequence

  • Sequence refers to lines of code which are run one line at a time

  • The lines of code are run in the order that they written from the first line of code to the last line of code

Branching (selection)

  • Branching, also known as selection is when the flow of the program is interrupted and a condition is tested

  • The outcome of this condition will then determine which lines or block of code is run next

  • There are two ways to write selection statements:

    • ifelseifelse… statements – this is when you test conditions sequentially 

    • switchcase… statements – this is when you test an expression against multiple possible constant values (known as cases)

Iteration

  • Iteration is repeating a line or a block of code using a loop

  • Iteration can be:

    • count controlled – this is when the code is repeated a fixed number of times (e.g. using a for loop)

    • condition controlled – this is when the code is repeated until a condition is met (e.g. using a while loop or a do while loop)

Identifying programming constructs

  • You can identify which programming constructs are used by looking at certain keywords

  • The keywords if, elseif, else, endif, switch, case indicate that the construct is selection

  • The keywords for, while, do indicate that the construct is iteration

  • If none of these keywords are used, this is a good indication that the construct is sequence

01 numbers = []
02 # Ask the user to enter 5 numbers
03 for i in range(5):
04 num = int(input("Enter a number: "))
05 numbers.append(num)
06 # Assume the first number is the largest
07 largest_number = numbers[0]
08 # Identifying the largest number
09 for num in numbers:
10 if num > largest_number:
11 largest_number = num
12 print("The largest number is:", largest_number)
  • In this example, iteration is used twice:

    1. Repeating code to ask the user to enter 5 numbers (indicated by the keyword for on line 03)

    2. Repeating code to identify the largest number (indicated by the keyword for on line 09)

  • In this example, selection is used once to determine if a number is bigger than the currently stored biggest number (indicated by the keyword if on line 10) and if so it will update the largest number on line 11

  • Sequence is any lines of code which does not use iteration or selection. In this example, the first and last lines of code are sequence

Responses

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