Switch Statements
Switch statements provide a clean way to handle multiple possible values for a single variable, making them ideal for menus, state machines, and option processing.
Basic Switch Statement
Section titled “Basic Switch Statement”output "Enter a day number (1-7): "input dayNumberdayNumber = convert dayNumber to number
switch dayNumber { case 1 output "Monday" jump case 2 output "Tuesday" jump case 3 output "Wednesday" jump case 4 output "Thursday" jump case 5 output "Friday" jump case 6 output "Saturday" jump case 7 output "Sunday" jump default output "Invalid day number"}Switch Keywords
Section titled “Switch Keywords”Defines a value to match:
case 1 output "Option one" jumpJump (or Break)
Section titled “Jump (or Break)”Exits the switch statement after executing a case:
case 2 output "Option two" jump // or breakDefault
Section titled “Default”Executes if no case matches:
default output "No match found"Exit/Stop/Return
Section titled “Exit/Stop/Return”Terminates the entire program:
case 9 output "Exiting program..." exit // or stop, or returnSimple Menu Example
Section titled “Simple Menu Example”output "=== Main Menu ==="output "1. New Game"output "2. Load Game"output "3. Settings"output "4. Exit"output ""output "Enter choice: "input choicechoice = convert choice to number
switch choice { case 1 output "Starting new game..." jump case 2 output "Loading game..." jump case 3 output "Opening settings..." jump case 4 output "Goodbye!" exit default output "Invalid choice. Please enter 1-4."}Switch with Strings
Section titled “Switch with Strings”output "Enter a command (start/stop/pause/resume): "input commandcommand = command to lowercase
switch command { case "start" output "Starting process..." jump case "stop" output "Stopping process..." jump case "pause" output "Pausing process..." jump case "resume" output "Resuming process..." jump default output "Unknown command: {command}"}Calculator Example
Section titled “Calculator Example”output "Simple Calculator"output "Enter first number: "input num1Inputnum1 = convert num1Input to number
output "Enter operation (+, -, *, /): "input operation
output "Enter second number: "input num2Inputnum2 = convert num2Input to number
switch operation { case "+" result = num1 + num2 output "{num1} + {num2} = {result}" jump case "-" result = num1 - num2 output "{num1} - {num2} = {result}" jump case "*" result = num1 * num2 output "{num1} * {num2} = {result}" jump case "/" if num2 equals 0 output "Error: Division by zero" else { result = num1 / num2 output "{num1} / {num2} = {result}" } jump default output "Invalid operation"}Traffic Light Controller
Section titled “Traffic Light Controller”currentLight = "green"
output "Current light: {currentLight}"output "What should happen? (next/emergency/reset): "input action
switch action { case "next" switch currentLight { case "green" currentLight = "yellow" output "Changed to yellow" jump case "yellow" currentLight = "red" output "Changed to red" jump case "red" currentLight = "green" output "Changed to green" jump } jump case "emergency" currentLight = "red" output "Emergency! All lights red!" jump case "reset" currentLight = "green" output "Reset to green" jump default output "Invalid action"}Grade Converter
Section titled “Grade Converter”output "Enter letter grade (A, B, C, D, F): "input letterGradeletterGrade = letterGrade to uppercase
switch letterGrade { case "A" output "Excellent! Score range: 90-100" output "GPA: 4.0" jump case "B" output "Good! Score range: 80-89" output "GPA: 3.0" jump case "C" output "Average. Score range: 70-79" output "GPA: 2.0" jump case "D" output "Below average. Score range: 60-69" output "GPA: 1.0" jump case "F" output "Failing. Score range: 0-59" output "GPA: 0.0" jump default output "Invalid grade. Please enter A, B, C, D, or F"}Month Days Calculator
Section titled “Month Days Calculator”output "Enter month number (1-12): "input monthInputmonth = convert monthInput to number
output "Enter year: "input yearInputyear = convert yearInput to number
// Check if leap yearisLeapYear = falseif year % 4 equals 0 { if year % 100 equals 0 { if year % 400 equals 0 isLeapYear = true } else isLeapYear = true}
switch month { case 1 output "January: 31 days" jump case 2 if isLeapYear output "February: 29 days (leap year)" else output "February: 28 days" jump case 3 output "March: 31 days" jump case 4 output "April: 30 days" jump case 5 output "May: 31 days" jump case 6 output "June: 30 days" jump case 7 output "July: 31 days" jump case 8 output "August: 31 days" jump case 9 output "September: 30 days" jump case 10 output "October: 31 days" jump case 11 output "November: 30 days" jump case 12 output "December: 31 days" jump default output "Invalid month number"}Vending Machine
Section titled “Vending Machine”constant SODA_PRICE = 1.50constant CHIPS_PRICE = 1.25constant CANDY_PRICE = 1.00constant WATER_PRICE = 1.00
output "=== Vending Machine ==="output "1. Soda - ${SODA_PRICE}"output "2. Chips - ${CHIPS_PRICE}"output "3. Candy - ${CANDY_PRICE}"output "4. Water - ${WATER_PRICE}"output ""output "Enter selection: "input selectionselection = convert selection to number
output "Enter payment amount: "input paymentInputpayment = convert paymentInput to number
price = 0itemName = ""
switch selection { case 1 price = SODA_PRICE itemName = "Soda" jump case 2 price = CHIPS_PRICE itemName = "Chips" jump case 3 price = CANDY_PRICE itemName = "Candy" jump case 4 price = WATER_PRICE itemName = "Water" jump default output "Invalid selection" exit}
if payment < price { shortage = price - payment output "Insufficient funds. Need ${shortage} more."} else { change = payment - price output "Dispensing {itemName}" if change > 0 output "Your change: ${change}" output "Thank you!"}ATM Machine
Section titled “ATM Machine”balance = 1000.00
output "=== ATM Menu ==="output "1. Check Balance"output "2. Deposit"output "3. Withdraw"output "4. Exit"output ""output "Enter choice: "input choicechoice = convert choice to number
switch choice case 1 output "Current balance: ${balance}" jump case 2 output "Enter deposit amount: " input depositInput deposit = convert depositInput to number
if deposit > 0 { balance = balance + deposit output "Deposited: ${deposit}" output "New balance: ${balance}" } else output "Invalid deposit amount" jump case 3 output "Enter withdrawal amount: " input withdrawInput withdraw = convert withdrawInput to number
if withdraw > balance output "Insufficient funds" else if withdraw <= 0 output "Invalid withdrawal amount" else { balance = balance - withdraw output "Withdrawn: ${withdraw}" output "New balance: ${balance}" } jump case 4 output "Thank you for using our ATM" exit default output "Invalid option"Switch vs If-Else
Section titled “Switch vs If-Else”Use Switch When:
Section titled “Use Switch When:”- Testing a single variable against multiple constant values
- Handling menus or option selections
- Code clarity is improved
- Cases are simple comparisons
// Good use of switchswitch menuChoice case 1 startGame() jump case 2 loadGame() jump case 3 exitGame() jumpUse If-Else When:
Section titled “Use If-Else When:”- Testing complex conditions
- Comparing ranges
- Multiple variables involved
- Conditions are not constant values
// Better with if-elseif score >= 90 AND score <= 100 output "A"else if score >= 80 output "B"else if score >= 70 output "C"Nested Switch Statements
Section titled “Nested Switch Statements”output "Select category (1=Electronics, 2=Clothing): "input categoryInputcategory = convert categoryInput to number
switch category case 1 output "Electronics:" output "1. Laptop" output "2. Phone" output "3. Tablet" input itemChoice itemChoice = convert itemChoice to number
switch itemChoice case 1 output "You selected: Laptop" jump case 2 output "You selected: Phone" jump case 3 output "You selected: Tablet" jump default output "Invalid item" jump case 2 output "Clothing:" output "1. Shirt" output "2. Pants" output "3. Shoes" input itemChoice itemChoice = convert itemChoice to number
switch itemChoice case 1 output "You selected: Shirt" jump case 2 output "You selected: Pants" jump case 3 output "You selected: Shoes" jump default output "Invalid item" jump default output "Invalid category"Best Practices
Section titled “Best Practices”-
Always include default
switch valuecase 1// handle case 1jumpdefaultoutput "Unexpected value: {value}" -
Use jump/break to prevent fall-through
// Always end cases with jumpcase 1output "Case 1"jump // Important! -
Keep cases simple
// Goodcase 1processOption1()jump// Avoid complex logic directly in cases -
Validate input before switch
input choicechoice = convert choice to numberif is number(choice)switch choice// cases...elseoutput "Please enter a number" -
Use constants for case values
constant OPTION_NEW = 1constant OPTION_LOAD = 2constant OPTION_EXIT = 3switch choicecase OPTION_NEWstartNewGame()jumpcase OPTION_LOADloadGame()jumpcase OPTION_EXITexitGame()