By accident I used the same counter twice in a nested for loop:
for i in 1:2
println(i)
for j in 1:2
for i in 5:6
i += 1
println(i, " ", j)
end
end
end
Which compiled without issue, and produced
1
6 1
7 1
6 2
7 2
2
6 1
7 1
6 2
7 2
It took me by surprise once I realized what was going on. Is this behavior intended? Since this leads to unexpected behavior, is there any way to make the compiler flag this as an error?
Just as an example, Fortran throws an error when compiling similar code:
program testfor
integer :: i, j, k
do i = 1,2
do j = 1,2
do i = 5,6
i = i+1
write(*,*) i, j
end do
end do
end do
end program testfor
Save as ftest.f90, then compiling with $:gfortran ftest.f90 -o ftest shows two errors:
ftest.f90:7:18:
5 | do i = 1,2
| 2
6 | do j = 1,2
7 | do i = 5,6
| 1
Error: Variable 'i' at (1) cannot be redefined inside loop beginning at (2)
ftest.f90:8:19:
7 | do i = 5,6
| 2
8 | i = i+1
| 1
Error: Variable 'i' at (1) cannot be redefined inside loop beginning at (2)