Skip to Content
Course content

93: IntRange, CharRange, and Progressions

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've noticed a recurring pattern when developers move from languages like Python or JavaScript to Kotlin: they assume that writing 1..100 actually creates a list containing one hundred integers in memory. It feels intuitive, right? You see a range, you think "collection."

But that's a costly misconception. Let's look at why that's wrong:

val hugeRange = 1..1_000_000
// You might think this just allocated 4MB of RAM for a million Ints.
// In reality, it allocated one single object that just stores two numbers: 1 and 1,000,000.

If ranges were actually lists, your memory usage would spike every time you wrote a simple for loop. Instead, Kotlin uses Progressions. A range is just a blueprint; it knows where it starts and where it ends, and it calculates the "next" value on the fly only when you actually ask for it.

Ranges are boundaries, not pre-filled lists

Because an IntRange or a CharRange is just a set of boundaries, they are incredibly efficient for checks. I almost always prefer the in operator over writing clunky comparison logic.

Take a scenario where you're validating a user's age for a specific movie rating. Instead of writing if (age >= 13 && age <= 17), you can just do this:

val age = 15
if (age in 13..17) {
    println("You're in the teen bracket.")
}

This works for characters too. Since characters are essentially numeric values under the hood, CharRange allows you to check for categories without a massive when block. If I want to see if a character is an uppercase letter, I don't need a regex; I just check the range:

val char = 'G'
if (char in 'A'..'Z') {
    println("It's an uppercase letter!")
}

Controlling the flow with step and downTo

The standard .. operator is great, but it only moves forward by one. In the real world, you often need more control over how you traverse a range. This is where step and downTo come in.

If you try to use 10..1, Kotlin won't throw an error, but the loop simply won't execute. The .. operator expects the start to be less than or equal to the end. To count backwards, you must use downTo.

Here is a practical example. Imagine you're building a simple countdown for a rocket launch, but you only want to announce every second second to avoid cluttering the logs:

for (i in 10 downTo 1 step 2) {
    println("T-minus $i seconds...")
}
// Output:
// T-minus 10 seconds...
// T-minus 8 seconds...
// T-minus 6 seconds...
// T-minus 4 seconds...
// T-minus 2 seconds...

I like to think of step as a modifier for the progression's "stride." You can apply it to both increasing ranges and downTo progressions. Just keep in mind that the step value must be positive; if you try to use a negative step, the compiler will stop you.




📋 Practical Task

Exercise: The Security System Access Validator

You are building a security module for a high-tech vault. The vault has three security tiers based on a numeric "Clearance Level" (1 to 100) and a specific "Access Code Character" (A to Z).

Write a program that does the following:

  • Define a variable clearanceLevel and a variable accessCode.
  • Use a range check to determine the tier:
    • 1..30: "Low Clearance"
    • 31..70: "Mid Clearance"
    • 71..100: "High Clearance"
    • Anything else: "Invalid Clearance"
  • Check if the accessCode is a valid uppercase letter using a CharRange. If it's not, print "Invalid Access Code".
  • If the user has "High Clearance" and a valid access code, simulate a "Vault Unlocking" sequence by printing a countdown from 5 down to 1, but only printing every odd number (5, 3, 1) using a progression.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.