Julia While 循环未按预期工作
Julia While Loop not Working as Expected
我无法理解我的 Julia while 循环遇到的问题。这是一个简单的例子:
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
我的问题是这里怎么没有定义bb?这是范围问题吗?我不认为是,因为最后 i 增加到 10。
如 Scope of Variables section of the Julia manual, while
loops introduce unique scope blocks. So, in the given example, bb
is local 到 while
循环中所述。要使 bb
在循环外可用,首先声明它:
julia> i = 1; bb = 10
10
julia> while i <10
bb = 2
i = i + 1
end
julia> bb
2
我无法理解我的 Julia while 循环遇到的问题。这是一个简单的例子:
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
我的问题是这里怎么没有定义bb?这是范围问题吗?我不认为是,因为最后 i 增加到 10。
如 Scope of Variables section of the Julia manual, while
loops introduce unique scope blocks. So, in the given example, bb
is local 到 while
循环中所述。要使 bb
在循环外可用,首先声明它:
julia> i = 1; bb = 10
10
julia> while i <10
bb = 2
i = i + 1
end
julia> bb
2