# Usage of loops for defining new variables

**URL:** <https://discourse.julialang.org/t/usage-of-loops-for-defining-new-variables/14056>\
**Category:** General Usage\
**Tags:** question, loops\
**Created:** [August 25, 2018, 8:21pm UTC](https://discourse.julialang.org/t/usage-of-loops-for-defining-new-variables/14056 "2018-08-25T20:21:30Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Boris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boris/32/3306_2.png) [@Boris](https://discourse.julialang.org/u/Boris)\
**Post date:** [August 26, 2018, 10:52am UTC](https://discourse.julialang.org/t/usage-of-loops-for-defining-new-variables/14056/6 "2018-08-26T10:52:52Z")

</div>

I read through the documentation and the other topics, i.e.

> [@REPL and for loops (scope behavior change)](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/8):
>
> It is not a bug. It worked in Julia 0.6, in Julia 0.7 it gave a deprecation warning that you must add global, and in 1.0 it throws an error. I really like favba’s idea of starting by introducing functions and teaching them to write tiny functions. Sounds like that’d also end up encouraging good coding habits.

[https://stackoverflow.com/questions/51930537/scope-of-variables-in-julia/](https://stackoverflow.com/questions/51930537/scope-of-variables-in-julia/)

and the link provided by @rdeits.

Yes, I also did feel it is counter intuitive, simply because I have never thought a for/while loop the same as a function, i.e .to have its own variable space. In other words, I simply did not expect  
x(1) = 1  
x (2) = 2  
and  
for i = 1:2  
x[i] = i;  
end  
to have different behaviour, I simply saw the for loop as an easier way not to have to write the same thing many times. Technically this is the way a function works, so it makes sense once I think about it, it was a point I have never thought about.

However, neither the documentation, nor the links provide an answer to my question regarding speed and global variables - is declaring a variable as global in a loop “bad” in terms of leading to performance loss? I mean in terms of the typical arguments against global variables (in relation to variable type and machine code optimization), e.g. [here](https://lectures.quantecon.org/jl/need_for_speed.html) or [here](http://wiki.c2.com/?GlobalVariablesAreBad). It is noted in the manual, that changing the values of global variables is not recommended but then, isn’t changing the value of a variable iteratively the main purpose of loops?

If it does, than this speaks for avoiding loops or am I missing something? I do a lot of Markov Chain Monte Carlo in my work where loops are essential. Every step of my program depends typically on the previous iteration and within a loop I change most of the variables of interest, i.e. they all have to be global.

---

_[View the full topic](https://discourse.julialang.org/t/usage-of-loops-for-defining-new-variables/14056)._
