I wrote a simple program, literally to test how the while cycle works, but it gives the same error, even though I wrote it identically to the one in the training material.
x=0
while x<10
x+=1
println(x)
end
and the error:
r Warning: Assignment to ‘x’ in soft scope is ambiguous because a global variable by the same name exists: ‘x’ will be treated as a new local. Disamb
iguate by using ‘local x’ to suopress this warning ar “global x° to assign to the existing global variable.
@
ERROR: Unuefvartrror: “x” not detined in local scope
Suggestion: check for an assignment to a local variable that shadows a global of the same name.
In short, you’ve created x as a global variable and then created a new “soft” scope within the while loop. The while loop doesn’t “know” whether you intend to reference the global variable or whether you’ve made a mistake and haven’t declared a local value for x.
(Julia’s soft scopes differ in their behaviour depending on whether they are executed in the REPL or from a file (e.g. julia myscript.jl). In my opinion, this difference probably wasn’t a great idea but I think the motivation was to make the REPL easier to work with.)
In any case, the solution here is to tell Julia that you want to use the global variable x inside the while loop, which you can do using the global keyword:
x = 0
while x < 10
global x
x += 1
println(x)
end
Having said this, it’s usually not a good idea to mutate global state since this can lead to spaghetti code. So an easier way to do this is make x local to the scope, either by putting the code in a let block or in a function:
let
x = 0
while x < 10
x += 1
println(x)
end
end
Or:
function print_to_ten()
x = 0
while x < 10
x += 1
println(x)
end
end
print_to_ten()