What if you could replace many confusing checks with one simple, clear expression?
Why When with multiple conditions in Kotlin? - Purpose & Use Cases
Imagine you have a list of fruits and you want to print a special message for each type. You try to check each fruit one by one using many if-else statements.
Using many if-else statements makes your code long, hard to read, and easy to make mistakes. It's like checking every fruit separately instead of grouping similar ones together.
The when expression lets you check multiple conditions in a clean and organized way. You can group several cases together, making your code shorter and easier to understand.
if (fruit == "apple" || fruit == "pear") { println("This is a sweet fruit") } else if (fruit == "lemon" || fruit == "lime") { println("This is a sour fruit") }
when (fruit) {
"apple", "pear" -> println("This is a sweet fruit")
"lemon", "lime" -> println("This is a sour fruit")
}You can write clear and concise code that handles many conditions together, making your programs easier to read and maintain.
Think about a traffic light system where you want to perform the same action for both red and yellow lights (stop), and a different action for green (go). Using when with multiple conditions groups these actions neatly.
Manual checks with many if-else are long and confusing.
when groups multiple conditions for cleaner code.
This makes your programs easier to read and less error-prone.
Practice
What does the when expression do when multiple conditions are separated by commas in Kotlin?
Solution
Step 1: Understand how
In Kotlin, when multiple conditions are separated by commas, it means thewhenhandles multiple conditionswhenexpression checks if the value matches any one of those conditions.Step 2: Interpret the behavior of multiple conditions
If the value matches any condition in the list, the corresponding block runs. It does not require all conditions to be true simultaneously.Final Answer:
It checks if the value matches any of the listed conditions and executes the corresponding block. -> Option BQuick Check:
Multiple conditions = any match triggers block [OK]
- Thinking all conditions must be true at once
- Believing only the first condition is checked
- Assuming multiple conditions cause errors
Which of the following is the correct syntax to check if a variable num is either 1, 2, or 3 using when with multiple conditions?
when (num) {
? -> println("Number is 1, 2, or 3")
else -> println("Other number")
}Solution
Step 1: Recall Kotlin
In Kotlin, multiple conditions in awhensyntax for multiple conditionswhencase are separated by commas without operators.Step 2: Match the correct syntax
1, 2, 3 uses commas to separate values:1, 2, 3, which is correct. Other options use logical operators or list syntax, which are invalid here.Final Answer:
1, 2, 3 -> Option AQuick Check:
Multiple conditions use commas [OK]
- Using || or && inside
whenconditions - Trying to use list syntax like [1, 2, 3]
- Missing commas between values
What will be the output of the following Kotlin code?
val x = 5
when (x) {
1, 2, 3 -> println("Low")
4, 5, 6 -> println("Medium")
else -> println("High")
}Solution
Step 1: Identify the value of
The variablexxis assigned the value 5.Step 2: Match
The second case checks ifxwithwhenconditionsxis 4, 5, or 6. Sincexis 5, this condition matches and prints "Medium".Final Answer:
Medium -> Option AQuick Check:
5 in 4,5,6 prints Medium [OK]
- Choosing the first matching group incorrectly
- Confusing else with matching values
- Assuming no output if multiple conditions exist
Find the error in this Kotlin when expression:
val grade = 'B'
when (grade) {
'A', 'B' -> println("Excellent")
'C' || 'D' -> println("Good")
else -> println("Try again")
}Solution
Step 1: Review the
In Kotlin, multiple conditions in awhencondition syntaxwhencase must be separated by commas, not logical operators like '||'.Step 2: Identify the incorrect usage
The line'C' || 'D'uses '||', which is invalid syntax inwhen. It should be'C', 'D'.Final Answer:
Using '||' insidewhenconditions is invalid syntax. -> Option DQuick Check:
Use commas, not logical operators [OK]
when conditions [OK]- Using logical operators instead of commas
- Thinking commas are wrong separators
- Confusing variable types with syntax errors
You want to write a Kotlin when expression that prints "Weekend" if the day is either "Saturday" or "Sunday", "Weekday" if the day is any other day of the week, and "Invalid" for any other input. Which code correctly implements this?
Solution
Step 1: Check correct multiple condition syntax for weekend days
when(day) { "Saturday", "Sunday" -> println("Weekend") "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday") else -> println("Invalid") } correctly uses commas to separate "Saturday" and "Sunday" to match either day.Step 2: Verify handling of other days and else case
when(day) { "Saturday", "Sunday" -> println("Weekend") "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday") else -> println("Invalid") } lists all weekdays explicitly and useselsefor invalid inputs, matching the problem requirements.Final Answer:
when(day) { "Saturday", "Sunday" -> println("Weekend") "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday") else -> println("Invalid") } -> Option CQuick Check:
Use commas for multiple matches and else for fallback [OK]
- Using || or && instead of commas
- Missing else for invalid inputs
- Not listing all weekdays explicitly
Ready to prove it?
Pass the test and this pattern moves unknown
