📖 Scenario: Imagine you are writing a small Kotlin program to decide what to wear based on the weather. You want to use expressions instead of statements to make your code cleaner and easier to understand.
🎯 Goal: Build a Kotlin program that uses expressions instead of statements to decide and print what to wear based on the temperature.
📋 What You'll Learn
Create a variable with a temperature value
Create a variable to hold the clothing suggestion using an expression
Use an if expression to decide the clothing suggestion
Print the clothing suggestion
💡 Why This Matters
🌍 Real World
Deciding what to wear based on weather is a common real-life task. Using expressions helps write clear and concise code for such decisions.
💼 Career
Understanding expressions over statements is important for writing clean Kotlin code, which is valuable for Android development and other Kotlin-based projects.
Progress0 / 4 steps
1
DATA SETUP: Create a temperature variable
Create a variable called temperature and set it to 15.
Kotlin
Hint
Use val to create a variable and assign the number 15 to temperature.
2
CONFIGURATION: Prepare to decide clothing
Create a variable called clothing and set it to an empty string "".
Kotlin
Hint
Use var to create a variable clothing and assign it an empty string.
3
CORE LOGIC: Use an if expression to assign clothing
Use an if expression to assign clothing the value "Jacket" if temperature is less than 20, otherwise "T-shirt". Use the exact code clothing = if (temperature < 20) "Jacket" else "T-shirt".
Kotlin
Hint
Use the if expression to assign clothing in one line.
4
OUTPUT: Print the clothing suggestion
Write println(clothing) to display the clothing suggestion.
Kotlin
Hint
Use println to show the value of clothing.
Practice
(1/5)
1. Why are expressions preferred over statements in Kotlin programming?
easy
A. Because expressions return values and can be used anywhere a value is needed
B. Because statements are faster to execute
C. Because statements always cause errors
D. Because expressions do not require variables
Solution
Step 1: Understand the difference between expressions and statements
Expressions produce values and can be used wherever a value is expected, while statements perform actions but do not return values.
Step 2: Identify why expressions are preferred
Using expressions allows writing clearer and more concise code by directly returning values without extra variables or statements.
Final Answer:
Because expressions return values and can be used anywhere a value is needed -> Option A
Quick Check:
Expressions return values = C [OK]
Hint: Remember: expressions produce values, statements do not [OK]
Common Mistakes:
Confusing speed with value return
Thinking statements always cause errors
Believing expressions don't need variables
2. Which of the following is the correct Kotlin syntax for using an expression in an if statement?
easy
A. if (x > 0) { return "Positive" } else { return "Non-positive" }
B. if x > 0 then "Positive" else "Non-positive"
C. val result = if (x > 0) "Positive" else "Non-positive"
D. val result = if x > 0 { "Positive" } else { "Non-positive" }
Solution
Step 1: Review Kotlin's expression syntax for if
Kotlin allows if to be used as an expression that returns a value, using parentheses and else branch.
Step 2: Identify the correct syntax
val result = if (x > 0) "Positive" else "Non-positive" correctly assigns the result of the if expression to a variable. Other options have syntax errors or incorrect usage.
Final Answer:
val result = if (x > 0) "Positive" else "Non-positive" -> Option C
Quick Check:
Correct if expression syntax = B [OK]
Hint: Use parentheses and else for if expressions [OK]
Common Mistakes:
Omitting parentheses around condition
Using incorrect keywords like 'then'
Trying to assign without else branch
3. What will be the output of this Kotlin code?
val max = if (10 > 5) 10 else 5
println(max)
medium
A. 5
B. 10
C. true
D. Compilation error
Solution
Step 1: Evaluate the if expression condition
The condition 10 > 5 is true, so the expression returns 10.
Step 2: Assign and print the value
The variable max is assigned 10, and println(max) outputs 10.
Final Answer:
10 -> Option B
Quick Check:
Condition true returns 10 = A [OK]
Hint: Check condition result to find expression value [OK]
Common Mistakes:
Confusing output with false branch
Expecting boolean output
Thinking code causes error
4. Identify the error in this Kotlin code snippet:
val result
if (x > 0) {
result = "Positive"
} else {
result = "Non-positive"
}
println(result)
medium
A. No error, code runs fine
B. Missing parentheses in if condition
C. Cannot assign string to 'result'
D. Variable 'result' must be initialized before use
Solution
Step 1: Check variable declaration and initialization
The variable result is declared without initialization, which is not allowed for val in Kotlin.
Step 2: Understand Kotlin's val initialization rules
val must be initialized at declaration or in constructor; here it is uninitialized and assigned later, causing error.
Final Answer:
Variable 'result' must be initialized before use -> Option D
Quick Check:
Uninitialized val causes error = D [OK]
Hint: val needs immediate initialization or constructor assignment [OK]
Common Mistakes:
Thinking val can be assigned later
Ignoring initialization errors
Assuming if syntax is wrong
5. How can you rewrite this Kotlin code using expressions to make it shorter and clearer?
var status: String
if (score >= 60) {
status = "Pass"
} else {
status = "Fail"
}
println(status)
hard
A. val status = if (score >= 60) "Pass" else "Fail"
println(status)
B. if (score >= 60) println("Pass") else println("Fail")
C. var status = "Pass" if (score >= 60) else "Fail"
println(status)
D. val status
status = if score >= 60 then "Pass" else "Fail"
println(status)
Solution
Step 1: Identify the current code structure
The code uses a statement-based if to assign status variable based on score.
Step 2: Use if as an expression for assignment
Kotlin allows if to return a value, so we can assign status directly with the expression, making code shorter and clearer.
Final Answer:
val status = if (score >= 60) "Pass" else "Fail"
println(status) -> Option A
Quick Check:
Use if expression for assignment = A [OK]
Hint: Assign if result directly to variable [OK]
Common Mistakes:
Using invalid syntax for if expression
Trying to assign without else branch
Using statements instead of expressions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.