# What is the difference between two codes?

**URL:** <https://discourse.julialang.org/t/what-is-the-difference-between-two-codes/82287>\
**Category:** General Usage\
**Created:** [June 5, 2022, 11:37pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-two-codes/82287 "2022-06-05T23:37:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [June 5, 2022, 11:37pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-two-codes/82287/1 "2022-06-05T23:37:41Z")

</div>

Dear all,

Here are two kinds of codes, In the first one, I use `global` ,and the other one not use `global`, What is the difference between them?

```julia
julia> i = 1
1

julia> while i < 5
           println(i)
           global i += 1
       end

```

```julia
julia> i = 1;

julia> while i < 5
           println(i)
           i += 1
       end;
1
2
3
4

```

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [June 5, 2022, 11:49pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-two-codes/82287/2 "2022-06-05T23:49:10Z")

</div>

[https://docs.julialang.org/en/v1/manual/variables-and-scoping/](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [June 6, 2022, 12:06am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-two-codes/82287/3 "2022-06-06T00:06:26Z")

</div>

It seems that we need not use `global` in the function. So in my example, two kinds of codes are the same.

---

<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:** [June 6, 2022, 5:14am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-two-codes/82287/4 "2022-06-06T05:14:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [June 6, 2022, 5:53am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-two-codes/82287/5 "2022-06-06T05:53:16Z")

</div>

All of your code in the REPL is already running in the global scope, so there is no difference.

Compare it to this

```julia
julia> i = -1;

julia> begin 
         local i = 1
         while i < 5
            print(i, " ") # save some vertical space
            i += 1
         end 
       end
1 2 3 4
julia> i
-1
```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [June 6, 2022, 7:55am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-two-codes/82287/6 "2022-06-06T07:55:27Z")

</div>

Something to keep in mind: `while` is a “soft” scope. “Soft” just means its scoping rules on accessing global variables is different in interactive contexts (REPL, notebooks) versus non-interactive ones (.jl scripts, `eval`-ed expressions).  
You can check script behavior really quick this way:

```julia
@eval begin
  # paste code
end

```
