...

/

Switch Statements in Scala

Switch Statements in Scala

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

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 ...

Access this course and 1400+ top-rated courses and projects.