Skip to content

Conversation

@WalterMadelim
Copy link

function main()
           c = 0
           while c <= 2
               c += 1
               if c <= 1
                   local d = c
               end
               println("In the $c-th iteration:")
               println("d = $d")
           end
       end

julia> main()
In the 1-th iteration:
d = 1
In the 2-th iteration:
ERROR: UndefVarError: `d` not defined in local scope

@LilithHafner LilithHafner changed the title Update faq.md Say that loops have a distinct scope for each itteration in faq.md Sep 19, 2025
@LilithHafner
Copy link
Member

This is a great thing to clarify, thanks! Technically, they are all in the same scope, just different executions of that scope. This is because scope is a syntactic construct.

The key detail to document is that the scope of a loop body is entered at the start of each iteration and then exited at the end of it, clearing any local variables between iterations.

Given this clarification, it might also make sense to document this in another place (e.g. perhaps https://docs.julialang.org/en/v1.11/manual/variables-and-scoping/), but I'm curious what you think about that.

@WalterMadelim
Copy link
Author

This is because scope is a syntactic construct

To be honest, I think most users do not need to understand what is a scope, as it is a human-defined construct. They only need to get familiar with a set of exemplary cases, so that they know whether they produce expected results finally.

clearing any local variables between iterations

I think it's the user lose control of the local variable in the previous iteration, at the same time having access to a new local variable which having the unaltered (user specified) name but is indeed a new local.

document this in another place (e.g. perhaps [link]

That documentation is already somewhat lengthy, even telling some historical issues. (I don't know if others would like to keep them). To put it in a nutshell I think code might be more powerful than texts. Inventing some typical cases helps. e.g. the one me-made https://discourse.julialang.org/t/is-it-defined-behavior-to-modify-iter-while-for-looping-over-it/132477/30?u=waltermadelim

😊

@nsajko nsajko added the docs This change adds or pertains to documentation label Sep 19, 2025

Here, `x` is a global variable, `while` defines a [local scope](@ref scope-of-variables), and `x += 1` is
an assignment to a global in that local scope.
Here, `x` is a global variable, `while` defines a distinct [local scope](@ref scope-of-variables) for each of its iteration, and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing white space:

Suggested change
Here, `x` is a global variable, `while` defines a distinct [local scope](@ref scope-of-variables) for each of its iteration, and
Here, `x` is a global variable, `while` defines a distinct [local scope](@ref scope-of-variables) for each of its iteration, and

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each of its iteration

Seems like bad grammar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs This change adds or pertains to documentation status: waiting for PR author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants