Kotlin

Kotlin When Expression

Kotlin is a very efficient cross-platform programming language that is pretty much similar to Scala and Java. Therefore, you will not find it difficult to use the existing concepts without any modifications in all of these programming languages if you are already familiar with them. However, in this article, we will walk you through the usage of the “when” expression of the Kotlin programming language in Ubuntu 20.04.

What is the Purpose of the “When” Expression of the Kotlin Programming Language?

The “when” expression of the Kotlin programming language serves exactly the same purpose as the “switch-case” statement in Java. However, instead of using the “switch” keyword in “Kotlin”, we use the “when” keyword. Then, for specifying the different cases in Kotlin, we do not explicitly use the “case” keyword; rather, we simply state those cases in separate lines. We will learn to implement the “when” expression in Kotlin with the help of a few examples in the proceeding section of this article.

Using the When Expression in Kotlin:

For using the “when” expression in the Kotlin programming language in Ubuntu 20.04, you can take a look at the following two examples:

Example # 1: Creating a Basic Calculator by using the When Expression in Kotlin:

In this example, we will be designing a basic calculator capable of performing addition, subtraction, multiplication, and division by using the “when” expression of the Kotlin programming language. For doing so, we have designed the Kotlin script shown in the image below:

In this Kotlin script, we have defined our “main()” function within which we have defined two values named “var1” and “var2” and have assigned them to the two random values, i.e. 27 and 22 in this case. We will basically use these two numbers for performing the different calculations. Then, we have used a “println” statement to prompt the user to enter an operator of their choice according to the operation needed to be performed out of addition, subtraction, multiplication, and division.

After that, we have used the “readLine()” function to read this operator from the terminal within the “operator” variable. Then, we have created another variable, “calculator”, to which we have assigned the result of the “when (operator)” expression. Within this expression, we have defined all the different cases, i.e. “+”, “-”, “*”, and “/” for performing addition, subtraction, multiplication, and division, respectively. Then, we have a fifth case too for defining the scenario in which we will cater for the invalid operators entered by the user. For this case, we have used the “else” keyword. Finally, outside the body of the “when” expression, we have used a “println” statement for printing the result of our calculation on the terminal.

We have compiled this Kotlin script into a class file by using the following command:

$ kotlinc when.kt

Then, we have executed this class file with the command given below:

$ kotlin WhenKt

When we ran this Kotlin program, we were first asked to enter an operator, as shown in the following image:

Just for the sake of testing, we entered the “+” operator as shown in the image below:

The result of our calculation is shown in the following image:

Similarly, the rest of the operations can also be performed smoothly simply by entering the relevant operator at the runtime. However, just for testing the “else” case, we entered an invalid operator this time after re-executing this script, as shown in the image below:

The result of our Kotlin script with an invalid operator is shown in the following image. It implies that our “else” case also works perfectly well.

Example # 2: Finding out the Day of a Week using the When Expression in Kotlin:

In this example, Kotlin script, we just want to find out the day of the week using the Kotlin “when” expression. For doing that, we have designed the Kotlin script shown in the image below:

In this Kotlin script, we have defined a “main()” function in which we have printed a message using the “println” statement for prompting the user to enter a day of their choice. Then, we have used the “readLine()” function for reading the day entered by the user within the “day” variable.

After that, we have used the “when” expression over the “day” variable. Within the body of this expression, we have defined all the different cases for printing the position of the day of the week entered by the user. For example, “Monday” is the first day of the week, “Tuesday” is the second day of the week, and so on. Again, we have used the “else” case for catering for the invalid day entered by the user.

When we executed this Kotlin script, we were asked to enter any day of the week, as shown in the following image:

We have entered “Friday” to test this script, as shown in the image below:

The output of this Kotlin script with our provided input is shown in the following image:

Finally, for testing the “else” case of our script and also for showing you that Kotlin is a case-sensitive language, we have again entered “Friday” as the input; however, this time, we have not capitalized its first alphabet as shown in the image below:

This time, the “else” case was executed because none of our provided cases within the script started with a small alphabet, as shown in the following image. If Kotlin had not been case-sensitive, the fifth case would have been executed instead of the “else” case.

Conclusion:

This article was designed to teach you the usage of the “when” expression of the Kotlin programming language. We began by stating the close resemblance of this expression with the “switch-case” statement in Java. Afterwards, we shared two extensive examples that used the “when” expression in Kotlin. Once you go through these examples, you will instantly learn the usage of this Kotlin expression.

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.