Building Ranges
Generate number sequences.
Building Ranges is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Range?
A range represents an ordered sequence of evenly spaced integers, like 1, 2, 3, 4, 5.
Ranges are memory efficient because they store only the start, end, and step, not every number. They are ideal for counting and looping.
The to Keyword
The simplest range uses to, which includes both endpoints.
So 1 to 5 covers 1, 2, 3, 4, and 5. The final number is part of the range.
val r = 1 to 5
println(r)Inclusive Ranges
A to range is called inclusive because it contains its upper bound.
To see the actual numbers rather than the compact description, you can convert the range to a list.
val r = 1 to 5
println(r.toList)The until Keyword
Use until when you want to stop just before the upper bound.
So 1 until 5 gives 1, 2, 3, 4 and leaves out 5. This is exclusive of the end value.
val r = 1 until 5
println(r.toList)to versus until
The difference is just the last number. to includes the end, until excludes it.
1 to 5 has five elements, while 1 until 5 has four. Choosing the right one avoids off-by-one bugs.
println((1 to 5).toList)
println((1 until 5).toList)Stepping with by
By default a range steps by one. Add by to change the step size.
Here we count from 0 to 10 in jumps of 2, producing the even numbers.
val evens = 0 to 10 by 2
println(evens.toList)Counting Down
A negative step lets a range count backward.
With by -1, the range starts high and decreases. This is the standard way to iterate in reverse.
val down = 5 to 1 by -1
println(down.toList)Range Size
You can ask a range how many elements it contains with size.
This is computed instantly from the bounds and step, without listing every number.
val r = 0 to 20 by 5
println(r.size)Checking Membership
The contains method tells you whether a value falls inside a range.
It respects the step too, so 3 is not contained in a step-2 range of even numbers.
val evens = 0 to 10 by 2
println(evens.contains(4))
println(evens.contains(3))Ranges Are Collections
A range behaves like any other sequence, so you can map or sum over it.
Here we square each number and add the results, all without building an explicit list first.
val total = (1 to 4).map(n => n * n).sum
println(total)Empty Ranges
If the start is already past the end, the range is simply empty.
For example 5 until 5 contains nothing. Empty ranges are valid and just produce no elements when looped.
val r = 5 until 5
println(r.isEmpty)Quick Check
Test what you learned about building ranges.
Recap
You build ranges with to (inclusive) and until (exclusive), adjust the step with by, and count down with a negative step.
Ranges are lightweight collections that support size, contains, and map. Next you will use them to drive loops.
Frequently asked questions
Is the “Building Ranges” lesson free?
Yes — the full text of “Building Ranges” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “Building Ranges”?
Generate number sequences. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building Ranges” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating Tuples
- Destructuring Tuples
- Building Ranges
- Ranges in Loops