Ranges in Loops
Iterate with to and until.
Ranges in Loops is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 4 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.
Looping with Ranges
Ranges and loops are a natural pair. A range gives you a tidy sequence of numbers to iterate over.
This lesson shows how to drive for loops, filter values, and build results using ranges.
A Basic for Loop
The classic counting loop uses a range with for.
The variable i takes each value of the range in turn. Here it prints 1 through 5 on separate lines.
for (i <- 1 to 5) println(i)Using until in Loops
When indexing things like array positions, until is often the right fit.
It stops one short of the bound, which matches zero-based indexing perfectly.
for (i <- 0 until 3) println(s"index $i")Stepping in a Loop
Combine by with a loop to skip values.
This prints the even numbers from 0 to 10 by stepping two at a time.
for (i <- 0 to 10 by 2) println(i)Counting Backward
A negative step makes a loop run in reverse.
This is the simplest way to count down, here from 3 to 1.
for (i <- 3 to 1 by -1) println(i)Accumulating a Sum
Loops over ranges are great for accumulating totals.
Here we add every number from 1 to 5 into a running total using a mutable variable.
var total = 0
for (i <- 1 to 5) total += i
println(total)Filtering with a Guard
A for loop can include a guard, which is an if condition that skips unwanted values.
Here only even numbers pass the guard and get printed.
for (i <- 1 to 10 if i % 2 == 0) println(i)Building Lists with for
Add yield to turn a loop into an expression that builds a new collection.
This produces a sequence of squares instead of printing anything.
val squares = for (i <- 1 to 5) yield i * i
println(squares.toList)Nested Loops
You can list two ranges in one for to create nested loops.
This pairs each row with each column, printing every combination from a small grid.
for (r <- 1 to 2; c <- 1 to 2) println(s"($r,$c)")Looping with foreach
Ranges also offer foreach as an alternative to for.
It runs a function for each element. Many Scala developers prefer for for readability, but both work.
(1 to 3).foreach(i => println(i))Indexing a Collection
A common pattern uses indices to loop over a collection by position.
The indices range matches the valid positions of the list, so each index is safe to use.
val names = List("Ann", "Ben", "Cy")
for (i <- names.indices) println(s"$i: ${names(i)}")Quick Check
Test what you learned about ranges in loops.
Recap
Ranges power loops: use for (i <- a to b) to iterate, by to step, a negative step to count down, and a guard to filter.
Add yield to build a new collection, nest ranges for grids, and use indices to walk a collection by position. You have completed the course.
Frequently asked questions
Is the “Ranges in Loops” lesson free?
Yes — the full text of “Ranges in Loops” 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 “Ranges in Loops”?
Iterate with to and until. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Ranges in Loops” 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