How to Replace Switch with When in Kotlin? | Concetto Labs

How to Replace Switch with When in Kotlin?

Author image By Manish Patel  |  Friday, November 27, 2020 04:54 PM  |  4 min read  |   777

How to Replace Switch with When in Kotlin?

As a beginner, you might look for how the Kotlin switch statement works. If your background is working in C, Java, etc. then switching to Kotlin works best. There is no switch statement available in Kotlin; instead, it replaces the switch with the when expression.

The switch is used as a two-state user interface element and work as a toggle between ON and OFF like button. One can add a switch into our application layout by using its switch object. By default, the switch is off. Moving on with Kotlin, then When is replace the Switch operator. A specific block of code needs to execute when the condition fulfilled. One can take help from Kotlin App Developers and develop android application.

In Kotlin switch case with string, when is mainly used in two ways:

  • When as a statement
  • When as an expression

When’ is an expression compared with other branches. If it used as a statement, the branch values compared sequentially with the argument and execute the corresponding branch where the condition matches. Here in this blog, we’ll look at how switch replaces the when.

Let’s start with understanding Switch in Kotlin Android and then we’ll check how the when keyword makes our code easier and more understandable.


Suppose you have used a conditional statement where the condition used in the conditional statement applies to a similar type of data. Then instead of that use vast code for the conditional statement. Use the switch to avoid using too many conditional statements in our code. Let’s study with the help of an example.

Let’s consider one example where we have to return emoji based on input spring.

input spring.
But if we rewrite the code using the When statement in Kotlin, then it provides better readability than if/else. In the above code, if/else statement replaced with the when statement.

fun getthe emoji

Replace Switch Case Kotlin with When

Replace Switch Case Kotlin with When

Similar to if/else, the switch is one of the ancient ways to deal with conditional execution. Whereas in Kotlin, we can replace switch case and if/else with when similarly, we implement the conditional execution. Now the switch can replace with the when statement also to if/else. Make the code more readable, consistent and adapt quickly with new developers.

Return values with When Block

Till now, you have seen the ability of when the block in Kotlin switch case with spring replaces conditional statements. But well, it can do more than this. Let’s say, it can return the values from the conditional blocks after you execute the necessary steps.

Return values with When Block

Arbitrary Condition with When

We can also use more than one checks to execute a block when the operator

Arbitrary Condition with When
There are several causes, but only one case that matches up will work as a respective block of code that executed. The comma added in the above example act as an OR operator.

Using When without an Argument

Using When without an Argument

We’ve used ‘when’ with an argument. But when the real potential of when is we use when without an argument. Understand how it looks.

Later, it acts as an if/else block with class-level variable conditions.

Now, Compare the instance with When

We can check whether the given operator is an instance of a particular type at runtime by using the ‘is’ operator. The operator ‘is’ works similar to the instanceof operator in Java. Now use the ‘is’ and ‘!is’ operator inside when block. Get to know how Kotlin switch case with string works with an example.

Now, Compare the instance with When

Main points about Kotlin when

Java uses Switch conditional statements that operated with primitive data types, string class, and Kotlin switch enums. instead, Kotlin allows when to be used with any user-defined types. It makes it developer-friendly.

Unlike Java, Kotlin’s when can use with dynamic value like return type of functions.
Also, check whether the given argument in collections uses the ‘in’ operator, which acts as contains.

The else is mandatory if you’re using when as an expression.

Example of a wide range of utility functions

In Kotlin, few simple utility functions are used for a simple task. Let’s say if you want to get a minimum or maximum value in the range. Similarly, the average value of the sum of range, total count or value of the range, etc.

Check below Kotlin Functions

  • count()
  • average()
  • min()
  • max()
  • sum()


In this blog, we have seen how powerful and useful the kotlin when the expression is. If you’re using Kotlin, you find yourself using the when expression all the time. Connect with us if you’re seeking to Hire Kotlin App Developers and switch to Kotlin when.

