Community resourceWorksheet

J277 2.2.1 Combining and nesting programming constructs

Part 7 of 7 · J277 2.2.1-2.2.2 · Programming fundamentals

Putting sequence, selection and iteration together, and reading the nesting that results.

Students will:

  • combine the three constructs in one program
  • read nested code from the outside in
  • use indentation to control what belongs to each construct
  • count values that meet a condition inside a loop
  • find and fix indentation faults

Inside: 6 explanation cells, 3 runnable Python tasks, 2 multiple-choice questions and 2 written answers. 16 marks, about 45 minutes.

Series: J277 2.2.1-2.2.2 · Programming fundamentals, part 7 of 7.

Shared by Coding PathwayVerified teacher

  • 13 cells
  • About 45 minutes
  • CC BY-SA 4.0
  • Shared 17 Aug 2026
  • Updated 9 Sept 2026

Preview

The whole resource, exactly as a class sees it. Answers and marking are held back.

Combining and nesting programming constructs

Real programs combine sequence, selection and iteration. Nesting places one construct inside another, shown in Python by additional indentation. An if nested inside a loop makes a decision during each repetition; a loop nested inside another loop completes its repetitions for every outer value. Read nested code from the outside in and trace one loop repetition at a time.

Multiple choice1 mark

What does an if statement indented inside a for loop allow a program to do?

  • AMake a decision for every item processed by the loop
  • BRun the loop only once
  • CTurn every value into a constant
  • DRemove the need for a condition

Worked example: selection inside iteration

The program below processes four scores. The loop visits every score, while the nested if increases passes only when the current score is at least 50.

Predict which two scores change the counter and the final value displayed. Then run the code and use the indentation to explain why the final print runs only once.

Worked example
scores = [42, 71, 65, 38]
passes = 0
for score in scores:
    if score >= 50:
        passes = passes + 1
print(passes)

Follow the indentation

The if statement is inside the loop, so it is tested once for each score. The final print is outside both constructs, so it runs once after all scores have been processed.

When writing an exam answer, plan the outer structure first. Then indent the repeated or conditional steps consistently.

Multiple choice1 mark

What does the worked program output?

  • A4
  • B2
  • C1
  • D216

Supported practice: count matching values

Count how many stored temperatures are below zero. Initialise below_zero once, loop through every temperature, and increase the counter only when the current value is negative.

The values -2 and -5 meet the condition, so the final count should be 2. Zero itself is not below zero.

Coding task3 marks
# Count how many temperatures are below zero.
temperatures = [4, -2, 7, -5, 0, 3]
below_zero = 0
# Use a loop containing selection.

Independent practice: nested loops

A small theatre has rows 1 to 3 and seat numbers 1 to 4. Generate every (row, seat) pair and append it to seats.

seats = [] creates an empty list. Calling seats.append((row, seat)) adds one paired row-and-seat position. You do not need to study tuples separately for this task; the brackets simply keep the two coordinates together.

Use an outer loop for rows and an inner loop for seats. The inner loop must complete all four seats for each row, producing twelve positions from (1, 1) to (3, 4).

Coding task3 marks
# Generate coordinate pairs for rows 1 to 3 and seats 1 to 4.
seats = []
# Use one loop nested inside another and append (row, seat).
Written answer3 marks

Explain why the inner loop in the seat program runs twelve times in total even though its own range contains only four values.

Link the three outer-loop repetitions to the four inner-loop repetitions.

Students type their answer here.

Written answer5 marks

A program should repeatedly ask for a score until -1 is entered. For every other score, it should count a pass when the score is at least 50. Describe the outer loop condition, the selection inside it and where the next score must be input.

State when repetition continues, what value is excluded, when the counter changes and how the loop receives an updated value.

Students type their answer here.

Debugging indentation

If a statement runs too often or not often enough, inspect its indentation. Moving print(total) into a loop changes it from one final output to an output on every repetition.

When combining constructs, identify the outer structure first and then ask what must happen during each repetition or branch. Indentation communicates that relationship in Python and is therefore part of the program's logic.