0
0
Kotlinprogramming~15 mins

Why expressions over statements matters in Kotlin - See It in Action

Choose your learning style9 modes available
Why expressions over statements matters
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use println to show the value of clothing.