Reputation: 787
I'm having trouble understanding a problem I have with my Julia while loops. Here's a simple example:
i = 1
while i <10
bb = 2
i = i + 1
end
bb
bb not defined
while loading In[36], in expression starting on line 1
My question is how is bb not defined here? Is this a problem of scope? I don't think it is, because i is incremented to 10 at the end.
Upvotes: 0
Views: 2323
Reputation: 4366
As mentioned in the Scope of Variables section of the Julia manual, while
loops introduce unique scope blocks. So, in the given example, bb
is local to the while
loop. To make bb
available outside the loop, declare it first:
julia> i = 1; bb = 10
10
julia> while i <10
bb = 2
i = i + 1
end
julia> bb
2
Upvotes: 6