This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.13, “How to add 'if' expressions (guards) to case statements.”
You want to add qualifying logic to a case statement in a
match expression, such as allowing a range of numbers, or matching a pattern, but only if that pattern matches some additional criteria.
if guard to your
case statement. Use it to match a range of numbers: