cYn cYn - 1 month ago 5
Scala Question

Increment for-loop by 2 in Scala

How do I increment the loop by 2 as equivalent to this in Java:

for (int i = 0; i < max; i+=2)


Right now in Scala I have:

for (a <- 0 to max)


For a fact
max
will always be even. I don't know how to increment the counter to 2 in each loop.

Answer

Try for (a <- 0 until max by 2)