Kotlin when Expression

If you are familiar with Java, then no course in Java is possible without flow of controls in which the switch forms a very important lesson. A typical switch looks like below:

       case 1:System.out.println(" MONDAY ");        break;
       case 2:System.out.println(" TUESDAY ");       break;
       case 3:System.out.println(" WEDNESDAY ");     break;
       case 4:System.out.println(" THRUSDAY ");      break;
       case 5:System.out.println(" FRIDAY ");        break;
       case 6:System.out.println(" WEEKEND ");       break;
       case 7:System.out.println(" WEEKEND ");       break;
       default:System.out.println(" NOT POSSIBLE "); break;

If you are familiar with Java, then the above example would have been the best example and the easiest one.

Kotlin when Expression

Now, when it comes to improvements there are different ways we can improvise an existing functionality. In Kotlin, when does exactly the same. The most prominent problem with this switch case which every developer has faced once in  lifetime is the use of break. If the break command is not used the compiler executes all the cases occurring after that case till a break is found. Also, there is a repetition of the syntax since we have to rewrite the case command again and again.

So improving the above features and keeping developers ease of coding, Kotlin came up with the expression based when {}. Yes, you got it correct, when even has the capability to return an expression or not which makes it one of the most unique features.Well thats not all, the when {} is also capable to work with condition expressions and to end it can be used even without arguments.

Now, lets see how the above switch would look like when ported to Kotlin’s when.

var weekDay = when(weekNumber){
	1 -> "MONDAY"
	2 -> "TUESDAY"
	4 -> "THURSDAY"
	5 -> "FRIDAY"
	6,7 -> "WEEKEND"	
	else -> "NOT POSSIBLE"

Now here in this case we saw that the execution result of the when case are even stored in a variable which can be used further like below :

println("Week Day is : \"$weekDay\"")

This all because of Kotlin’s safe approach to programming. This type of execution would reduce chances of error and making our program compile bug free.

Now, lets see an example when we try to pass range in the expression. Yes, Kotlin does allows to test the scenario for the multiple ranges. Let’s have a look at the code below :

fun main(args : Array<String>){

    var percentage = 25

    when(percentage) {
        in 0..40 -> println("Reappear in exam ! ")
        in 41..60 -> println("Pass....")
        in 61..75 -> println("First Class....")
        in 76..90 -> println("Distinction ::))) ....")
        else -> println("You are too close to perfect 0's")


Reappear in exam !

So, here the learning we draw is when expression even executes if we pass range of numbers in which our case could occur. Simplifying the task of repeatedly writing the same code again and again.

Till here we have used encountered that our code is strictly limited to one line of code in the cases but whereas in case of switch we can have multiple lines of code in each case which were then separated by “break”. So if you thought this was a limitation of when, you are wrong here yet again. Because when expression does allows each case have blocks of code, which can be simply identified by braces ‘{}’. Lets see the below example to have a better understanding.

fun main(args : Array<String>){

    var percentage = 50
    when(percentage) {
        in 0..40 -> println("Reappear in exam ! ")
        in 41..100 -> {	
			println("Pass....with percentage :$percentage")


Pass….with percentage :50

Thats all the interesting part of when – modern day switch is all about. It reduces the redundancy of switch cases and makes us write swift and simple yet effective coding.

Well that is the reason Kotlin was introduced! So thanks for reading this tutorial on Kotlin…Hope you enjoyed it. Stay tuned for more learning and interesting lectures.

Leave a Comment

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