# Error in NL expression UndefVarError: exp1 not defined

**URL:** <https://discourse.julialang.org/t/error-in-nl-expression-undefvarerror-exp1-not-defined/68780>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [September 26, 2021, 3:07pm UTC](https://discourse.julialang.org/t/error-in-nl-expression-undefvarerror-exp1-not-defined/68780 "2021-09-26T15:07:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manoj.centura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj.centura/32/216443_2.png) [@manoj.centura](https://discourse.julialang.org/u/manoj.centura)\
**Post date:** [September 26, 2021, 3:07pm UTC](https://discourse.julialang.org/t/error-in-nl-expression-undefvarerror-exp1-not-defined/68780/1 "2021-09-26T15:07:05Z")

</div>

Hi All,  
is there a way to use NL expression from nested loops? outside nested loops  
example code

```julia
using JuMP
model=Model()
@variable(model,x)

xy=1
for z in 1:10
	if z==1
		if xy>=1
			@NLexpression(model,exp1,x^3)
			@NLexpression(model,exp2,x^2)
		end
	elseif z==2	
		if xy>=1
			@NLexpression(model,exp3,x^3)
			@NLexpression(model,exp4,x^2)
		end
	elseif z==3	
		if xy>=1
			@NLexpression(model,exp5,x^3)
			@NLexpression(model,exp6,x^2)
		end
	end
end	
@NLobjective(model,Min,exp1+exp2+exp3+exp4+exp5+exp6)
optimize!
error received
ERROR: UndefVarError: exp1 not defined
any idea how to resolve it. it is big code and I cant
```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 26, 2021, 9:50pm UTC](https://discourse.julialang.org/t/error-in-nl-expression-undefvarerror-exp1-not-defined/68780/2 "2021-09-26T21:50:31Z")

</div>

This is not a JuMP problem, but a regular Julia problem.

It’s usually a sign that you should structure your code differently. (e.g., What happens if `xy < 1` when `z==1`? What are exp1 and exp2 then? Why loop over `z` if you know what to do for various `z`? Why not just write explicitly?)

```nohighlight
julia> x = 2
2

julia> xy=1
1

julia> for z in 1:10
               if z==1
                       if xy>=1
                               exp1 = x^3
                               exp2 = x^2
                       end
               elseif z==2        
                       if xy>=1
                               exp3 = x^3
                               exp4 = x^2
                       end
               elseif z==3        
                       if xy>=1
                               exp5 = x^3
                               exp6 = x^2
                       end
               end
       end

julia> exp1+exp2+exp3+exp4+exp5+exp6
ERROR: UndefVarError: exp1 not defined
Stacktrace:
 [1] top-level scope
   @ REPL[22]:100

```

One solution is to give variables a default value:

```nohighlight
julia> x = 2
2

julia> xy=1
1

julia> exp1 = exp2 = exp3 = exp4 = exp5 = exp6 = 0
0

julia> for z in 1:10
           if z==1
               if xy>=1
                   exp1 = x^3
                   exp2 = x^2
               end
           elseif z==2        
               if xy>=1
                   exp3 = x^3
                   exp4 = x^2
               end
           elseif z==3        
               if xy>=1
                   exp5 = x^3
                   exp6 = x^2
               end
           end
       end

julia> exp1+exp2+exp3+exp4+exp5+exp6
36

```

---

<div class="post-metadata">

**Author:** ![manoj.centura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj.centura/32/216443_2.png) [@manoj.centura](https://discourse.julialang.org/u/manoj.centura)\
**Post date:** [October 6, 2021, 3:52am UTC](https://discourse.julialang.org/t/error-in-nl-expression-undefvarerror-exp1-not-defined/68780/3 "2021-10-06T03:52:56Z")

</div>

Thanks it worked
