# How to stop a loop without loosing the generated data?

**URL:** <https://discourse.julialang.org/t/how-to-stop-a-loop-without-loosing-the-generated-data/90936>\
**Category:** New to Julia\
**Tags:** for-loop\
**Created:** [November 28, 2022, 6:04am UTC](https://discourse.julialang.org/t/how-to-stop-a-loop-without-loosing-the-generated-data/90936 "2022-11-28T06:04:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_ricci](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@Alex\_ricci](https://discourse.julialang.org/u/Alex_ricci)\
**Post date:** [November 28, 2022, 6:04am UTC](https://discourse.julialang.org/t/how-to-stop-a-loop-without-loosing-the-generated-data/90936/1 "2022-11-28T06:04:30Z")

</div>

I work on an iterative algorithm. The simplified version of it is

Step 1: optimize problem1  
Step 2: if objective\_function\_of\_problem1 \> zub exclude the answer and go to the step 1  
Step 3: optimize problem2 with data pertaining to the solution of p1  
Step 4: if objective\_function\_of\_problem1 + objective\_function\_of\_problem2 \< zub then update zub = function of problem2

I do this for step 1 and 2:

```julia
zub=1000
function initiate()
    for k in 1:10
        optimize!(p1)
        xᵏ=value.(x)
        o¹=objective_value(p1)
        if o¹ > zub
            #@constraint for excluding the answer ......
            break
        end
    end
    return xᵏ
end

UndefVarError: xᵏ not defined

```

It looks like after break the data is lost.  
Is it possible to stop the iteration with a condition without losing the data?

I wrote below code for step 4

```julia
zub=1000
function initiate()
    optimize!(p1)
    o¹=objective_value(p1)
    o²=solve_p2(xʳ)[1]
    if o²+o¹ < zub
        zub=o²+o¹
    end
end
UndefVarError: zub not defined

```

But it says zub is not defined. I defined it in the first line!

Thanks!

---

<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 28, 2022, 6:24am UTC](https://discourse.julialang.org/t/how-to-stop-a-loop-without-loosing-the-generated-data/90936/2 "2022-11-28T06:24:07Z")

</div>

I would have suggested to put it in a function, but it already is. Just move the `return` up to where the `break` is.

And you need `zub` to be available inside the function. Either define it inside, make it an input argument or make it a `global`.

---

<div class="post-metadata">

**Author:** ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)\
**Post date:** [November 28, 2022, 6:31am UTC](https://discourse.julialang.org/t/how-to-stop-a-loop-without-loosing-the-generated-data/90936/3 "2022-11-28T06:31:36Z")

</div>

For your first bit of code, you may use a [`local` variable](https://docs.julialang.org/en/v1/manual/variables-and-scoping/), to ensure that `xᵏ` is available throughout the whole function, instead of only inside the `for` loop.

```julia
zub=1000
function initiate()
    local xᵏ # <-- this sets xᵏ’s scope
    for k in 1:10
        optimize!(p1)
        xᵏ=value.(x)
        o¹=objective_value(p1)
        if o¹ > zub
            break
        end
    end
    return xᵏ
end

```

* * *

For your second code block, the problem is that the variable `zub` in the function

```julia
zub=1000
function initiate()
    …
    if o²+o¹ < zub
        zub=o²+o¹
    end
end

```

occurs first as the result of an assignment, so it is assumed to be a local variable. You may declare it a `global` variable to fix that:

```julia
zub=1000
function initiate()
    …
    if o²+o¹ < zub
        global zub=o²+o¹
    end
end

```

(See the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-untyped-global-variables) regarding global variables.)
