# Strange behaviour with if-else-Statements

**URL:** https://discourse.julialang.org/t/strange-behaviour-with-if-else-statements/47266
**Category:** Optimization (Mathematical)
**Tags:** jump, bug
**Created:** [September 25, 2020, 2:40pm UTC](https://discourse.julialang.org/t/strange-behaviour-with-if-else-statements/47266 "2020-09-25T14:40:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![SonntagK](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@SonntagK](https://discourse.julialang.org/u/SonntagK)
#### Post date: [September 25, 2020, 2:40pm UTC](https://discourse.julialang.org/t/strange-behaviour-with-if-else-statements/47266/1 "2020-09-25T14:40:08Z")

</div>

Hello,

I’m new to Julia and working with Julia/JuMP for optimization purposes. I want to use if-else-statements in the constraints I’m defining to write them more compact. When trying so I ran in some strange behaviour.

Im using `Julia v1.5.1` and `JuMP v0.21.3.`

1.) Just `Julia`: Writing the following into a .jl and running with include()

```julia
if 1 == 2
    1
end
+ 5

```

returns the correct value `5` .

But writing the following into a .jl and running with include() gives an error.

```julia
if 1 == 2 1 end + 5

```

The Error is: `ERROR: LoadError: MethodError: no method matching +(::Nothing, ::Int64)`

2.) Using `Julia` and `JuMP`: When defining the following function ` Test()` the resulting constraint `con` in the .lp file is not like you would expect it

```julia
using JuMP

m = Model()

function Test(m::Model,set_constr::Int64)

    @variable(m, x[1:5]>=0)

    @constraint(m,con, 
                if set_constr == 1  
                    x[1]
                else 
                    if set_constr == 2
                        0.1*x[2]
                    end
                    +
                    x[4]
                    +
                    if set_constr == 3
                        x[5]
                    else 0 end
                end
                <= 0)

    JuMP.write_to_file(m, "model.lp")
    empty!(m)   
end 

```

For ` Test(m,1)` we get: `con: 1 x_1_ <= 0` like we want to have.

For ` Test(m,2)` we get: `con: <= 0` while we would like to get `con: 0.1 x_2_ + x_4_ <= 0`

For ` Test(m,3)` we get `con: 1 x_5_ <= 0` while we would like to get `con: x_4_ + x_5_ <= 0 `

Is my understanding of the if-else-statement false here or is this a bug?

Thanks for your help and kind regards

Konstantin

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [September 25, 2020, 2:57pm UTC](https://discourse.julialang.org/t/strange-behaviour-with-if-else-statements/47266/2 "2020-09-25T14:57:42Z")

</div>

This is not a bug. Starting a line with + doesn’t connect it with the previous line.

```julia
julia> 5
5

julia> +5
5

julia> 5 + 5
10

```

This is what’s happening

```julia
julia> if true
           10
       end + 5
15

julia> if false
           10
       end + 5
ERROR: MethodError: no method matching +(::Nothing, ::Int64)

```

You’re hitting the else case (i.e. your condition is false) which returns nothing. Nothing + Int is an error. The reason putting +5 on a new line works is because it’s parsed as

```julia
nothing # this line does nothing
+5 # +5 === 5, coincidentally this is what you wanted, but not for the right reason

```

You can use ternary expressions for compact if statements

```julia
x = 1 == 2 ? 15 : 10

```

Or you can use the boolean to do something directly if that makes sense in context

```julia
x = 10 + 5 * (1 == 2)

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [September 25, 2020, 3:06pm UTC](https://discourse.julialang.org/t/strange-behaviour-with-if-else-statements/47266/3 "2020-09-25T15:06:38Z")

</div>

By the way, if for some reason you really want to write it that way, you can always just stick in a semi-colon to act as a newline:

```julia
julia> if false
           10
       end; + 5
5

```

---

<div class="post-metadata">

### Author: ![GerhardTotschnig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerhardtotschnig/32/215516_2.png) [@GerhardTotschnig](https://discourse.julialang.org/u/GerhardTotschnig)
#### Post date: [September 25, 2020, 3:40pm UTC](https://discourse.julialang.org/t/strange-behaviour-with-if-else-statements/47266/4 "2020-09-25T15:40:12Z")

</div>

It seems, as tomerarnon mentioned, a + in the beginning of a line in Julia does not represent a continuation of the line before. See:

> [@Best practice for avoiding line break bugs in long expressions](https://discourse.julialang.org/t/best-practice-for-avoiding-line-break-bugs-in-long-expressions/7475):
>
> I had a very frustrating debugging session yesterday. My code was running without errors but producing incorrect results. It took at least two hours for me to find this problem: x = a + really \* long + + (and + complex) / expression + - that \* (spans - several) + lines \* of / code Can you spot the error? The problem is that the expression terminates after the third line because I forgot to hang a trailing + sign there. The fourth line evaluates without error but doesn’t do anything…

Parentheses (or + at the end of the line) allows Julia to recognize the multiline code.

---

<div class="post-metadata">

### Author: ![SonntagK](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@SonntagK](https://discourse.julialang.org/u/SonntagK)
#### Post date: [September 25, 2020, 3:47pm UTC](https://discourse.julialang.org/t/strange-behaviour-with-if-else-statements/47266/5 "2020-09-25T15:47:24Z")

</div>

Thank you very much for your reply and the explanation! I see the problem with what I wrote.
