What if you could replace messy if-else chains with a simple, readable list of conditions?
Why When without argument in Kotlin? - Purpose & Use Cases
Imagine you have many different conditions to check, like deciding what to do based on the weather. You write many if-else statements, each checking a different condition one by one.
This manual way is slow to write and hard to read. It's easy to make mistakes, like forgetting a condition or mixing up the order. It also makes your code long and confusing.
The when expression without an argument lets you check multiple conditions clearly and simply. It works like a clean list of rules, making your code easier to read and less error-prone.
if (x > 0) { println("Positive") } else if (x == 0) { println("Zero") } else { println("Negative") }
when {
x > 0 -> println("Positive")
x == 0 -> println("Zero")
else -> println("Negative")
}You can write clear, neat code that checks many different conditions without repeating yourself or getting lost in nested ifs.
For example, deciding what message to show a user based on their age, membership status, and activity level all in one place, without complicated if-else chains.
When without argument lets you check many conditions clearly.
It makes your code shorter and easier to understand.
It helps avoid mistakes common in long if-else chains.
Practice
when without argument do in Kotlin?Solution
Step 1: Understand
When used without an argument,whenwithout argumentwhenevaluates each condition as a boolean expression.Step 2: Compare with other uses
Unlikewhenwith argument, it does not match values but checks conditions one by one.Final Answer:
It checks boolean conditions one by one until one is true. -> Option BQuick Check:
whenwithout argument = boolean checks [OK]
- Thinking it matches values like switch-case
- Confusing it with loops
- Assuming it declares variables
when without argument in Kotlin?Solution
Step 1: Identify
The correct syntax omits the argument and uses boolean conditions directly inside curly braces.whenwithout argument syntaxStep 2: Check options
when { condition1 -> action1 condition2 -> action2 else -> defaultAction } matches this pattern with conditions and an else branch; others use an argument or incorrect structure.Final Answer:
when { condition1 -> action1 condition2 -> action2 else -> defaultAction } -> Option AQuick Check:
Correct syntax = when { condition1 -> action1 condition2 -> action2 else -> defaultAction } [OK]
- Adding argument inside parentheses
- Missing else branch
- Using values instead of conditions
val x = 10
when {
x < 5 -> println("Less than 5")
x in 5..10 -> println("Between 5 and 10")
else -> println("More than 10")
}Solution
Step 1: Evaluate conditions in order
First condition: x < 5 is false (10 < 5 is false). Second condition: x in 5..10 is true (10 is in 5 to 10).Step 2: Execute matching branch
Since second condition is true, it prints "Between 5 and 10" and stops checking further.Final Answer:
Between 5 and 10 -> Option CQuick Check:
10 in 5..10 = true [OK]
- Assuming x < 5 is true
- Ignoring range check
- Expecting else branch output
when without argument:val y = 3
val result = when {
y > 5 -> println("Greater than 5")
y < 5 -> println("Less than 5")
}Solution
Step 1: Check
The code has two conditions but no else branch to handle all other cases.whenbranchesStep 2: Understand Kotlin requirement
When used without argument, Kotlin requires an else branch to cover all possibilities or the compiler errors about non-exhaustive when.Final Answer:
Missing else branch for unmatched cases -> Option AQuick Check:
Always add else branch [OK]
- Ignoring else branch requirement
- Adding parentheses after when
- Thinking conditions must be constants
val temp = 25
when {
temp <= 0 -> println("Freezing")
temp in 1..15 -> println("Cold")
temp in 16..25 -> println("Warm")
temp > 25 -> println("Hot")
else -> println("Unknown")
}What will be printed and why is this
when usage correct?Solution
Step 1: Check which condition matches temp = 25
temp <= 0 is false; temp in 1..15 is false; temp in 16..25 is true (25 is included).Step 2: Confirm correctness of when usage
All possible values are covered by conditions and else branch ensures fallback; no overlap causes error.Final Answer:
"Warm"; because conditions cover all ranges with else as fallback -> Option DQuick Check:
25 in 16..25 = true, else covers all [OK]
- Misreading range boundaries
- Assuming 25 > 25 is true
- Thinking else is optional
Ready to prove it?
Pass the test and this pattern moves unknown
