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?
Output
Step 1: The program starts. There is one way in.
Press Next and watch the glowing box travel back to 'while not_guessed?' before the loop is left.
Where structured programming came from
Reason it through
Why does structured programming use selection and loops instead of go-to statements?
First link · your turn
Before structured programming, how did programmers repeat a section of code?
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
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?
In structured programming, when should a while loop end?
Why is using break to end a while loop a deviation from structured programming?
Can Python be used for structured programming?
What is the job of a flag like not_guessed in a structured guessing loop?
What did programmers use go-to statements for before structured programming?
What is spaghetti code?
Which statements does structured programming use to control the flow of a program?
What is a subprogram?
How do you return a value from a subprogram?
When can the main program use a variable that was made inside a subprogram?
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 postedMore Edexcel GCSE Computer Science topics
- Programming – selection
- Programming – sequencing
- Programming arithmetic operators
- Programming logical operators
- Programming relational operators
- Reading, writing, analysing and refining programs
- String manipulation
- Structural components of programs
- Structured data types
- User input handling
- Validation – length check
- Validation – presence check
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.