switch Statement
The switch statement selects a block of code to execute based on the value of an expression.
switch expression do
case value1 then
# body
case value2, value3 then
# body
default then
# body
end
Syntax rules
- The
dokeyword follows the switch expression - Each
caseblock usesthento open and is implicitly terminated by the nextcase,default, or the closingend - Multiple values per case are comma-separated
- The
defaultclause is optional and handles unmatched values - The outer
switchblock is closed withend
No fall-through
Only the matched case executes. There is no fall-through between cases, and no break is needed.
switch Data.action do
case "create" then
Data.status = "created"
case "update", "patch" then
Data.status = "updated"
default then
Data.status = "unknown"
end
In this example, if Data.action is "update" or "patch", only the second case body executes. The default case is skipped.
No match without default
If no case matches and there is no default clause, execution silently continues past the switch statement:
switch Data.status do
case "active" then
Data.label = "Active"
case "inactive" then
Data.label = "Inactive"
end
# If Data.status is "pending", execution continues here with no case executed