Revise Computingrevisecomputing.co.uk
Free student revisionSee questionsFor schoolsSchool enquiry
Free revisionSee questionsFor schoolsSchool enquiry
Knowledge/A Level/H446/02 Algorithms and programming/Programming Techniques/Programming constructs

Programming constructs: sequence, iteration, branching

Programming constructs: sequence, iteration, branching.

2.2.1 Programming Techniques

Programming Techniques — Note 1 of 6

Candidates need to understand the constructs of sequence, iteration and branching, and use them independently and in combination to produce a solution. This includes IF (with ELSEIF and ELSE) and select case statements, and both condition-controlled (e.g. while, repeat until) and count-controlled (e.g. FOR) iteration. Candidates need to be able to read, create and trace code (for example using a trace table) that uses these constructs.

What you need to know

All programs are built from three constructs: sequence, iteration and branching (selection). You need to use them independently and in combination, and read, create and trace code that uses them.

What to know

  • A condition-controlled loop can be used as a count-controlled loop by updating a counter inside it.
  • Constructs can be nested, e.g. a selection statement inside a loop.

Key terms

Exam tipCommon mistake

Sequence

Instructions executed one after another in order.

Branchingselection

Choosing which code to run based on a condition, using if ... then ... elseif ... else ... endif or switch/case.

Iteration

Repeating code. Count-controlled loops (for) repeat a set number of times; condition-controlled loops (while, do ... until) repeat until a condition changes.

  • A while loop checks its condition BEFORE each iteration (it may run zero times); a do ... until loop checks AFTER (it always runs at least once).
  • Common mistake: Initialising a counter or total inside a loop, so it is reset every iteration.

Exam technique

  • Tip:Use a trace table to follow code that combines these constructs.

Code examples

Sequence, selection and iteration in OCR pseudocode
text
total = 0
for i = 1 to 5
    mark = int(input("Enter mark: "))
    if mark >= 0 AND mark <= 100 then
        total = total + mark
    else
        print("Invalid mark")
    endif
next i

do
    answer = input("Continue? (y/n) ")
until answer == "y" OR answer == "n"

switch answer:
    case "y":
        print("Continuing")
    default:
        print("Goodbye")
endswitch
← Previous topicOutline the benefits and trade offs of concurrent processing in a particular situation2.1.5 Thinking ConcurrentlyNext note →Recursion, how it can be used and how it compares to an iterative approach2.2.1 Programming Techniques

Back to Programming Techniques

Programming Techniques

Note 1 of 6

  1. 1Programming constructs: sequence, iteration, branching
  2. 2Recursion, how it can be used and how it compares to an iterative approach
  3. 3Global and local variables
  4. 4Modularity, functions and procedures, parameter passing by value and by reference
  5. 5Use of an IDE to develop/debug a program
  6. 6Use of object oriented techniques
Topic overview