GCSE · Computer Science · Edexcel · Spec 1CP2

Single entry/exit code blocks

Two guessing games, both stop on the right guess. Only one follows the structured rule. Spot where each leaves its loop.

Computer Science · Programming

Follow the guessing game out of its loop

The secret is 7. You guess 3, then 7. Step through and spot the one box the loop leaves from.

Before you step: which box will the loop actually leave from?

YesNoYesNoStartsecret = 7,not_guessed =Truewhile not_guessed?guess = INPUTguess == secret?not_guessed =FalseOUTPUTCorrect!End

Output

 

Step 1: The program starts. There is one way in.

1 / 12

Press Next and watch the glowing box travel back to 'while not_guessed?' before the loop is left.

Step 1 of 12: The program starts. There is one way in..

Exam line: In a structured while loop you can always point at the single exit: the condition test, answering No because the condition is now False.
Watch out: The correct guess does not leave the loop by itself. It only changes not_guessed; the loop leaves when control gets back to the test.

Same game, two ways out

With breakvsWith not_guessed

Read across each row. The first two rows are about the exit.

Focus

What ends the loop

With break

The break statement, the moment the guess is right

With not_guessed

The condition not_guessed becoming False

The insight

With while True the condition can never be False, so the loop only stops because break forces it to.

Where control leaves

With break

From inside the body, part-way through the loop

With not_guessed

At the while test, once the body has finished

Does it work?

With break

Yes. The game still ends on a correct guess.

With not_guessed

Yes. Exactly the same behaviour.

Follows structured programming?

With break

No. It is a deviation, because it forces the loop to stop part-way through.

With not_guessed

Yes. One entry, one exit: it ends when its condition is False.

Where structured programming came from

?

Reason it through

Why does structured programming use selection and loops instead of go-to statements?

Link 1 of 3

First link · your turn

Before structured programming, how did programmers repeat a section of code?

2
Locked — reveal the link above first
3
Locked — reveal the link above first

Computer Science · Subprograms

Build a subprogram that hands back its answer

A subprogram (also called a subroutine) finds the highest number in a range of numbers and hands it back. Fill in the two missing lines.

  1. def highest(numbers):A subprogram with one specific task: find the highest number.
  2. biggest = numbers[0]Start by treating the first number as the biggest so far.
  3. for n in numbers:
  4. if n > biggest:
  5. biggest = nA larger number replaces biggest. The answer now lives in biggest.
  6. missing step
Which line is step 6?

Exam line: return is followed by the name of the variable. The main program can only use a subprogram's variable if it is returned.

Computer Science · Structured programming

Which idea is closest to yours?

Two versions of the guessing game both stop when you guess 7. One ends its loop with break. The other sets not_guessed to False.

Which idea is closest to what you think right now?
How sure are you?

Exam line: A structured while loop ends only when its condition evaluates to False.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Two programs that both work. The difference is where the loop is allowed to leave.

What you need to know

  • A properly structured program should have one entry point and one exit point.
  • In structured programming, a while loop should only end when its condition evaluates to False.
  • Have a goPriya writes while True: and says, 'My loop ends when its condition is False, so it's structured.' Is she right?

    No. True is never False, so that condition can't be what ends the loop.

    A structured while loop should only end when its condition evaluates to False, and a condition that is always True never gets the chance.

  • Python can be structured, but using break to end a while loop breaks the convention: it stops the loop part-way through.
  • The structured fix: use a flag such as not_guessed, repeat while it is True, and make it False on a correct guess.
  • Have a goSomeone needs three guesses to get it right. How many times does the while test see True before it finally sees False?

    Three times, then the fourth test sees False and the loop ends.

    Each wrong guess leaves the flag True, so the loop goes round again. The correct guess sets it to False, and the loop only notices when control returns to the test.

  • Before structured programming, programmers used go-to statements to repeat code, and too many made messy 'spaghetti code'.
  • Structured programming controls the flow with selection (if, elif, else) and loops (while, for) instead.
  • Programs are divided into subprograms (also called subroutines), each built for one specific task and callable many times.
  • Calling a subprogram many times reduces repeated code and go-to statements.
  • Have a goA program calls its subprogram average() three times. How many times is the averaging code actually written?

    Once. The call is repeated, not the code.

    A subprogram can be called multiple times, which is exactly how it reduces repeated code.

  • To pass a value back, write return followed by the variable's name; the main program can only use a subprogram's variable if it is returned.

The big picture

A structured code block has a single entry and a single exit. A while loop should only end when its condition is False, so a break statement, which leaves part-way through, deviates from structured programming. A flag like not_guessed lets the loop end through its own condition. Structured programming also replaced go-to jumps with selection and loops, and uses subprograms that return values.

Key points

1One entry, one exit: a structured while loop ends only when its condition is False.
2break leaves a loop part-way through, so it deviates from structured programming even though the loop stops.
3A flag such as not_guessed lets a loop end through its own condition.
4Selection and loops replaced go-to jumps; a subprogram hands a value back with return and the variable's name.

Worked example

Problem

A password program loops with while True and uses break when the password is right. Rewrite it so the loop ends through its own condition, using a flag called locked.

⚠ Watch out

Thinking break is just as good because the loop still stops. It does stop, but it leaves part-way through the body, which is a deviation from structured programming. Setting a flag such as not_guessed to False lets the loop end through its own condition.

🧠

Memory hook

A structured loop is a room with one door: you walk in at the while test and you walk out at the while test. break is a trapdoor in the floor.

✓

Check yourself

Cover the page. Pick any while loop and point to the single place it leaves. Then say what a break inside it would change.

Flashcards

(11)
How many entry points and exit points should a properly structured program have?
One entry point and one exit point.
In structured programming, when should a while loop end?
Only when its condition evaluates to False.
Why is using break to end a while loop a deviation from structured programming?
It forces the loop to stop part-way through instead of leaving when the condition is false.
Can Python be used for structured programming?
Yes, but it is possible to break the convention, for example with a break statement in a while loop.
What is the job of a flag like not_guessed in a structured guessing loop?
It is the loop's condition. The loop repeats while it is True, and the correct guess makes it False.
What did programmers use go-to statements for before structured programming?
To repeat sections of code.
What is spaghetti code?
Messy, hard-to-follow code, caused by using too many go-to statements.
Which statements does structured programming use to control the flow of a program?
Selection statements (if, elif, else) and loops (while, for).
What is a subprogram?
A smaller section of a program designed to do one specific task (also called a subroutine). It can be called many times, reducing repeated code and go-to statements.
How do you return a value from a subprogram?
Write return followed by the name of the variable. This passes the value back to the main program.
When can the main program use a variable that was made inside a subprogram?
Only if the subprogram returns it.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 21 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.