Kotlin when Statement

Kotlin when Statement – Complete Beginner Guide With Examples
Control flow is one of the most important concepts in programming. When you need to make decisions based on conditions, Kotlin provides a powerful and flexible tool called the when statement.
The when statement in Kotlin is more advanced and expressive than the traditional switch statement found in other programming languages.
In this SEO-optimized, beginner-friendly guide, you will learn:
What the
whenstatement isWhy it is better than
if-elseandswitchBasic syntax
Using
whenas a statementUsing
whenas an expressionMultiple conditions in one branch
Range conditions
Type checking with
whenUsing
whenwithout an argumentReal-world examples
Common beginner mistakes
Let’s get started
What Is the Kotlin when Statement?
The when statement is used to execute different blocks of code based on different conditions.
It works similarly to:
switchin Javaif-elsechains
But it is more powerful and flexible.
Basic Syntax of when
Simple Example of when
Output:
Here, Kotlin checks the value of day and matches it with each case.
Why Use when Instead of Multiple if-else?
You could write:
But when:
Looks cleaner
Improves readability
Reduces nested conditions
Handles multiple cases efficiently
Using when as an Expression
One of the biggest advantages of Kotlin’s when statement is that it can return a value.
Example:
Here, when directly returns a value.
Multiple Conditions in a Single Branch
You can combine multiple values.
This reduces repetition.
Using Ranges in when
Kotlin allows range conditions.
This is extremely useful for grading systems, scoring, and validations.
Using when Without an Argument
You can also use when without passing a value.
Example:
This works like an improved if-else ladder.
Type Checking with when
You can check types using is.
Example:
This feature is very powerful in real-world Kotlin applications.
Smart Casting in when
When you check a type using is, Kotlin automatically casts it.
Example:
No manual casting required.
Using when with Enums
when works perfectly with enum classes.
Example:
No else needed if all cases are covered.
Using when with Sealed Classes
Sealed classes are commonly used with when.
Example:
This ensures compile-time safety.
Real-World Example – Login Validation
This example shows practical usage.
Common Beginner Mistakes
Forgetting else Branch
When using when as an expression, else is required unless all cases are covered.
Confusing when with if
Use when when checking multiple conditions.
Not Using Ranges Properly
Use in keyword for range checks.
Writing Too Many Conditions
Keep logic simple and readable.
when vs if-else
| Feature | when | if-else |
|---|---|---|
| Readability | High | Moderate |
| Multiple conditions | Easy | Complex |
| Range checking | Built-in | Manual |
| Type checking | Built-in | Limited |
| Expression support | Yes | Yes |
For multiple branching, when is preferred.
When Should You Use when?
Use when when:
Checking multiple values
Handling enums
Handling sealed classes
Validating ranges
Performing type checks
Avoid it when simple if is enough.
Frequently Asked Questions (FAQs)
1. What is the when statement in Kotlin?
The when statement is a control flow structure used to execute code based on multiple conditions.
2. Is when better than switch?
Yes, Kotlin’s when is more powerful and flexible than the traditional switch statement.
3. Can when return a value?
Yes, when can be used as an expression and return a value.
4. Do we always need an else branch?
An else branch is required when not all possible cases are covered.
5. Can when check types?
Yes, it supports type checking using the is keyword.
Conclusion
The Kotlin when statement is a powerful decision-making tool.
You learned:
Basic syntax
Using
whenas expressionRange conditions
Type checking
Smart casting
Enums and sealed classes
Real-world examples
Mastering when will make your Kotlin programs cleaner and more readable.
