x

Revision notes for Edexcel GCSE Computer Science Writing programs with control constructs. Open the guide for explanations and worked examples. Written against the Edexcel GCSE Computer Science (1CP2) specification, so the content matches what's examinable rather than general Computer Science background.

Writing programs with control constructs

What you'll learn

  • How sequencing, selection and repetition control the order a program runs in.
  • How to choose between count-controlled loops, condition-controlled loops and iteration over a data structure.
  • How Python 3 uses indented code blocks.
  • How to write clearer programs using single entry and single exit points.

The starting point: program flow

A statement is one instruction in a program, such as assigning a value, asking for input or printing output.

Program flow means the order in which statements are executed. By default, Python runs statements from top to bottom, but control constructs can change that order.

Definition

Control construct

A control construct is a programming structure that controls which statements run, how many times they run, and in what order.

This diagram gives the big picture of the control constructs you choose from when writing a program.

Flowcharts showing sequencing, selection, count-controlled repetition, condition-controlled repetition and iteration over a data structure

Sequencing

Sequencing means statements are executed one after another in the order they are written.

balance = 20
balance = balance - 6
balance = balance * 2
print(balance)

The second line uses the current value of balance, then stores the new value back into balance.

Example

Tracing a short sequence

  1. The first statement stores 20 in balance.
  2. The second statement uses that value, subtracts 6, and stores 14 back in balance.
  3. The third statement uses the updated value, doubles it, and stores 28.
  4. The final statement outputs 28.
Key Idea

Sequence matters

Changing the order of statements can change the result, especially when variables are updated.

Selection

Selection means the program chooses between different paths.

In Python, selection is written using if, elif and else.

mark = int(input("Enter mark: "))

if mark >= 80:
    grade = "distinction"
elif mark >= 50:
    grade = "pass"
else:
    grade = "resit"

print(grade)

A condition is an expression that evaluates to either True or False, such as mark >= 50.

You can combine conditions using logical operators. In algorithms you may see AND, OR and NOT; in Python 3 these are written as and, or and not.

Example

Choosing a selection branch

For mark = 63:

  1. Test mark >= 80. This is false, so the if block is skipped.
  2. Test mark >= 50. This is true, so grade = "pass" runs.
  3. Because one branch has been chosen, the else branch is not run.
Common Mistake

Testing the same value twice in the wrong order

If you test mark >= 50 before mark >= 80, then a mark of 90 will be classified as "pass" before the program ever reaches the distinction test. Put the most specific or highest boundary first when using elif.

Repetition

Repetition means running the same block of code more than once. A repeated block is often called a loop.

Count-controlled repetition

A count-controlled loop is used when the number of repetitions is known before the loop starts.

In Python, this is usually a for loop with range().

total = 0

for week_number in range(4):
    hours = int(input("Hours this week: "))
    total = total + hours

print(total)

This repeats exactly 4 times.

Common Mistake

range stops before the end value

range(4) gives 4 repetitions: 0, 1, 2, 3. range(1, 4) gives 1, 2, 3, so it only repeats 3 times.

Condition-controlled repetition

A condition-controlled loop is used when the program should keep repeating while a condition is true, or until a condition becomes true.

In Python, this is usually a while loop.

valid_mark = False

while not valid_mark:
    mark = int(input("Enter a mark from 0 to 100: "))

    if mark >= 0 and mark <= 100:
        valid_mark = True
    else:
        print("Invalid mark")

This loop may run once, many times or, if the condition is never changed properly, forever.

Example

Choosing a loop type

A program must collect marks for 10 students, but each mark must be re-entered until it is between 0 and 100.

  1. The program knows there are exactly 10 students, so the outer loop should be count-controlled.
  2. The program does not know how many attempts each student will need to enter a valid mark, so the validation loop should be condition-controlled.
  3. The condition-controlled loop belongs inside the count-controlled loop, because each student needs their own valid mark.
Tip

Choosing the right construct

  • Use sequence for steps that always happen in order.
  • Use selection when the program must choose a path.
  • Use count-controlled repetition when you know how many times to repeat.
  • Use condition-controlled repetition when repetition depends on a condition changing.
  • Use iteration over a data structure when you need to process every stored item.

