What is the syntax of a switch statement in Kotlin?

Jun 09, 2026

Leave a message

Mia Robinson
Mia Robinson
Mia is a logistics coordinator at the company. She ensures the smooth transportation of products both at home and abroad, which is crucial for the company's product sales and customer satisfaction.

Hey there! As a switch supplier, I often get asked about the syntax of a switch statement in Kotlin. So, I thought I'd take a few minutes to break it down for you.

Two-pedal Auto Switch high qualityBoat Rocker Switch high quality

First off, if you're not familiar with Kotlin, it's a modern programming language that runs on the Java Virtual Machine (JVM). It's been gaining a lot of popularity in recent years, especially for Android development, because it's concise, safe, and interoperable with Java.

Now, let's talk about the switch statement. In many programming languages, including Java, there's a switch statement that allows you to select one of many code blocks to be executed based on the value of an expression. In Kotlin, the equivalent is the when expression.

The basic syntax of a when expression in Kotlin looks like this:

when (expression) {
    value1 -> {
        // code to execute when expression == value1
    }
    value2 -> {
        // code to execute when expression == value2
    }
    else -> {
        // code to execute when expression doesn't match any of the above values
    }
}

Let's break this down a bit. The when keyword starts the expression. Inside the parentheses, you have an expression which can be any valid Kotlin expression. This is the value that the when expression will evaluate against.

Each case in the when expression is defined by a value followed by an arrow (->). When the expression matches the value, the code block after the arrow will be executed.

The else branch is optional, but it's useful when you want to handle cases where the expression doesn't match any of the specified values. It's like a catch - all for everything else.

Here's a simple example:

fun main() {
    val number = 2
    when (number) {
        1 -> println("The number is 1")
        2 -> println("The number is 2")
        else -> println("The number is neither 1 nor 2")
    }
}

In this example, since number is 2, the output will be "The number is 2".

One of the really cool things about the when expression in Kotlin is that it can also be used without an argument. In this case, it acts more like a series of if - else statements.

fun main() {
    val x = 10
    val y = 20
    when {
        x > y -> println("x is greater than y")
        x < y -> println("x is less than y")
        else -> println("x is equal to y")
    }
}

Here, the when expression checks different conditions without evaluating a single expression.

Now, let's talk about how this relates to my business as a switch supplier. We offer a wide range of switches for different applications. For example, we have the Boat Rocker Switch, which is designed specifically for boats. These switches are built to withstand the harsh marine environment, with features like water - resistance and corrosion - resistance.

We also have the Automotive General Power Switch. This type of switch is commonly used in cars to control various electrical components. It's reliable and durable, ensuring smooth operation in automotive applications.

And then there's the Two - pedal Auto Switch. This switch is designed for specific automotive functions, providing precise control and long - lasting performance.

Just like the when expression in Kotlin, different switches are used based on different conditions or requirements. You wouldn't use a boat switch in a car, and vice versa. Each switch is tailored to a particular application, just like each case in a when expression is designed for a specific value or condition.

If you're in the market for high - quality switches for your project, whether it's for a boat, a car, or any other application, I'd love to talk to you. We have a team of experts who can help you choose the right switch for your needs. Don't hesitate to reach out for a purchase consultation. We're here to make sure you get the best switch solutions for your requirements.

References:

  • Kotlin Programming Language Documentation
  • Kotlin in Action by Dmitry Jemerov and Svetlana Isakova
Send Inquiry