Connect with Us if you are Seeking to Hire Kotlin Developers

Get a free Call today!


Author image

Manish Patel

Manish Patel is a Co-Founder of Concetto Labs, a leading mobile app development company specialized in android and iOS app development. We provide a one-stop solution for all IT related services.

Why Our Client Love Us?

  • Fast kick-off
  • Agile Ready
  • 98% Client Retention
  • 360-Degree solutions

Our integrity and process focuses largely on providing every customer the best recommendations for their respective business. Our clients become recurring customers because we always go beyond their expectations to deliver the best solutions.

Get In Touch
Kamika Kilgore
Customer for over 2 years Kamika Kilgore United States

Concetto Labs has been a fantastic partner in consistently delivering high quality, high value solutions to our organization. Their team does an excellent job of first understanding our processes and requirements, and then translating them into solutions that meet or exceed expectations. Concetto Labs has helped us simplify multiple processes and workflows as well as suggested additional improvements we had not considered. Their communication and follow-through are top tier. They have proven themselves to be a trusted partner and we look forward to leveraging their services on future projects.

Mr. Sieva Savko
Customer for over 4 years Mr. Sieva Savko Norway

Concetto Labs have provided us with a tremendous amount of help. Their competence in various IT fields led to a positive outcome while facing different challenges. A lot of value was given by their helpful professional staff. To describe this company in three words: effectiveness, collaboration, trust. I would also like to personally thank Mr. Tejas Patel for his positive, friendly, and highly competent approach. The all-around support provided by him made every task at hand a no-brainer.

Miss. Caroline Jack
Customer for over 4 years Miss. Caroline Jack South Africa

It has been an absolute pleasure working with the Concetto Labs team! We have worked together on a few projects now, all of which have been successful. You can rely on good communication and response times from these guys. And they go above and beyond to ensure that the result is achieved. Because of their great service, they have become an integral part of our business and we will continue to use them going forward.

Mr. Richard Bartlett
Customer for over 3 years Mr. Richard Bartlett United States

Concetto Labs is a group of incredibly talented individuals. They are very responsive and communicate with you each and every step of the process. I worked with Manish Patel throughout the entire process and must say that I have never conducted business with a more honest and professional individual. Together with his team, Manish created exactly how we envisioned it to be. Thanks for all of your hard work.

Mr. Conrad Abraham
Customer for over 2 years Mr. Conrad Abraham United States

Concetto Labs team is my go-to for any simple or complex development projects. They have risen to the challenge. Great project management, communication, and super quick turnaround. I've done multiple projects with them and don't plan on changing that.

Mr. Edward Chin
Customer for over 4 years Mr. Edward Chin Canada

Concetto Labs was able to bring my complex idea to life. Throughout the project, Concetto Labs maintained constant communications and was always professional - considering I had no experience in what I wanted to create their welcoming attitude, patience, and knowledge in the field created the perfect environment to work in. I was so impressed with their work that I've kept them on board as my go-to experts. Thanks for the worthwhile experience and the breathtaking product.

Karuna Govind
Karuna Govind CTO & Co Founder (Coupay) London, UK

We started working with Concetto Labs due to the influx of work on mobile (Flutter) and frontend (React.js). It's been fantastic working with them. Good, consistent communication and good quality of work means they may be our first choice for many projects to come.

Arunabha Choudhury
Arunabha Choudhury Director (Fuzzann Technologies Private Limited) India

We are a healthcare IT company and wanted to build a Mobile Application for both Android and IOS using Flutter. The Concetto Lab team has been very patient with our project requirements and made sure all our queries are answered. It was a great show of professionalism and customer relationship. Even during the project, the team was very accommodating of all the changes we requested. The project was delivered successfully and we deployed the app in the Android Play Store. Overall, the entire process has been very transparent and the team was able to deliver exactly what we had envisioned the project outcome to look like.

Good Firms Clutch App Futura Microsoft Associate Mobile App Top Developers Gesia