Match Expression in Scala

Hey there!

Welcome to ClearUrDoubt.com.

In this post, we will look at a program to demonstrate the Match Expression in Scala.

Match Expression is used to implement Java switch statement flow in Scala.

Let’s look at the sample program:

Output:

Match expression looks like this :

expression match { choices }

choices will use case statements to handle the matching expression.

case _ used for handling the values that are not matched the prior expressions. It is like default case handling in Java Switch statement.

Unlike in Java, the Match expression should cover all possible scenarios for matching, else we will end up getting an exception called MatchError. It’s always better to have a default case handler.

Happy Learning :).

Please leave a reply in case of any queries.

Leave a Reply

Your email address will not be published. Required fields are marked *