# Use of variable in Julia REPL

**URL:** https://discourse.julialang.org/t/use-of-variable-in-julia-repl/24843
**Category:** General Usage
**Created:** [June 1, 2019, 11:32pm UTC](https://discourse.julialang.org/t/use-of-variable-in-julia-repl/24843 "2019-06-01T23:32:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Mr.Robot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mr.robot/32/8052_2.png) [@Mr.Robot](https://discourse.julialang.org/u/Mr.Robot)
#### Post date: [June 1, 2019, 11:32pm UTC](https://discourse.julialang.org/t/use-of-variable-in-julia-repl/24843/1 "2019-06-01T23:32:32Z")

</div>

## Problem

This issue has bothered me for a long time. Previously I thought it was related to my program, but the following example shows it should not.

As could be seen in the snapshot

- The REPL does have access to variable `x` in the first loop.
- Then suddenly the access is lost when I want to `x += 1` in the second loop.

I mainly use Julia for scientific computation and maybe there is something syntactic I am not aware of. But here I believe `x` is a global variable and it could be accessed when session is open.

Could someone help me, thank you in advance.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/e/de515c66016d613c719507af3215164a8851450e.png)

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [June 2, 2019, 1:06am UTC](https://discourse.julialang.org/t/use-of-variable-in-julia-repl/24843/2 "2019-06-02T01:06:39Z")

</div>

Loops have their own scope as of Julia 1.0. I’m a bit puzzled about why the first loop works, but there’s tons of discussion about this, cf [this](https://github.com/JuliaLang/julia/issues/28789), [this](https://discourse.julialang.org/t/new-scope-solution/16707), and [this](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894). I’ll bet the answer lies somewhere in there, but hopefully someone else can explain.

(Note though that (I don’t think) either of those solutions I linked were implemented)

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [June 2, 2019, 1:10am UTC](https://discourse.julialang.org/t/use-of-variable-in-julia-repl/24843/3 "2019-06-02T01:10:44Z")

</div>

Oh also, as a general rule, please copy-paste and quote your code rather than posting screenshots. In this case it’s trivial, but it just makes it easier if people can copy your code and paste it into their own repl to reproduce. [See here](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [June 2, 2019, 1:36am UTC](https://discourse.julialang.org/t/use-of-variable-in-julia-repl/24843/5 "2019-06-02T01:36:50Z")

</div>

Which version are you on? With the 1.1 Julia (

```julia
julia> versioninfo()
Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-6650U CPU @ 2.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = "C:\Users\PetrKrysl\AppData\Local\atom\app-1.37.0\atom.exe" -a
  JULIA_NUM_THREADS = 2

```

) I get the same result as you. I think the scoping rules ([New scope solution - #53 by StefanKarpinski](https://discourse.julialang.org/t/new-scope-solution/16707/53)) may be in effect.  
In the first case, the variable is read, and the globally visible `x` is printed.  
In the second case, the variable is written into (it is incremented), and therefore it is assumed to be a local variable, and since the local `x` does not exist, an error is reported.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [June 2, 2019, 1:38am UTC](https://discourse.julialang.org/t/use-of-variable-in-julia-repl/24843/6 "2019-06-02T01:38:31Z")

</div>

You may get the second loop to work as

```julia
julia> for i in 1:3
       global x += 1
       end

```
