Back to 课程

Computer Science GCES OCR

0% Complete
0/0 Steps
  1. Cpu Architecture Performance And Embedded Systems Ocr
    5 主题
  2. Primary And Secondary Storage Ocr
    6 主题
  3. Data Storage And Compression Ocr
    12 主题
  4. Networks And Topologies Ocr
    6 主题
  5. Wired And Wireless Networks Protocols And Layers Ocr
    6 主题
  6. Identifying And Preventing Threats To Computer Systems And Networks Ocr
    2 主题
  7. Operating Systems And Utility Software Ocr
    2 主题
  8. Ethical Legal Cultural And Environmental Impact Ocr
    2 主题
  9. Computational Thinking Searching And Sorting Algorithms Ocr
    3 主题
  10. Designing Creating And Refining Algorithms Ocr
    5 主题
  11. Programming Fundamentals And Data Types Ocr
    5 主题
  12. Additional Programming Techniques Ocr
    7 主题
  13. Defensive Design And Testing Ocr
    6 主题
  14. Boolean Logic Diagrams Ocr
    2 主题
  15. Programming Languages And Integrated Development Environments Ides Ocr
    3 主题
  16. The Exam Papers Ocr
    2 主题
  17. Structuring Your Responses Ocr
    3 主题
课 Progress
0% Complete

Exam code:J277

What is a Programming Construct?

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

  • They control logic and behaviour of code

  • There are three core programming constructs:

    • Sequence

    • Selection

    • Iteration

Sequence

What is 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

  • Sequence is crucial to the flow of a program, any instructions out of sequence can lead to unexpected behaviour or errors

Example

  • A simple program to ask a user to input two numbers, number two is subtracted from number one and the result is outputted

Line

OCR reference language/Python

01

print("Enter the first number")

02

Num1 = input()

03

print("Enter the second number")

04

Num2 = input()

05

Result = Num1 - Num2

06

print(Result)

  • A simple swap of line 01 and line 02 would lead to an unexpected behaviour, the user would be prompted to input information without knowing what they should enter

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… statements – this is when you test conditions sequentially 

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

if-else-if-else-statement-pseudocode-computer-science-revision-notes

Example

Concept

OCR exam reference

Python

IF-THEN-ELSE

if answer == "Yes" then

print("Correct")

elseif answer == "No" then

print("Wrong")

else

print("Error")

endif

if answer == "Yes":

print("Correct")

elif answer == "No":

print("Wrong")

else:

print("Error")

CASE SELECT or SWITCH

switch day :

case "Sat":

print("Saturday")

case "Sun":

print("Sunday")

default:

print("Weekday")

endswitch

match day:

case "Sat":

print("Saturday")

case "Sun":

print("Sunday")

case _:

print("Weekday")

If vs select case

  • Select case can mean less code but it only useful when comparing multiple values of the same variable

  • If statements can be more flexible and are generally used more in languages such as Python

Iteration

What is 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)

xp66-srn-for-loop-pseudocode-computer-science-revision-notes
  • 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)

while-loop-password-pseudocode-computer-science-revision-notes

Examples

Iteration

OCR exam reference

Python

FOR loop

(Count controlled)

for x = 0 to 9

print("Hello")

next x

for x in range(10):

print("Hello")

This will print the word “Hello” 10 times (0-9 inclusive)

for x = 2 to 10 step 2

print(x)

next x

for x in range(2,12,2):

print(x)

# Python range function excludes end value

This will print the even numbers from 2 to 10 inclusive

for x = 10 to 0 step -1

print(x)

next x

for x in range(10,-1,-1):

print(x)

# Python range function excludes end value

This will print the numbers from 10 to 0 inclusive

WHILE loop

(Condition controlled)

while colour != "Red"

colour = input("New colour")

endwhile

while colour != "Red":

colour = input("New colour")

 

This will loop until the user inputs the colour “Red”. Check condition is carried out before entering loop

DO WHILE loop

(Condition controlled)

do

colour = input("New colour")

until answer == "Red"

# Not used in Python

 

This will loop until the user inputs the colour “Red”. Loop iterates once before a check is carried out

How to identify 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

Worked Example

Tick (✓) one box in each row to identify whether each programming construct has or has not been used in the program [3]

total = 0

for i = 1 to 5

num = input("Enter a number")

total = total + num

next i

print(total)

<img alt=”A table with three rows labeled Sequence, Selection, and Iteration, and two columns, Has been used and Has not been used. The cells are empty.” class=”ContentBlock_figure__vJw2q” data-nimg=”1″ decoding=”async” height=”188″ loading=”lazy” sizes=”(max-width: 320px) 320w, (max-width: 640px) 640w, (max-width: 960px) 960w, (max-width: 1280px) 1280w, 1920w” src=”https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=3840/https://cdn.savemyexams.com/uploads/2024/07/33445_programming-constructs-we-table-1.jpg” srcset=”https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=16/https://cdn.savemyexams.com/uploads/2024/07/33445_programming-constructs-we-table-1.jpg 16w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=32/https://cdn.savemyexams.com/uploads/2024/07/33445_programming-constructs-we-table-1.jpg 32w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=48/https://cdn.savemyexams.com/uploads/2024/07/33445_programming-constructs-we-table-1.jpg 48w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=64/https://cdn.savemyexams.com/uploads/2024/07/33445_programming-constructs-we-table-1.jpg 64w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=96/https://cdn.savemyexams.com/uploads/2024/07/33445_programming-constructs-we-table-1.jpg 96w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=128/https://cdn.savemyexams.com/uploads/2024/07/33445_programming-constructs-we-table-1.jpg 128w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=256/https://cdn.savemyexams.com/uploads/2024/07/33445_programming-constructs-we-table-1.jpg 256w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=384/https://cdn.savemyexams.com/uploads/2024/07/33445_programming-constructs-we-table-1.jpg 384w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=640/https://cdn.savemyexams.com/uploads/2024/07/33445_programming-constructs-we-table-1.jpg 640w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=750/https://cd

Responses

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