In Scala, you can use the switch statement as an alternative to else if statements to simplify your code. The switch statement is similar to match expressions in Scala, but it is used for testing against a single value, like an integer or a string.
Here's an example of using switch as else if:
val x = 5
x match {
case 1 => println("One")
case 2 => println("Two")
case 3 => println("Three")
case _ if x > 3 => println("Greater than three")
case _ => println("Not a valid number")
}
In this example, we use the match keyword to test the value of x. We have four cases that match specific values (1, 2, and 3), and one case that matches any value greater than 3.
The _ is used as a wildcard, which matches any value. In the second to last case, we add a guard (if x > 3) to match any value greater than 3.
We can also use switch instead of match. Here's an example:
val x = "dog"
x match {
case "cat" => println("Meow")
case "dog" => println("Woof")
case "bird" => println("Tweet")
case _ => println("Unknown animal")
}
In this example, we test the value of x against three possible string values: "cat", "dog", and "bird". If x matches any of those values, we print the corresponding animal sound. If it doesn't match any of those values, we print "Unknown animal".
Note that in Scala, switch is implemented using pattern matching under the hood.