# Looping in expressions

**URL:** https://discourse.julialang.org/t/looping-in-expressions/111162
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [March 4, 2024, 9:52pm UTC](https://discourse.julialang.org/t/looping-in-expressions/111162 "2024-03-04T21:52:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)
#### Post date: [March 4, 2024, 9:52pm UTC](https://discourse.julialang.org/t/looping-in-expressions/111162/1 "2024-03-04T21:52:24Z")

</div>

Any preferred style of looping in expressions, objectives, or constraints that is syntactically superior and less error-prone? I see all the following works:

```julia
using JuMP

model = Model()

@variable(model, x[1:10] >= 0)
@variable(model, y[1:10] >= 0)
@variable(model, z[1:10] >= 0)

@expression(model, ex1, sum(sum(sum(x[i]+y[j]+z[k] for i in 1:3) for j in 1:2) for k in 1:3))

@expression(model, ex2, sum(x[i]+y[j]+z[k] for i in 1:3 for j in 1:2 for k in 1:3))

@expression(model, ex3, sum(x[i]+y[j]+z[k] for i in 1:3, j in 1:2, k in 1:3))

```

All of these return the same expression, so they must be equivalent. I remember not all of these worked a few years ago (I may be wrong), so I am curious if any of these can be chosen, or should I be careful while using one over the other. I would obviously prefer `ex3` because of its readability, but any comments from JuMP developers would be appreciated. Thanks!

---

<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: [March 4, 2024, 10:11pm UTC](https://discourse.julialang.org/t/looping-in-expressions/111162/2 "2024-03-04T22:11:26Z")

</div>

They are all equivalent. I’d use `ex3`.
