I was surprised by this behavior (in Julia)
for i in 1:10
println(i)
i=i+4
end
prints:
1
2
...
9
10
(modification of i
in the loop body is not taken into account)
In C/C++
for(int i=1;i<=10;i++) {
std::cout << "\n" << i;
i+=4;
}
you would get:
1
6
Reading Julia doc:iteration, I realized that the for loop
for i = I # or "for i in I"
# body
end
is certainly transformed into:
state = start(I)
while !done(I, state)
(i, state) = next(I, state)
# body
end
In that case we understand that i
modifications are not taken into account. Everything depends on the state variable.
Question1: am I right with this explanation?
Question2: the state variable seems to be inaccessible/hidden to the user. By consequence, construction like
for i in 1:10
println(i)
i=i+4
end
with a for loop seems impossible. Is it correct?
(I know that I can use a while i<=n
loop)