The x is not kept between iterations of the for see http://docs.julialang.org/en/release-0.4/manual/variables-and-scoping/#for-loops-and-comprehensions
On Monday, February 8, 2016 at 9:41:40 PM UTC+10, David van Leeuwen wrote: > > Hello, > > According to > http://docs.julialang.org/en/release-0.4/manual/control-flow/#man-conditional-evaluation > variables > in if blocks do not introduce lexical scope, and hence are available > afterwards. This makes sense and is what I need. > > However, it seems afterwards relates to position in the encapsulating > block, and not to execution time. > > function testif() > for i in 1:2 > if i==1 > x = 0 > end > if x==2 > println(x) > end > end > end > > > This code gives me an undefined `x` in the print statement, where I would > have expected `x` to be initialized in the first iteration. It seems I > need to define `x` outside the for loop, even though I don't need it there. > > > Is this interpretation in Julia intentional? > > Cheers, > > ---david > >
