Good afternoon Programming Community, I am glad to welcome you to another Kotlin edition. we will look upon the replacement of switch with the when keyword. Firstly, we will look on to some examples of switch and after that, we will look at how the when keyword makes our task or code easier and more understandable. So, let’s get started.
Standard Conditional
if (number == 1) { println("value: 1") } else if (nummber == 2) { println("value: 2") } else if (number == 3) { println("value: 3") } else if (number == 4) { println("value 4") } else { println("value greater 5") }
A traditional switch is basically just a statement that can substitute a series of simple if/else that make basic checks as displayed above. However it cannot replace all sort of if/else sequences but just those which compare a value with some constant. So, you can only use a switch to perform an action when one specific variable has a certain precise value.
To remove this difficulty, switch-case was introduced, where we pass the variable to be compared with-in the switch statement (in our example, that variable is number) and compare it with various case statements present corresponding to it and do the operation.
‘switch’
switch (number) { case 1: println("value: 1") break; case 2: println("value: 2") break; case 3: println("value: 3") break; case 4: println("value: 4") break; default: println("value greater 5") break; }
So, in the above code in order to print the numbers in word, you have to use various conditional statements and this results in a large number of lines of code. Think of a situation when you have to print the words representation of numbers up to 100 or 1000. If you are using conditional statements then you have to use 1000 conditional statements.
In the above code, number is passed in switch statement and cases are used to compare that number. For example, if the number is 1 then case 1 will be executed, if number is 2 then case 2 will be executed and so on. But if the number is not equal to any of the case present then the default block will be executed.
‘when’
when { number == 1 -> { println("value: 1") } nummber == 2 -> { println("value: 2") } number == 3 -> { println("value: 3") } number == 4 -> { println("value 4") } else -> { println("value greater 5") } }
So, if you are moving from Java to Kotlin, then you will find some changes in the syntax part also. In Java we use switch but in Kotlin, that switch gets converted to when. when is also used for conditional representation but it does the things in a very smarter and easier way. Whenever you are having a number of possibilities then you can use when in your code.
In the above code, like in switch, the number is passed in when and then we compare that number with various options available. In our case, if the number == 1, then “one” will be printed and so on. If more than one match is found then the match that occurs first will be considered only. If no match to the number is found then the else part will be executed.
When in doubt use ‘when’
- no complex case/break groups, only the condition followed by ->
- it can group two or more equivalent choices, separating them with a comma
Wrapping Things Up
we learned how to use when in place of switch in Kotlin. We saw that, if we are having a number of possibilities for a particular variable then we can make use of when to handle all the possibilities. Also, we can use when for multiple or more than one choices.