for this case:
scala> for (x <- 1 to 10) {
| println(x)
| }
Is the “x” a variable in this loop? why it don’t need a “var x” definition?
Thanks.
for this case:
scala> for (x <- 1 to 10) {
| println(x)
| }
Is the “x” a variable in this loop? why it don’t need a “var x” definition?
Thanks.
x
isn’t a var
; no mutation occurs here, but rather a fresh binding each time.
It desugars to: (1 to 10).foreach(x => println(x))