# Scope when using the if-else-end

**URL:** <https://discourse.julialang.org/t/scope-when-using-the-if-else-end/105920>\
**Category:** New to Julia\
**Created:** [November 8, 2023, 2:00am UTC](https://discourse.julialang.org/t/scope-when-using-the-if-else-end/105920 "2023-11-08T02:00:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![atBNE](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@atBNE](https://discourse.julialang.org/u/atBNE)\
**Post date:** [November 8, 2023, 2:00am UTC](https://discourse.julialang.org/t/scope-when-using-the-if-else-end/105920/1 "2023-11-08T02:00:29Z")

</div>

This program fails. Knowing that “if… does not introduce a new scope”, how to repair it?

```julia
a = 1
while a >0
  local a
  print("n, 1=next "); a = parse(Int, readline() )
  if a == 1
    u = u + 1 # this line #7 is failing...
  else
    u = a
  end # end if
  println( u,' ',2*u )
end # end while

```

When using the REPL with julia 1.9.0 , the input 4 and 5 work ok, the next input, 1, fails.  
This is the REPL dump:

```julia
julia> include("prime-other.jl")
n, 1=next 4
4 8
n, 1=next 5
5 10
n, 1=next 1
ERROR: LoadError: UndefVarError: `u` not defined
Stacktrace:
 [1] top-level scope
   @ C:\Users\worker3\Desktop\primality\try-other.jl:7
 [2] include(fname::String)
   @ Base.MainInclude .\client.jl:478
 [3] top-level scope
   @ REPL[2]:1
in expression starting at C:\Users\worker3\Desktop\primality\try-other.jl:2

```

I am a beginner. Any help? Any reference to the docs? Thanks

---

<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:** [November 8, 2023, 4:53am UTC](https://discourse.julialang.org/t/scope-when-using-the-if-else-end/105920/2 "2023-11-08T04:53:34Z")

</div>

From [the docs](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Loops-and-Comprehensions):

> In loops and comprehensions, new variables introduced in their body scopes are freshly allocated for each loop iteration, as if the loop body were surrounded by a `let` block

So each iteration’s `u` is different, and by the time you try `u = u + 1`, the right-hand `u` was not assigned. If you want a `u` that persists across all iterations, access one from the scope outside `while`. If the outer scope is global, you’ll need a `global u` declaration to access it in a script, don’t need it in a REPL or notebook.

Also, are you sure you intended the `local a`? It’s different from the outer `a=1` that’s getting checked in `while a>0`, so that while loop will run forever since you lost the ability to alter the outer `a`.

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [November 8, 2023, 3:58pm UTC](https://discourse.julialang.org/t/scope-when-using-the-if-else-end/105920/3 "2023-11-08T15:58:09Z")

</div>

It looks like you tried to mark up your code block with triple double quote characters (") … you probably meant to use triple single quote characters ('). 🙂

```julia
function looks_better()
   @show "looks better, doesn't it?"
end
```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 8, 2023, 4:15pm UTC](https://discourse.julialang.org/t/scope-when-using-the-if-else-end/105920/4 "2023-11-08T16:15:52Z")

</div>

> [@frylock](#):
>
> It looks like you tried to mark up your code block with triple double quote characters (") … you probably meant to use triple single quote characters ('). 🙂

(I edited the post to fix the quotes.)

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 8, 2023, 4:48pm UTC](https://discourse.julialang.org/t/scope-when-using-the-if-else-end/105920/5 "2023-11-08T16:48:52Z")

</div>

Looks like you accidentally described backticks (`) as single quotes (') 😃

To be clear, triple _backtick_ fences create code blocks.
