Eric.O
March 5, 2022, 5:33pm
#1
I want to print my output in Julia, I have written it in the form presented below but I want to use a for loop to make it more concise.
Any help on how to write the code below using for loop will be appreciated please.

@show objective_value(model)
@show value(w[1])
@show value(w[2])
@show value(w[3])
@show value(w[4])
@show value(w[5])
@show value(x[1])
@show value(x[2])
@show value(x[3])
@show value(x[4])
@show value(x[5])
@show value(X)
@show value(y)

odow
March 5, 2022, 6:44pm
#2
Please make a new post, rather than commenting on an old one.

Just write a for loop over the indices?

```
for I in 1:5
@show value(x[I])
end
```

1 Like

Eric.O
March 5, 2022, 8:35pm
#3
I tried this but am not getting it rightly.
This is the output I get when I run the code

value(w[I]) = 0.0
value(w[I]) = 0.0
value(w[I]) = 0.0
value(w[I]) = 0.0
value(w[I]) = 0.0

I want it to display as w1, w2, w3 …w5.

Your help will be greatly appreciated please.

Eric.O
March 5, 2022, 8:40pm
#4
I also want to write the codes below using a for loop.

Kindly help me out.

Rewrite using a for loop
@constraint (model, X^2 + y^2 - d[1]-2X-2y <= -2)

@constraint (model, X^2 + y^2 - d[2]-2X-8y <= -17)

@constraint (model, X^2 + y^2 - d[3]-8X-8y <= -32)

```
@show objective_value(model)
for i in 1:5
@show value(w[i])
end
for i in 1:5
@show value(x[i])
end
@show value(X)
@show value(y)
```

odow
March 7, 2022, 2:18am
#7
```
for i in 1:5
println("x[$i] = ", value(x[i]))
end
```

1 Like

Eric.O
March 8, 2022, 12:20am
#8
Thank you very much.
This works now and my problem is solved.
why did you add $ in the bracket please?

To interpolate the value of `i`

into the string.

1 Like

Eric.O
March 8, 2022, 4:48pm
#10
Thank you for your help.
Everything works perfectly now.

1 Like