Do Until (OCR ERL)
Do-Until Loops in OCR ERL
Welcome to the world of do-until loops! This is a special type of loop that's guaranteed to run at least once, making it perfect for situations where you need something to happen before checking a condition.
⚠️ IMPORTANT: This page uses OCR Exam Reference Language (ERL) syntax, which is DIFFERENT from Python. The rest of this site uses Python, but for this topic we're using OCR ERL as specified in the exam board documentation.
⚠️ Why OCR ERL and Not Python?
Unlike the other pages on this site, this page uses OCR Exam Reference Language (ERL) syntax, not Python. Here's why:
| Language | Has Do-Until? |
|---|---|
| Python | ❌ No! Python doesn't have do-until loops (you'd need to use while with a break) |
| OCR ERL | ✅ Yes! Has do-until loops that you need to know for your exam |
Remember: OCR ERL is the pseudocode language used in OCR Computer Science exams. It's designed to be language-independent but looks similar to many programming languages. You won't run this code in Python—it's for understanding the concept of do-until loops.
💻 Want to practice coding in OCR ERL? Visit examreferencelanguage.co.uk where you can write and test OCR ERL code in your browser!
Do-Until: Run First, Check Later
A do-until loop is a type of iteration where the code inside the loop runs at least once, and then keeps repeating until a condition becomes true.
The basic structure in OCR ERL is:
do
// code to repeat
answer = input("Enter answer: ")
until answer == "correct"
Key parts:
do- Starts the loop (code runs immediately)- Indented code - The statements that will repeat
until condition- Loop continues until this condition is TRUE
Important: The condition is checked after the code runs, so the loop body executes at least once, even if the condition starts as true!
🔄 While vs Do-Until: The Critical Difference
The key difference between while and do-until is when the condition is checked:
| Loop Type | When Checked | Minimum Runs |
|---|---|---|
while |
BEFORE the loop body runs | 0 times (might not run at all!) |
do-until |
AFTER the loop body runs | At least 1 time (always runs once!) |
Example comparison:
// While loop - checks FIRST
while answer == "correct":
print("Well done!")
endwhile
// Do-until loop - checks AFTER
do
answer = input("Guess: ")
until answer == "correct"
In the while loop, if answer isn't "correct" at the start, the loop never runs. In the do-until loop, the user gets asked at least once, no matter what!
Indentation & Structure in OCR ERL
OCR ERL uses Python-style indentation to show which code belongs inside the loop.
Indentation rules for do-until:
- Write
doon its own line - Indent all code that should repeat
- Write
until conditionat the same level asdo
do
name = input("Enter name: ")
print("Hello " + name)
until name == "quit"
Notice: do and until are at the same indentation level, and the code between them is indented once.
Complete Example 1: Simple Loop
Let's start with a simple OCR ERL program using a do-until loop:
print("Keep guessing!")
do
guess = input("What is the best TV show? ")
print("You guessed: " + guess)
until guess == "spongebob squarepants"
print("Correct! You got it!")
This program:
- Prints "Keep guessing!"
- Starts the do-until loop (runs at least once)
- Asks the user for their guess
- Shows them what they guessed
- Checks if they entered "spongebob squarepants" - if not, loops back to step 3
- When they get it right, exits the loop and prints "Correct! You got it!"
Key point: The user will be asked at least once, even if the condition somehow started as true, because the check happens after the code runs!
Your turn! Think about situations where you need to do something at least once before checking a condition. Press inspire me to get started with practice questions!
Complete Example 2: With Selection
Here's a more advanced example that includes selection (if-else) inside the loop:
print("Guess the best TV show!")
do
guess = input("What is the best TV show? ")
if guess == "spongebob squarepants":
print("Correct! Well done!")
else:
print("Nope! Try again.")
endif
until guess == "spongebob squarepants"
print("You got it eventually!")
This program:
- Prints an introduction
- Starts the do-until loop (runs at least once)
- Asks the user for their guess
- Uses selection to check if they got it right and gives appropriate feedback
- Repeats until they enter "spongebob squarepants"
- Prints a final message after they succeed
Notice: This example combines iteration (the do-until loop) with selection (the if-else statement) to give the user feedback while they're still guessing!
Tip: Start with Example 1 to understand the basics, then come back to this one to see how you can combine do-until with other programming constructs!
Write a program to...
💻 Test this code: Copy the code above and paste it into examreferencelanguage.co.uk to run it and see how do-until loops work in OCR ERL!