What are the three programming constructs, and how do you use selection and the two kinds of iteration?
The three basic programming constructs: sequence, selection (if and switch/case) and iteration (count-controlled for loops and condition-controlled while and do until loops), and when to use each.
An OCR J277 2.2.2 answer on the three programming constructs: sequence, selection (if and switch/case) and iteration (count-controlled for loops and condition-controlled while and do until loops), with the OCR Exam Reference Language for each.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
OCR wants you to use the three basic programming constructs: sequence, selection and iteration. You must know selection with if and with switch/case, and the two kinds of iteration (count-controlled for loops and condition-controlled while and do ... until loops), and choose the right one for a task. These constructs appear in almost every Paper 2 program.
Sequence
Selection
A grade example using if/elseif:
if mark >= 70 then
print("Distinction")
elseif mark >= 50 then
print("Merit")
elseif mark >= 40 then
print("Pass")
else
print("Fail")
endif
Iteration
Try this
Q1. Name the three basic programming constructs. [1 mark]
- Cue. Sequence, selection and iteration.
Q2. State which kind of loop you would use to repeat a block exactly 20 times. [1 mark]
- Cue. A count-controlled loop (a
forloop).
Q3. State the difference between a while loop and a do ... until loop. [2 marks]
- Cue. A
whileloop checks the condition before each pass, so it may run zero times; ado ... untilloop checks after each pass, so it always runs at least once.
Exam-style practice questions
Practice questions written in the style of OCR exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
OCR 20214 marksExplain the difference between a count-controlled loop and a condition-controlled loop, and give an example of when each would be the better choice.Show worked answer →
Count-controlled loop (up to 2): repeats a fixed, known number of times, using a for loop with a start and end value. Example: printing the 12 rows of a times table, where you know there are exactly 12.
Condition-controlled loop (up to 2): repeats while (or until) a condition is met, when the number of repetitions is not known in advance, using a while or do ... until loop. Example: asking the user to enter a password until they get it right, where you do not know how many tries it will take.
Markers reward the known-versus-unknown number of repetitions distinction and a sensible example for each. Saying "for loops are faster" misses the point.
OCR 20225 marksA program should ask the user to guess a number between 1 and 10. If they guess 7 it prints "Correct", otherwise it prints "Wrong". Write an algorithm using selection, and then describe how you would change it so the user keeps guessing until they are correct.Show worked answer →
Selection version (up to 3): read the guess, then use if/else.
guess = int(input("Guess a number 1 to 10: "))
if guess == 7 then
print("Correct")
else
print("Wrong")
endif
Marks: input (1), correct condition == 7 (1), correct output on each branch (1).
Changing it (up to 2): wrap the input and check in a condition-controlled loop that repeats until the guess is correct, for example a while guess != 7 loop, or a do ... until guess == 7 loop, so the user keeps guessing. Markers reward a correct loop that repeats the input and stops on the correct guess.
Related dot points
- The use of variables and constants, the common data types (integer, real, Boolean, character and string), choosing an appropriate data type, and casting (converting) between data types.
An OCR J277 2.2.1 answer on variables and constants, the common data types (integer, real, Boolean, character, string), choosing an appropriate data type for data, and casting between data types.
- The common operators: arithmetic (add, subtract, multiply, divide, exponent, MOD and DIV), comparison operators, and Boolean operators (AND, OR, NOT), and how they are used in expressions.
An OCR J277 2.2.2 answer on the common operators: arithmetic (including exponent, MOD and DIV), comparison operators, and the Boolean operators AND, OR and NOT, with worked examples of integer division and modulus.
- Producing algorithms using pseudocode and flowcharts to solve a problem, identifying the inputs, processes and outputs, and interpreting, correcting and refining algorithms others have written.
An OCR J277 2.1.2 answer on designing algorithms with pseudocode and flowcharts: identifying inputs, processes and outputs, the OCR Exam Reference Language, the standard flowchart symbols, and interpreting, correcting and refining algorithms.
- Using trace tables to determine the output of an algorithm and to follow how the values of variables change, and determining the purpose of a simple algorithm.
An OCR J277 2.1.2 answer on using trace tables to follow an algorithm step by step, record how variable values change, find the output, and determine the purpose of a simple algorithm.
- The use of subprograms (procedures and functions), passing parameters into a subprogram, returning values from a function, local versus global variable scope, and generating random numbers.
An OCR J277 2.2.3 answer on subprograms: procedures and functions, passing parameters, returning values, the difference between local and global variables, the benefits of subprograms, and generating random numbers.
Sources & how we know this
- OCR GCSE (9-1) Computer Science (J277) specification — OCR (2020)