Increment for-loop by 2 in Scala
Try for (a <- 0 until max by 2)
Note the difference between to
and until
. With a strict i < max
you will want until.
val max = 10
scala> for(i <- 0 until max by 2)
| println(i)
0
2
4
6
8
scala> for(i <- 0 to max by 2)
| println(i)
0
2
4
6
8
10