Kotlin Switch

If you are new to the Kotlin language, you might wonder how the switch statement works in the Kotlin language. Especially if you’ve already worked with other programming languages. In Kotlin, we don’t use a switch statement. Instead, Kotlin uses the “When” expression, which replaces the switch statement. In the article, we will have the concept of using the “when” keyword over switch. The switch is a loop case statement that iterates the values. However, Kotlin now utilizes “when” keywords instead of “switch”.

Syntax of the “when” Expression in Kotlin:

The general syntax of the “when” condition we used in Kotlin is given below:

when (expression) {
        value1 -> {
            // statements
        value2 -> {
            // statements
               else ->
            // when no value matches then else will execute

When a particular condition is met, a specific block of code must be run. The “when” statement compares all of the branches. It executes the code at the end of the “when” block after finding the first match. We don’t require a “break” statement in the “when” expression at the end of each case, as we did with previous programming languages’ switch cases.

How to Use the “when” Expression in Kotlin:

To understand the usage of when expression in Kotlin language, we should take the following examples provided:

Example # 1: Program of Using When as an Expression in Kotlin:

A simple example of a “when” expression in Kotlin is shown below. We will print the provided number information by using a “when” expression based on the value of a number.

In the previous example code, we have called the main function. We have defined two variables in the main function. The first variable is denoted with the keyword “var” and defined as “Integer”. The variable “integer” contains a value “3”. Then, we have another variable as “var”, which is defined as “required”. The “required” variable is initialized with the “when” expression. The “when” expression is passed with the “Integer” variable as an argument and return value with which the argument is matched. The println statement will print the match value result.

We have “3” as the integer value, which is matched in the “when” expression. We have the “3” value, as seen in the image below:

Example # 2: Program of Using When Without an Expression in Kotlin:

The “when” as an expression is not mandatory. We can use “when” just like any other language. Let’s execute the following example code of when without an expression:

In the previous code main function, we have defined a variable with the “var” keyword and assigned it as the name “Integer”. The variable “Integer” contains a number which is “5”. Note that we have called the “when” case without an expression. The “when” is then passed with the variable “Integer”, which will match the number given in the when condition. As we have a number “5” in the variable “Integer” that is the matched value in the “when” condition. It simply prints the value which is in the println function. Otherwise, the else will execute.

The output of the matching branch is shown in the following image:

Example # 3: Program of Using Multiple Statements of When in Kotlin:

Multiple statements can also be contained within a “when” condition block. We have multiple statement blocks in the following example:

In the example code, we have the main function called. Then, we have a variable with the keyword “var” and define it as “Month”. The variable “Month” is initialized with the number value “7”. The “when” condition is used in the code, which takes the variable “Month” as an argument and matches with the argument passed value. The match value is only followed by the condition “->”. Note that we have more than one statement in the “when” case. The only matched statement will execute.

The output gives the matched values shown on the following terminal screen:

Example # 4: Program of Using Multiple Branches of When in Kotlin:

A comma can be used to divide multiple condition branches in “when”. We can run the same logic for many options; we use the following method:

In the following code, we have the main function declaration. Then, we have a variable defined as “var” and assign the variable name as “Day”. We have initialized a variable “Day” with a value “6”. The “when” condition is used to take “Day” as an argument. Here, we have grouped two or more values separating them with the commas. These grouped values have equivalent println statements. If there are multiple matches, then the first branch is chosen. That means it’s important to note the order of the written branch.

Example # 5: Program of Using When to Check String Prefix in Kotlin:

The following method is used to check for a prefix in a string. We can also apply this method to check the string suffix, but we have checked the following prefix:

In the previous code example, we have a function as “hasPrefix” initialized with the “when” condition. The “when” condition will match the value of the string with the specified prefix “kotlin”. Then, we have the main function defined in which we have a variable assigned as “str1”. The “str1” contains the string called in function “hasPrefix”. The variable result will return the Boolean value as a message whether the string matched with the given prefix or not.

The output shows the prefix when the condition is matched with the specified string.


The article aims to familiarize you with the new feature of the Kotlin “when” expression instead of using the switch in the code. We have demonstrated the simple syntax of the when expression. Then, we provided various examples and used them in the Kotlin code snippet. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.