Programming Constructs (OCR AS Computer Science): Revision Note

Exam code: H046

Becci Peters

Written by: Becci Peters

Reviewed by: James Woodhouse

Updated on

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:

    • if... elseif... else... statements - this is when you test conditions sequentially 

    • switch... case... 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

You've read 0 of your 5 free revision notes this week

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Becci Peters

Author: Becci Peters

Expertise: Computer Science Content Creator

Becci has been a passionate Computing teacher for over 9 years, teaching Computing across the UK helping to engage, interest and develop confidence in the subject at all levels. Working as a Head of Department and then as an educational consultant, Becci has advised schools in England, where her role was to support and coach teachers to improve Computing teaching for all. Becci is also a senior examiner for multiple exam boards covering GCSE & A-level. She has worked as a lecturer at a university, lecturing trainee teachers for Computing.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.