Loading…
Loading…
Loading…
OCR GCSE Computer Science · J277
OCR J277 Check the specification (PDF) (opens in a new tab)
Control flow is the order in which a program executes its statements. A program may carry out steps in order, choose between alternatives or repeat some steps. These behaviours are represented by three basic programming constructs: sequence, selection and iteration.
Real programs combine them. A quiz might ask a question, decide whether the answer is correct and repeat this process for several questions. The examples below use Python 3, a high-level programming language.
Sequence means executing statements one after another, in the order they appear. Each statement can use values produced by earlier statements.
count = 5
total = 2
count = count + total
print(count)
The program stores 5 in count, then 2 in total. It calculates their sum and replaces the value in count with 7. Finally, it displays 7.
The order matters: the values must be assigned before the calculation uses them. The assignment count = count + total first evaluates the expression on the right using the current values, then stores the result in the variable on the left.
Selection uses a condition to decide which statements to execute. A condition produces a Boolean result: True or False. For example, score >= 50 is true when the score is at least 50.
score = 65
if score >= 50:
print("Pass")
else:
print("Not yet passed")
print("Result recorded")
Python tests the condition after if. Here it is true, so the program displays Pass and skips the else branch. If the score were 42, it would display Not yet passed instead. In either case, it then displays Result recorded, because that final statement is outside the selection.
The colon introduces a block of statements. Indentation—the spaces at the start of a line—shows which statements belong to that block. Several statements can belong to one branch.
An if does not need an else: sometimes the program should perform an action only when a condition is true and otherwise move on. For more than two alternatives, Python provides elif, meaning “else if”:
if score >= 80:
print("High score")
elif score >= 50:
print("Pass")
else:
print("Not yet passed")
The tests are considered from top to bottom. Only the first matching branch executes. A score of 85 produces High score, even though it is also at least 50. These thresholds are program rules, not GCSE grade boundaries.
Iteration means repetition. The repeated statements form the loop body.
A count-controlled loop is appropriate when a count determines how many repetitions are needed—for example, displaying three numbered questions. Python can express this using for with range:
for question_number in range(1, 4):
print("Question", question_number)
print("Finished")
range(1, 4) supplies the integer values 1, 2 and 3: it includes the starting value but excludes the stopping value. On each repetition, question_number takes the next value and the indented statement executes.
The output is:
Question 1
Question 2
Question 3
Finished
The final output is outside the loop, so it runs once after all three repetitions. To repeat a task five times without needing numbering from 1, range(5) supplies five values: 0, 1, 2, 3 and 4.
A condition-controlled loop repeats according to a condition rather than a specified repetition count. It is useful when the number of repetitions is not known in advance, such as asking for an answer until an accepted response is entered.
Python's while loop checks its condition before each repetition. If the condition is true, it executes the body and returns to the test. If it is false, execution continues after the loop.
energy = 3
while energy > 0:
print("Move")
energy = energy - 1
print("Rest")
The first test uses energy = 3, so the body runs. Each repetition decreases energy by 1:
| Energy before the test | Is energy > 0? | What happens? |
|---|---|---|
| 3 | True | Display Move; energy becomes 2 |
| 2 | True | Display Move; energy becomes 1 |
| 1 | True | Display Move; energy becomes 0 |
| 0 | False | Leave the loop |
The program displays Move three times and then Rest. Although this particular example has a predictable number of repetitions, the while statement controls repetition by testing a condition.
A while loop tests before each repetition. The update returns execution to the test; a false condition leads out of the loop.
A while body may execute zero times. If energy starts at 0, the first test is false and the program goes straight to Rest.
The tested value must be able to change so that repetition can end. Here, removing the decrease would leave energy at 3 forever, producing an infinite loop.
For repeated input, the update is a new input rather than a calculation:
answer = input("Enter Y: ")
while answer != "Y":
answer = input("Enter Y: ")
print("Accepted")
!= means “not equal to”. The program keeps asking while the answer is not Y. Each new input replaces the previous answer, allowing the next test to produce a different result.
A condition-controlled loop can also test after its body. In OCR Exam Reference Language, a do ... until loop executes its body at least once and stops when its condition becomes true. This differs from Python's while, which tests first and continues while its condition is true.
Nesting means placing one construct inside another. Selection inside a loop makes a decision during every repetition.
scores = [42, 71, 65, 38]
passes = 0
for score in scores:
if score >= 50:
passes = passes + 1
print(passes)
Here, scores stores four values in a list. The for loop visits each value once. During each visit, the nested if tests the current score. Only 71 and 65 meet the condition, so passes increases twice and the final output is 2.
Sequence establishes the starting values; iteration processes the scores; selection decides whether to increase the counter. The extra indentation places the counter update inside the selection, which is itself inside the loop. The final output is outside both.
To practise these techniques in Python, run the examples and compare their outputs with the explanations. Then change one input or starting value at a time: try scores on either side of 50, alter the count-controlled range, or start the energy loop at 0. Predict the effect before running the changed program.
Get unlimited access to all revision notes, key terms, and exam tips.
if, optionally elif and else.| Loop | Control | Python example |
|---|---|---|
| Count-controlled | A count determines repetitions | for number in range(1, 4): |
| Condition-controlled | A condition determines continuation | while energy > 0: |
range(1, 4) supplies 1, 2 and 3. range(5) supplies 0 to 4.
while tests first and repeats while its condition is true: zero repetitions are possible.do ... until tests after the body: at least one repetition; stops when its condition is true.Follow indentation from the outside in. Selection inside a loop tests a condition on each repetition. Statements outside the loop execute after it finishes.
Get unlimited access to all revision notes, key terms, and exam tips.
Trace statements in execution order, recording variable changes and outputs separately.
In Python, use == to compare values and = to assign a value.
Check range boundaries: range(1, 4) supplies 1, 2 and 3, not 4.
Indent only the statements that belong inside a branch or loop. An output after a loop runs once; an output inside it runs on every repetition.
For a while loop, identify the starting value, continuation condition and update. Check that the update can eventually make the condition false.
Control flow
The order in which a program's statements are executed.
Sequence
A programming construct in which statements execute in order, one after another.
Selection
A programming construct that uses a condition to decide which statements to execute.
Condition
An expression that evaluates to True or False and can control a branch or loop.
Iteration
A programming construct that repeats a group of statements.
Count-controlled loop
A loop whose number of repetitions is determined by a count.
Condition-controlled loop
A loop whose repetition is controlled by whether a condition is true or false.
Loop body
The statements belonging to a loop, executed during each repetition.
Infinite loop
A loop that continues indefinitely because its stopping condition is never reached.
Nesting
Placing one programming construct inside another, such as selection inside a loop.
Put your knowledge into practice — try past paper questions for Computer Science
Control flow
The order in which a program's statements are executed.
Sequence
A programming construct in which statements execute in order, one after another.
Selection
A programming construct that uses a condition to decide which statements to execute.
Condition
An expression that evaluates to True or False and can control a branch or loop.
Iteration
A programming construct that repeats a group of statements.
Count-controlled loop
A loop whose number of repetitions is determined by a count.
Condition-controlled loop
A loop whose repetition is controlled by whether a condition is true or false.
Loop body
The statements belonging to a loop, executed during each repetition.
Infinite loop
A loop that continues indefinitely because its stopping condition is never reached.
Nesting
Placing one programming construct inside another, such as selection inside a loop.