# The while cycle doesn't work

**URL:** https://discourse.julialang.org/t/the-while-cycle-doesnt-work/134542
**Category:** New to Julia
**Tags:** scope
**Created:** [December 14, 2025, 3:55am UTC](https://discourse.julialang.org/t/the-while-cycle-doesnt-work/134542 "2025-12-14T03:55:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Kobold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobold/32/219958_2.png) [@Kobold](https://discourse.julialang.org/u/Kobold)
#### Post date: [December 14, 2025, 3:55am UTC](https://discourse.julialang.org/t/the-while-cycle-doesnt-work/134542/1 "2025-12-14T03:55:27Z")

</div>

I wrote a simple program, literally to test how the while cycle works, but it gives the same error, even though I wrote it identically to the one in the training material.  
x=0  
while x\<10  
x+=1  
println(x)  
end

and the error:  
r Warning: Assignment to ‘x’ in soft scope is ambiguous because a global variable by the same name exists: ‘x’ will be treated as a new local. Disamb  
iguate by using ‘local x’ to suopress this warning ar “global x° to assign to the existing global variable.  
@

ERROR: Unuefvartrror: “x” not detined in local scope  
Suggestion: check for an assignment to a local variable that shadows a global of the same name.

---

<div class="post-metadata">

### Author: ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)
#### Post date: [December 14, 2025, 4:11am UTC](https://discourse.julialang.org/t/the-while-cycle-doesnt-work/134542/2 "2025-12-14T04:11:53Z")

</div>

Missing an `end`

```julia-auto
julia> while x<100
           x+=1
           if x%2==0
               println(x)
           end
       end
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

```

Consider using a comprehension as an alternative

```julia-auto
[println(x) for x in 1:100 if x % 2 == 0]

```

---

<div class="post-metadata">

### Author: ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)
#### Post date: [December 14, 2025, 5:53am UTC](https://discourse.julialang.org/t/the-while-cycle-doesnt-work/134542/3 "2025-12-14T05:53:43Z")

</div>

This is an issue of scope, and I recommend reading more about it here: [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#local-scope)

In short, you’ve created `x` as a global variable and then created a new “soft” scope within the `while` loop. The while loop doesn’t “know” whether you intend to reference the global variable or whether you’ve made a mistake and haven’t declared a local value for `x`.

(Julia’s soft scopes differ in their behaviour depending on whether they are executed in the REPL or from a file (e.g. `julia myscript.jl`). In my opinion, this difference probably wasn’t a great idea but I think the motivation was to make the REPL easier to work with.)

In any case, the solution here is to tell Julia that you want to use the global variable `x` inside the `while` loop, which you can do using the `global` keyword:

```julia-auto
x = 0
while x < 10
  global x
  x += 1
  println(x)
end

```

Having said this, it’s usually not a good idea to mutate global state since this can lead to spaghetti code. So an easier way to do this is make `x` local to the scope, either by putting the code in a `let` block or in a function:

```julia-auto
let
  x = 0
  while x < 10
    x += 1
    println(x)
  end
end

```

Or:

```julia-auto
function print_to_ten()
  x = 0
  while x < 10
    x += 1
    println(x)
  end
end

print_to_ten()

```

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [December 16, 2025, 8:29pm UTC](https://discourse.julialang.org/t/the-while-cycle-doesnt-work/134542/4 "2025-12-16T20:29:35Z")

</div>

There is a tip called [A Common Confusion](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#A-Common-Confusion-2479cb3548c466db) at the top of the Scope section of the manual going over this exact case.

You don’t need to read all of the scoping rules to get a working knowledge of the scope behavior.

1. Global scope (top level) is special. You generally want to pass global variables as function arguments to make them accessible to inner scopes.
2. Variables defined in a local scope will be available to inner scopes and not available to outer scopes. If you write all your code inside functions, then you will get better performance and more consistent scoping behavior.
