# Best way for comparisons in one line to avoid undesired effects of operator precedence

**URL:** <https://discourse.julialang.org/t/best-way-for-comparisons-in-one-line-to-avoid-undesired-effects-of-operator-precedence/49973>\
**Category:** New to Julia\
**Created:** [November 11, 2020, 9:50am UTC](https://discourse.julialang.org/t/best-way-for-comparisons-in-one-line-to-avoid-undesired-effects-of-operator-precedence/49973 "2020-11-11T09:50:45Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [November 11, 2020, 9:50am UTC](https://discourse.julialang.org/t/best-way-for-comparisons-in-one-line-to-avoid-undesired-effects-of-operator-precedence/49973/1 "2020-11-11T09:50:45Z")

</div>

I am wondering how to write best a simple

```julia
if comparison 
    expression 
else 
    expression
end

```

in one line.

```julia
julia> if 7+2<10 3 else 0 end
3

```

works as desired. If the comparison is true, give a value 3 else give a value 0.

But if the value resulting from condition true should be negative = - 3 then the above does not work any more:

```julia
julia> if 7+2<10 -3 else 0 end
0

```

It is executed as _if 7+2\<(10 -3) else 0 end_ and this is not the code I want.

So far understandable to me.  
Splitting the code over two lines would help:

```julia
julia> if 7+2<10  
         -3 else 0 end
-3

```

I am trying to fine ways to write it as oneliner:

```julia
julia> if (7+2<10) -3 else 0 end
ERROR: TypeError: non-boolean (Int64) used in boolean context

```

also gives an error.  
The versions below give also an errors:

```julia
julia> if (7+2<10) (-3) else 0 end
ERROR: syntax: space before "(" not allowed in "((7 + 2) < 10) (" at REPL[131]:1
julia> if (7+2<10) (3) else 0 end
ERROR: syntax: space before "(" not allowed in "((7 + 2) < 10) (" at REPL[131]:1

```

Surprisingly the following works as desired:

```julia
julia> if 7+2<10 (-3) else 0 end
julia> if (7+2<10) 3 else 0 end

```

So I wonder what is the best way to for a “if else end” statement **in one line**  
to clearly separate the condition from the expression if the condition is true.

A ternary operator is also an option.

From my experiments it seems best not to use parentheses in then condition part and use parentheses for the expression part may work.  
Like:

```julia
if 7+2<10 && 4+1<=8-1 (-3) else 0 end

```

Is there another way to clearly separate condition part and the expression part in a if then else oneliner?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [November 11, 2020, 9:56am UTC](https://discourse.julialang.org/t/best-way-for-comparisons-in-one-line-to-avoid-undesired-effects-of-operator-precedence/49973/2 "2020-11-11T09:56:34Z")

</div>

Two ways:

```julia
julia> if 7+2<10; -3 else 0 end
-3

julia> 7+2<10 ? -3 : 0
-3

```

---

<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:** [November 11, 2020, 9:57am UTC](https://discourse.julialang.org/t/best-way-for-comparisons-in-one-line-to-avoid-undesired-effects-of-operator-precedence/49973/3 "2020-11-11T09:57:14Z")

</div>

Thanks a lot!  
What does this “;” do? Could you explain more? A semicolon can be used allways to separate parts of code where it is not clear?

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [November 11, 2020, 12:21pm UTC](https://discourse.julialang.org/t/best-way-for-comparisons-in-one-line-to-avoid-undesired-effects-of-operator-precedence/49973/4 "2020-11-11T12:21:15Z")

</div>

No it’s just that `A ? B : C` is Julia’s cool syntax to say `if A then B else C`.

[EDIT] Sorry, I misread your semicolon for a colon (you wrote “colon” instead of “semicolon”, which confused me)… In `if A; B else C end`, the semicolon `;` does what you think: it separates the condition `A` from what to do if `A` is true.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 11, 2020, 5:53pm UTC](https://discourse.julialang.org/t/best-way-for-comparisons-in-one-line-to-avoid-undesired-effects-of-operator-precedence/49973/5 "2020-11-11T17:53:21Z")

</div>

To complement a little @briochemc comment, in Julia semicolons (outside function calls) and newlines are interchangeable, so if you _need_ a newline anywhere, you may use instead a semicolon in its place.

---

<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:** [November 12, 2020, 11:53am UTC](https://discourse.julialang.org/t/best-way-for-comparisons-in-one-line-to-avoid-undesired-effects-of-operator-precedence/49973/6 "2020-11-12T11:53:23Z")

</div>

Thank you very much for your answers. Now I understand it better!
