# How to avoid using global variable in while scope?

**URL:** <https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500>\
**Category:** New to Julia\
**Created:** [February 12, 2023, 10:00am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500 "2023-02-12T10:00:14Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![romantimo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romantimo/32/46841_2.png) [@romantimo](https://discourse.julialang.org/u/romantimo)\
**Post date:** [February 12, 2023, 10:00am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/1 "2023-02-12T10:00:14Z")

</div>

In this simple example, if I need a counter for a while loop, how to avoid using global variable? Can I initialize something inside while scope?

```julia
i = 0
while i < 10
  global i
  i += 1
  k = i
end

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 12, 2023, 10:08am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/2 "2023-02-12T10:08:43Z")

</div>

Most of the time, `for` loops are more appropriate than `while` loops:

```julia
for i = 0:9
    k = i
end

```

However, if you must use `while`, you can create a local scope with the [`let` block](https://docs.julialang.org/en/v1/base/base/#let) and nest the loop within it:

```julia
let i = 0
    while i < 10
        k = i
        i += 1
    end
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 12, 2023, 10:14am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/3 "2023-02-12T10:14:07Z")

</div>

Note that the global is only needed if you are running the loop in the global scope of an included script, which probably is a bad idea.

See: [Scope of loops · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/v1.0/loopscopes/)

---

<div class="post-metadata">

**Author:** ![romantimo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romantimo/32/46841_2.png) [@romantimo](https://discourse.julialang.org/u/romantimo)\
**Post date:** [February 12, 2023, 10:26am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/4 "2023-02-12T10:26:52Z")

</div>

yes… but this is like creating a new scope inside global to use it for the separate section of a code. Not really smth very good i my case. But thx)

---

<div class="post-metadata">

**Author:** ![romantimo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romantimo/32/46841_2.png) [@romantimo](https://discourse.julialang.org/u/romantimo)\
**Post date:** [February 12, 2023, 10:28am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/5 "2023-02-12T10:28:52Z")

</div>

Actually you’re right this is important. I probably need to mention, that I’m running this loop inside module.

```julia
module mod
  i = 0
  while i < 10
    global i
    i += 1
    k = i
  end
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 12, 2023, 10:31am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/6 "2023-02-12T10:31:22Z")

</div>

The general practice is to put everything into functions. Then you execute the function.

---

<div class="post-metadata">

**Author:** ![romantimo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romantimo/32/46841_2.png) [@romantimo](https://discourse.julialang.org/u/romantimo)\
**Post date:** [February 13, 2023, 8:46am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/7 "2023-02-13T08:46:57Z")

</div>

How that would be as a function for a while loop? Can you write a short example pls?  
And how this `global i` will be avoided?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [February 13, 2023, 8:49am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/8 "2023-02-13T08:49:25Z")

</div>

For example:

```julia
function calc_k()
    i = 0
    while i < 10
        i += 1
        k = i
   end

   return k
end

```

---

<div class="post-metadata">

**Author:** ![romantimo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romantimo/32/46841_2.png) [@romantimo](https://discourse.julialang.org/u/romantimo)\
**Post date:** [February 13, 2023, 9:11am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/9 "2023-02-13T09:11:56Z")

</div>

could you please tell why `while` inside function can work without `global i` and `while` inside module cannot?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [February 13, 2023, 9:30am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/10 "2023-02-13T09:30:24Z")

</div>

This is the section of the docs explaining the relevant scoping rules:  
[https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope)  
In particular, see rule 3 in the list, titled “Soft scope”, and the example beginning with the text

> Next, let’s consider the body of `sum_to_def` extracted into global scope

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 13, 2023, 11:48am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/11 "2023-02-13T11:48:39Z")

</div>

> [@romantimo](#):
>
> could you please tell why `while` inside function can work without `global i` and `while` inside module cannot?

The first thing is that in a loop where a variable is assingned, like this:

```julia
i = 1
for j i n 1:3
    i = 2
end
println(i)

```

there is a possible ambiguity associated with `i` referring to the `i` outside the loop, or to the a new `i` created by the scope of the loop iteration.

In Julia, this ambiguity can have important performance implications depending on whether `i` is [a non-constant global variable](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-untyped-global-variables) or a variable in a local scope, where its type can be inferred upon compilation.

If the whole block is inside a function (or a `let` block, for example), the type of `i` can be inferred (in general) then `i` is local to the scope of the function, can be type-stable and everything will be optimized accordingly. Thus, there are no issues associated with that pattern.

If, on the other side, the code is in a global scope, the performance will be very different depending on the possibility of type-inferring `i`. Julia therefore wants to discourage people from writing code in global scope, particularly code that is aimed to be performant. Thus, if the user wants to write a loop in global scope, he/she has to be explicit aware that of that ambiguity and possible implications, and it was decided that requiring an explicit global annotation was a good choice.

Later, in v1.5, that requirement was removed from code typed directly into the REPL, because otherwise one could not copy-paste code from the body of functions to the REPL directly, and that was annoying. This having minor importance since nobody writes performant critical code outside functions in the REPL directly.

I think that is more or less the story of it. It is as tradeoff between usability and promoting good practices of Julia programming.

---

<div class="post-metadata">

**Author:** ![romantimo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romantimo/32/46841_2.png) [@romantimo](https://discourse.julialang.org/u/romantimo)\
**Post date:** [February 16, 2023, 9:36am UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-while-scope/94500/12 "2023-02-16T09:36:33Z")

</div>

thank you @lmiq for this thoughtful explanation
