# \[AMPL 24501\] implementing for loop

**URL:** <https://discuss.ampl.com/t/ampl-24501-implementing-for-loop/353>\
**Category:** Google Group Mirror\
**Created:** [February 1, 2023, 2:57am UTC](https://discuss.ampl.com/t/ampl-24501-implementing-for-loop/353 "2023-02-01T02:57:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emad\_Abdullah](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/emad_abdullah/32/151_2.png) [@Emad\_Abdullah](https://discuss.ampl.com/u/Emad_Abdullah)\
**Post date:** [February 1, 2023, 2:57am UTC](https://discuss.ampl.com/t/ampl-24501-implementing-for-loop/353/1 "2023-02-01T02:57:51Z")

</div>

Hi ;

how to implement for loop in AMPL ?

can we implement the equation below  
for j=1:m  
var fx {i in 0…n, j in 0…m} =0.5+0.25\*(x[i,j]\*U[i,j]+y[i,j]\*U2[i,j]+z[i,j]\*U3[i,j])-x[i,j];

end

Kind regards

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [February 1, 2023, 4:15pm UTC](https://discuss.ampl.com/t/ampl-24501-implementing-for-loop/353/2 "2023-02-01T16:15:41Z")

</div>

Hi,

Bidimensional parameters can be defined without using for loops. For example:

```auto
param fx {i in 0..n, j in 0..m} default 0.5+0.25*x[i,j];

```

You need to declare “n”, “m”, and “x” previously. You could use “:=” instead of “default” in case you are not going to modify the value of fx. You can also define more complex formulas:

```auto
param fx {i in 0..n} = if i = 0 then 0 else fx[i-1] + i;

```

Equivalent to:

```auto
param fx {i in 0..n} = sum{j in 0..i} j;

```

I would not recommend using imperative paradigm to assign values to parameters, as they are more lines of code and might be less efficient. However, this would be a way using “for” and “let” inside a script:

```auto
param fx {i in 0..n, j in 0..m};
for{i in 0..n, j in 0..m} {

let fx[i,j] := 0.5+0.25*x[i,j];
}

```

It is not clear what you wanted to do in your example (j index is used in the loop and also in the variable subscript). Hope this helps!

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [February 1, 2023, 8:07pm UTC](https://discuss.ampl.com/t/ampl-24501-implementing-for-loop/353/3 "2023-02-01T20:07:12Z")

</div>

You do not say whether x, y, z, U, U2, and U3 are variables (defined in AMPL var statements) or parameters (defined in param statements). If any of them are variables, then you are trying to define the variable fx[i,j] by an expression involving other variables. In that case, you must use an = in the var statement:

```auto
var fx {i in 0..n, j in 0..m} =
    0.5+0.25*(x[i,j]*U[i,j]+y[i,j]*U2[i,j]+z[i,j]*U3[i,j])-x[i,j];

```

If you only want to define fx for j from 1 to m, you can write `j in 1..m` in the var statement instead of `j in 0..m`. Or, as Marcos mentions, you can give a different expression for the case where j equals 0; for example:

```auto
var fx {i in 0..n, j in 0..m} =
   if j = 0 then 0 else
      0.5+0.25*(x[i,j]*U[i,j]+y[i,j]*U2[i,j]+z[i,j]*U3[i,j])-x[i,j];

```
