Using When Expression as a Powerful Switch in Kotlin
📖 Scenario: You are building a simple program that tells the type of a day based on its name. This is like a calendar helper that categorizes days into weekdays or weekends.
🎯 Goal: Build a Kotlin program that uses the when expression to check the name of a day and print whether it is a weekday or weekend.
📋 What You'll Learn
Create a variable with the exact name
day and assign it the string value "Monday".Create a variable with the exact name
weekendDays that holds a list of weekend day names: "Saturday" and "Sunday".Use a
when expression with the variable day to check if it is a weekend or a weekday.Print the exact output
"Monday is a Weekday" or similar for the given day.💡 Why This Matters
🌍 Real World
This kind of day categorization is useful in calendar apps, scheduling software, or any program that needs to treat weekdays and weekends differently.
💼 Career
Understanding <code>when</code> expressions helps you write clear and concise decision-making code, a common task in software development.
Progress0 / 4 steps