I don't understand the logic behind this code

object linda {
def main(args: Array[String]): Unit = {
var lcv = 0;
var loop_start = 0;
var loop_finish = 15;

for (
lcv <- loop_start to loop_finish
if lcv % 3 ==1
) {
println (lcv)
}
}
}

Can someone please explain me how does this work?

It’s this part that I am struggling with I don’t understand the % sign and the 3==1

% is the modulo operator, so if the remainder is 1, so 7 would cause this
to hit.

HTH,
Jeff

% is the modulo operation https://en.wikipedia.org/wiki/Modulo_operation,
i.e. the remainder after division.

Can someone explain me how 3 modulo 1 =1,4,7,10 and 13 in Scala.

Any integer you can think of modulo 1 is 0 as there is no remainder from
the division by 1. If you think Scala is getting it wrong, post some code.

Someone mentioned the bad formatting on the other question. Use the </> thingy.

Avoid var. Use val instead. The compiler should be able to warn that lcv shadows. Open an issue if it doesn’t.

It should also warn for spurious var, maybe with -Xlint.

object linda {
  def main(args: Array[String]): Unit = {
    var lcv = 0
    var loop_start = 0
    var loop_finish = 15

    for (lcv <- loop_start to loop_finish if lcv % 3 == 1) println (lcv)
  }
}

-Xlint does warn about some features of the code sample.

It would be amazing if -Yexplain added text for every operator. What does % mean? And so on.

lcv % 3 == 1 means, ‘Is the remainder of dividing lcv by 3 equal to 1?’