Using When with Ranges and Types in Kotlin
📖 Scenario: You are building a simple program that categorizes input values based on their type and numeric range. This is useful in many apps where you want to respond differently depending on what kind of data you get and its value.
🎯 Goal: Create a Kotlin program that uses a when expression to check both the type and the numeric range of a variable, then prints a message describing the input.
📋 What You'll Learn
Create a variable called
input with a specific value.Create a variable called
ageRange representing ages from 13 to 19 inclusive.Use a
when expression to check the type of input and if it is an Int, check if it falls within ageRange.Print the correct message based on the checks.
💡 Why This Matters
🌍 Real World
Checking input types and ranges is common in apps that need to validate user data, like age verification or input validation.
💼 Career
Understanding how to use <code>when</code> with types and ranges helps you write clear and concise Kotlin code for decision making, a key skill for Android and backend Kotlin developers.
Progress0 / 4 steps