Write a reverse loop in Scala
In Scala we usually write for loops with this syntax:
// 1 2 3 4 5 6 7 8 9 10
for (i <- 1 to 10) print(i + " ")
But what if we want to do the reverse? Start at 10, and reduce until we reach 1.
Here’s how to do it:
// 10 9 8 7 6 5 4 3 2 1
for (i <- 10 to 1 by -1) print(i + " ")
The by
in our for loop tells Scala how it should deal with the iteration, for example:
for (i <- 1 to 10 by 2) print(i + " ")
Results in 1 3 5 7 9
, using by
inside your for loops you can gain a lot of flexibility!