Iteration over every item in a data structure

A data structure is a way of storing multiple values together, such as a Python list.

Iteration over a data structure means visiting each item in turn and applying the same logic to it.

marks = [42, 50, 79]
pass_count = 0

for mark in marks:
    if mark >= 50:
        pass_count = pass_count + 1

print(pass_count)

This is different from for number in range(3). Here, the loop variable mark directly takes each value from the list.

Example

Counting passes in a list

For marks = [42, 50, 79]:

  1. Start with pass_count as 0.
  2. Visit 42 first. It is not at least 50, so the count stays 0.
  3. Visit 50 next. It is at least 50, so the count becomes 1.
  4. Visit 79 last. It is at least 50, so the count becomes 2.

Code blocks and indentation

A code block is a group of statements controlled by the same construct. In Python, indentation shows which statements belong to a block.

if temperature > 30:
    print("Hot day")
    print("Drink water")

print("Weather check complete")

Only the two indented print() statements are inside the if block. The final statement is not indented, so it runs after the selection has finished.

Common Mistake

Indentation changes the meaning

In Python, indentation is not decoration. If a statement is indented under an if, for or while, it belongs to that block and may run differently.

Single entry and single exit points

A single entry point means there is one clear way into a block or subprogram. A single exit point means there is one clear way out.

A subprogram is a named section of code that can be called from elsewhere. In Python, subprograms are written using def. A function returns a value; a procedure-style subprogram performs actions and may not return a useful value.

def grade_from_mark(mark):
    grade = ""

    if mark >= 80:
        grade = "distinction"
    elif mark >= 50:
        grade = "pass"
    else:
        grade = "resit"

    return grade

This function has one entry point: the first line inside the function. It also has one main exit point: return grade.

Example

Writing a single-exit function

  1. Decide the function’s job: convert one mark into one grade.
  2. Use selection to store the correct value in grade, rather than returning immediately from each branch.
  3. Return grade once at the end, so the exit point is easy to find and trace.
Key Idea

Structured programs are easier to trace

Single entry and single exit points make programs easier to read, test and debug because the flow of control is predictable.

Putting the constructs together

Here is a small Python 3 program that uses the constructs appropriately.

def grade_from_mark(mark):
    grade = ""

    if mark >= 80:
        grade = "distinction"
    elif mark >= 50:
        grade = "pass"
    else:
        grade = "resit"

    return grade

marks = []
student_count = int(input("How many students? "))

for student_number in range(student_count):
    valid_mark = False

    while not valid_mark:
        mark = int(input("Enter a mark from 0 to 100: "))

        if mark >= 0 and mark <= 100:
            valid_mark = True
        else:
            print("Invalid mark")

    marks.append(mark)

pass_count = 0

for mark in marks:
    if mark >= 50:
        pass_count = pass_count + 1

print("Passes:", pass_count)

for mark in marks:
    print(mark, grade_from_mark(mark))

This program uses:

  • Sequencing: setup happens before input, input happens before output.
  • Selection: marks are checked and grades are chosen.
  • Count-controlled repetition: the program repeats once per student.
  • Condition-controlled repetition: invalid marks are requested again.
  • Iteration over a data structure: every mark in the list is processed.
  • Single entry/exit subprogram style: grade_from_mark() has one clear return point.
Exam technique

In the exam

  1. Match the construct to the wording: “exactly 5 times” suggests count-controlled repetition; “until valid” suggests condition-controlled repetition; “for each item” suggests iteration over a data structure.
  2. Check indentation carefully in Python questions, because it decides which statements belong inside a block.
  3. When writing a subprogram, make its purpose clear, use meaningful variable names, and avoid scattered exit points.
Self review

Check yourself

  • When would you choose a while loop instead of a for loop with range()?
  • What is the difference between iterating over range(5) and iterating over a list such as marks?
  • Why can multiple exit points make a subprogram harder to trace?
You've reached the end

Test yourself on this topic, or move on to the next guide.

FlashcardsSelf-test with active recall
Primitive and structured data typesUp next

How was this guide?

Writing programs with control constructs Revision Guide

  1. GCSE
  2. /Computer Science
  3. /Writing programs with control constructs