Switch-Case in OCR ERL
Welcome to the world of multi-way decision-making! Today, we're going to learn about switch-case statements, which allow your program to choose between multiple options based on a single value.
⚠️ 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 Switch-Case? |
|---|---|
| Python | Not until Python 3.10+ (and it's called "match-case", not "switch-case") |
| OCR ERL | ✅ Yes! Has traditional switch-case statements 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 Thonny. It's for understanding the concept of switch-case statements.
💻 Want to practice coding in OCR ERL? Visit examreferencelanguage.co.uk where you can write and test OCR ERL code in your browser!
Switch-Case: Choosing From Multiple Options
A switch-case statement allows you to test a variable against multiple values and execute different code for each value. It's like a menu where you pick one option from many.
The basic structure in OCR ERL is:
switch variable_name:
case "value1":
print("Response for value1")
case "value2":
print("Response for value2")
default:
print("Response for any other value")
endswitch
Key parts:
switch variable_name:- Starts the switch statement with the variable to testcase "value":- Each case tests for a specific value. (like if ==)default:- Runs if none of the cases match (like "else")endswitch- Marks the end of the switch statement
Indentation & Colons in OCR ERL
OCR ERL uses Python-style indentation to show which code belongs where. This is for readability!
Indentation style guide:
- After
switch variable_name:put a colon and indent the next line - Each
caseanddefaultline is indented once - The code inside each case is indented twice (or once more than the case)
endswitchreturns to the original indentation level
switch day:
case "Sat":
print("Saturday")
case "Sun":
print("Sunday")
default:
print("Weekday")
endswitch
Notice the pattern: switch line → case lines indented once → print statements indented twice → endswitch back to start.
Complete Example
Let's see a full OCR ERL program using switch-case:
best_show = input("What is the best TV show? ")
switch best_show:
case "spongebob":
print("Absolutely! Spongebob Squarepants is glorious")
case "stranger things":
print("Good show, but not the best!")
default:
print(best_show + "?!?! Nope! It's Spongebob!")
endswitch
This program:
- Asks the user to input their answer
- Uses switch-case to check what they entered:
- If it matches "spongebob" exactly, it agrees enthusiastically
- If they said "stranger things", it gives a different response
- For any other answer (default), it disagrees and tells them the correct answer
Your turn! Think about how you could use switch-case to create a menu system, a day-of-the-week program, or a simple game choice system. Press inspire me to get started with practice questions!
Write a program to...
💻 Test this code: Copy the code above and paste it into examreferencelanguage.co.uk to run it and see how switch-case works in OCR ERL!