Nim by Example: Case

Using a case statement is another way of branching control flow.
A sample usage of case.
let i = 1
case i:
of 1:
    echo "i is 1"
of 2:
    echo "i is 2"
else:
    echo "i is not 1 or 2"
With integers (and other ordinal types), of branch can specify a range.
let j = 15
case j:
of 1..10:
    echo "j is between 1 and 10"
else:
    echo "j is not between 1 and 10"
Conditions for the same of branch are separated with a comma.
let k = "good"
case k:
of "cool", "good":
    echo "k is cool or good"
For ordinal types (i.e. booleans, integers, chars), case must cover all possible cases. An else branch covers all the remaining cases not specified with of branches. Its body can be declared empty by using the discard statement.
let m = 'a'
case m:
of 'a'..'z':
    echo "variable m holds a letter"
else:
    discard
$ nim c -r case.nim
i is 1
j is not between 1 and 10
k is cool or good
variable m holds a letter

Previously: If/Else
Next up: Arrays