The when expression helps you choose what to do based on different values. Using multiple conditions lets you check several options together in one place.
When with multiple conditions in Kotlin
when (variable) { value1, value2, value3 -> { // code for these values } value4 -> { // code for value4 } else -> { // code if no match } }
You separate multiple conditions with commas.
The else block runs if no other condition matches.
color is either "red" or "blue" and prints "Primary color" for both.val color = "red" when (color) { "red", "blue" -> println("Primary color") "green" -> println("Secondary color") else -> println("Unknown color") }
val day = 6 when (day) { 6, 7 -> println("Weekend") in 1..5 -> println("Weekday") else -> println("Invalid day") }
This program checks the fruit name and prints a message based on groups of fruits.
fun main() { val fruit = "apple" when (fruit) { "apple", "banana", "orange" -> println("This is a common fruit.") "kiwi", "mango" -> println("This is a tropical fruit.") else -> println("Unknown fruit") } }
You can combine as many values as you want separated by commas.
Using when with multiple conditions keeps your code shorter and easier to read.
when lets you check many values in one place.
Multiple conditions are separated by commas.
Use else to handle any unmatched cases.
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
