# Issue with loop

**URL:** <https://discourse.julialang.org/t/issue-with-loop/20768>\
**Category:** General Usage\
**Created:** [February 13, 2019, 10:08pm UTC](https://discourse.julialang.org/t/issue-with-loop/20768 "2019-02-13T22:08:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erisme](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@Erisme](https://discourse.julialang.org/u/Erisme)\
**Post date:** [February 13, 2019, 10:08pm UTC](https://discourse.julialang.org/t/issue-with-loop/20768/1 "2019-02-13T22:08:13Z")

</div>

Hi,

I need some help please, I have an error that i don’t understand how to fix it. Something that i can’t see.

julia\> a=0  
0

julia\> (a+=1 for i in 1:3)  
Base.Generator{UnitRange{Int64},getfield(Main, Symbol(“##3#4”))}(getfield(Main, Symbol(“##3#4”))(), 1:3)

julia\> a  
0

julia\> for i=1:3  
a = a+1  
end  
ERROR: UndefVarError: a not defined  
Stacktrace:  
[1] top-level scope at .\REPL[4]:2 [inlined]  
[2] top-level scope at .\none:0

Can someone help me to understand what is wrong?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 13, 2019, 10:30pm UTC](https://discourse.julialang.org/t/issue-with-loop/20768/2 "2019-02-13T22:30:01Z")

</div>

In order to modify a global in a loop you must write `global a`.

---

<div class="post-metadata">

**Author:** ![Erisme](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@Erisme](https://discourse.julialang.org/u/Erisme)\
**Post date:** [February 14, 2019, 12:24am UTC](https://discourse.julialang.org/t/issue-with-loop/20768/3 "2019-02-14T00:24:51Z")

</div>

Thank you in this case how can we write the loop in one line:

(global a+=1 for i in 1:3) does not work. any idea?

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [February 14, 2019, 12:58am UTC](https://discourse.julialang.org/t/issue-with-loop/20768/4 "2019-02-14T00:58:04Z")

</div>

The problem is that `(global a+=1 for i in 1:3)` creates a “generator” which goes through the loop lazily, ie at a later point when you call `collect` or iterate through it. You can just do `[global a+=1 for i in 1:3]` to run the loop immediately.

---

<div class="post-metadata">

**Author:** ![Erisme](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@Erisme](https://discourse.julialang.org/u/Erisme)\
**Post date:** [February 14, 2019, 2:55pm UTC](https://discourse.julialang.org/t/issue-with-loop/20768/5 "2019-02-14T14:55:49Z")

</div>

Yes but this one is createing an array.

I found this way:

a=0  
for i=1:3  
eval(: (a+=1))  
end

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [February 14, 2019, 3:27pm UTC](https://discourse.julialang.org/t/issue-with-loop/20768/6 "2019-02-14T15:27:48Z")

</div>

use `foreach`

---

<div class="post-metadata">

**Author:** ![jkbest2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkbest2/32/7350_2.png) [@jkbest2](https://discourse.julialang.org/u/jkbest2)\
**Post date:** [February 14, 2019, 3:49pm UTC](https://discourse.julialang.org/t/issue-with-loop/20768/7 "2019-02-14T15:49:37Z")

</div>

I don’t think there’s any reason to use `eval` here. If you do what @StefanKarpinski suggested above:

```julia
a = 0
for i = 1:3
  global a += 1
end

```

you get what (I think) you’re looking for.

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [February 14, 2019, 3:54pm UTC](https://discourse.julialang.org/t/issue-with-loop/20768/8 "2019-02-14T15:54:37Z")

</div>

Also note, if you don’t like using `global` you can just put this all in a function and it works:

```julia
function suma()
   a = 0
   for i = 1:3 
       a += 1
   end
   return a
end

```

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [February 14, 2019, 6:16pm UTC](https://discourse.julialang.org/t/issue-with-loop/20768/9 "2019-02-14T18:16:15Z")

</div>

Along the lines @isaacsas suggestion, here is my temporary suggestion:

- Use Jupyter for most interactive use for now
- When you are in the REPL or in a julia script, never use loops or control structures relying on scope rules outside of a function. This will not be a major inconvenience in the short-term.

If you do this, the scoping behavior will be exactly the sort of thing you have in mind (i.e., you won’t need to think about global scope issues, never need to type `global` under normal circumstances, and can copy/paste code between functions and jupyter).

Finally, have faith that some variation of [Another possible solution to the global scope debacle](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894) will be implemented when it is ready and tested, then you can start using loops again outside of functions/jupyter, and you probably won’t need to think about this stuff again.
