What is the syntax of a switch statement in Kotlin?
Oct 24, 2025
Leave a message
In the realm of programming, Kotlin has emerged as a powerful and versatile language favored by developers worldwide. One of its fundamental control flow constructs is the switch statement, known as the when expression in Kotlin. This blog post will delve deep into the syntax of the when expression in Kotlin and also touch upon our role as a switch supplier in the industrial context.
Understanding the Basics of the when Expression in Kotlin
The when expression in Kotlin serves as a more flexible and powerful alternative to the traditional switch statement found in languages like Java. It allows for more complex conditional checks and can be used in different contexts, including as an expression that returns a value.


Simple Value Comparison
The most basic use of the when expression is to compare a value against multiple cases. Here's a simple example:
fun main() {
val number = 2
when (number) {
1 -> println("The number is 1")
2 -> println("The number is 2")
3 -> println("The number is 3")
else -> println("The number is not 1, 2, or 3")
}
}
In this example, the when expression takes the number variable and checks it against different cases. If the value of number matches one of the cases, the corresponding block of code is executed. The else case is optional but is used to handle any values that don't match the specified cases.
Multiple Conditions in a Single Case
You can also group multiple conditions in a single case using commas. Here's an example:
fun main() {
val day = "Saturday"
when (day) {
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("It's a weekday")
"Saturday", "Sunday" -> println("It's a weekend")
else -> println("Invalid day")
}
}
In this code, the cases for weekdays and weekends are grouped together, making the code more concise.
Range and Type Checks
The when expression in Kotlin also supports range and type checks. Here's an example that demonstrates both:
fun main() {
val value: Any = 25
when (value) {
in 1..10 -> println("The value is between 1 and 10")
in 11..20 -> println("The value is between 11 and 20")
is Int -> println("The value is an integer")
else -> println("The value is something else")
}
}
In this example, the when expression first checks if the value is within a certain range. If not, it checks the type of the value. This flexibility makes the when expression a powerful tool in Kotlin.
Our Role as a Switch Supplier
As a switch supplier, we understand the importance of quality and reliability in the industrial sector. We offer a wide range of switches to meet the diverse needs of our customers. Some of our popular products include the Z-15GQ-B Q8 Q21 Q22 Basic Switch, the 514120 Actuator, and the D4A-3101N General-purpose Limit Switch.
These switches are designed to provide accurate and reliable performance in various industrial applications. Whether you need a basic switch for a simple control circuit or a more advanced actuator for a complex automation system, we have the right product for you.
Using when in Industrial Control Logic
In industrial control systems, programming languages like Kotlin can be used to implement control logic. The when expression can be particularly useful in these scenarios. For example, consider a system that monitors the status of a machine. The status can be one of several values, such as "Running", "Stopped", "Error", etc. The following Kotlin code demonstrates how the when expression can be used to handle different statuses:
fun main() {
val machineStatus = "Running"
when (machineStatus) {
"Running" -> {
// Perform actions for a running machine
println("The machine is running. Monitoring performance...")
}
"Stopped" -> {
// Perform actions for a stopped machine
println("The machine is stopped. Checking for restart conditions...")
}
"Error" -> {
// Perform actions for an error state
println("The machine has encountered an error. Initiating diagnostic procedures...")
}
else -> {
println("Unknown machine status")
}
}
}
This code uses the when expression to handle different machine statuses and perform appropriate actions based on the status.
Advanced Usage of when as an Expression
In Kotlin, the when expression can also be used as an expression that returns a value. This is useful when you need to assign a value based on a condition. Here's an example:
fun main() {
val number = 3
val result = when (number) {
1 -> "One"
2 -> "Two"
3 -> "Three"
else -> "Other"
}
println(result)
}
In this code, the when expression is used to assign a string value to the result variable based on the value of number.
Conclusion
The when expression in Kotlin is a powerful and flexible control flow construct that offers many advantages over the traditional switch statement. Its ability to handle multiple conditions, perform range and type checks, and be used as an expression makes it a valuable tool for developers.
As a switch supplier, we are committed to providing high-quality products and excellent customer service. If you are in the market for industrial switches, we encourage you to explore our product range and contact us for procurement and further discussions. Our team of experts is ready to assist you in finding the right switches for your specific needs.
References
- Kotlin Programming Language Documentation
- Industrial Electronics Handbook
Send Inquiry





