...

/

Switch Statements in Scala

Switch Statements in Scala

Learn about Match statements that are used in place of Switch statements in Scala.

We'll cover the following...

Match is switch on steroids

You can use the match statement much like a switch statement. Check out the following code for an example of the syntax:

Press + to interact
object Main {
def main(args: Array[String]) {
println(wordFor(1));
}
// method containing match keyword
def wordFor(number:Int) = number match {
case 1 => "one"
case 2 => "two"
case 3 => "three"
case _ => "unknown"
}
}

There’s no need for a return or break keyword because the match statement actually returns whatever value is returned from the matched case ...