0
0
Kotlinprogramming~15 mins

When with multiple conditions in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Using When with Multiple Conditions in Kotlin
📖 Scenario: You are creating a simple program to categorize a day of the week into different types based on the day name.
🎯 Goal: Build a Kotlin program that uses a when expression with multiple conditions to print the type of day.
📋 What You'll Learn
Create a variable called day with the exact value "Tuesday".
Create a variable called weekendDays that holds the days "Saturday" and "Sunday".
Use a when expression with multiple conditions combined using commas to check if day is a weekend day or a weekday.
Print the exact output based on the when result.
💡 Why This Matters
🌍 Real World
Categorizing days helps apps show different content or behavior on weekends versus weekdays, like special offers or schedules.
💼 Career
Understanding how to use <code>when</code> with multiple conditions is important for writing clear and concise Kotlin code in Android apps and backend services.
Progress0 / 4 steps
1
Create the day variable
Create a variable called day and set it to the string "Tuesday".
Kotlin
Need a hint?

Use val day = "Tuesday" to create the variable.

2
Create the weekendDays list
Create a variable called weekendDays and set it to a list containing the strings "Saturday" and "Sunday".
Kotlin
Need a hint?

Use listOf("Saturday", "Sunday") to create the list.

3
Use when with multiple conditions
Write a when expression using day to check if it matches "Saturday" or "Sunday" in one branch, and "Monday", "Tuesday", "Wednesday", "Thursday", or "Friday" in another branch. Use commas to separate multiple conditions in each branch. Assign the result to a variable called dayType with the value "Weekend" or "Weekday" accordingly. Use else to assign "Unknown".
Kotlin
Need a hint?

Use commas to list multiple days in each when branch.

4
Print the dayType
Write a println statement to print the value of the variable dayType.
Kotlin
Need a hint?

Use println(dayType) to print the result.