var i = 0
while true:
var j: int
inc i
if i == 2: j = i
echo j
if i == 5: break
$ ./t 0 2 0 0 0
I had not really expected that. Is that what modern languages do now? So one has to be careful.
These semantics are very common. This behavior is present in C++ too, which is quite an old language by now; and I'm sure some languages older than C++ behave similarly.
j is declared in the scope defined by the loop, which is instantiated for every iteration, and in this case it's being implicitly zero-initialized. I dunno if you expected different results because of the absence of explicit initialization of the variable.
Yes, I was not aware that the scope of the loop is NEWLY instantiated for every iteration.
And for a string?
while true:
var s: string
Is s set to nil for each iteration, or only set to lenght of zero? May a string not defined in the loop body have advantages for performance?