📖 Scenario: You are building a simple program that checks different conditions about a number and prints messages accordingly.
🎯 Goal: Learn how to use the when expression without an argument to check multiple conditions in Kotlin.
📋 What You'll Learn
Create a variable called number with the value 15
Create a variable called isEven that checks if number is even
Use a when expression without argument to check conditions on number and isEven
Print the correct message based on the conditions
💡 Why This Matters
🌍 Real World
Using <code>when</code> without argument helps you check multiple conditions clearly and cleanly, which is useful in many programs like games, calculators, or decision-making apps.
💼 Career
Understanding <code>when</code> expressions is important for Kotlin developers to write readable and efficient code that handles different cases without complicated if-else chains.
Progress0 / 4 steps
1
Create the number variable
Create a variable called number and set it to 15.
Kotlin
Hint
Use val number = 15 to create the variable.
2
Create the isEven variable
Create a variable called isEven that is true if number is even, otherwise false. Use number % 2 == 0.
Kotlin
Hint
Use the modulus operator % to check if the number is even.
3
Use when without argument to check conditions
Write a when expression without argument that checks these conditions in order: 1. If number is less than 10, print "Number is less than 10". 2. If isEven is true, print "Number is even". 3. Otherwise, print "Number is odd and 10 or more". Use when { ... } syntax with conditions as branches.
Kotlin
Hint
Use when { condition -> action } to check each condition.
4
Print the result
Run the program to print the correct message based on the when expression.
Kotlin
Hint
The output should be Number is odd and 10 or more because 15 is odd and greater than 10.
Practice
(1/5)
1. What does when without argument do in Kotlin?
easy
A. It matches a value against constants only.
B. It checks boolean conditions one by one until one is true.
C. It creates a loop that runs multiple times.
D. It declares a variable with multiple types.
Solution
Step 1: Understand when without argument
When used without an argument, when evaluates each condition as a boolean expression.
Step 2: Compare with other uses
Unlike when with 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 B
Quick Check:
when without argument = boolean checks [OK]
Hint: Remember: no argument means boolean conditions checked [OK]
Common Mistakes:
Thinking it matches values like switch-case
Confusing it with loops
Assuming it declares variables
2. Which of the following is the correct syntax for when without argument in Kotlin?
easy
A. when {
condition1 -> action1
condition2 -> action2
else -> defaultAction
}
C. when (condition) {
true -> action1
false -> action2
}
D. when (value) {
condition1 -> action1
condition2 -> action2
}
Solution
Step 1: Identify when without argument syntax
The correct syntax omits the argument and uses boolean conditions directly inside curly braces.
Step 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 A