# Parenthesizing conditions

**URL:** https://discourse.julialang.org/t/parenthesizing-conditions/80000
**Category:** New to Julia
**Created:** [April 25, 2022, 5:39pm UTC](https://discourse.julialang.org/t/parenthesizing-conditions/80000 "2022-04-25T17:39:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [April 25, 2022, 5:39pm UTC](https://discourse.julialang.org/t/parenthesizing-conditions/80000/1 "2022-04-25T17:39:50Z")

</div>

The [Julia Style Guide](https://docs.julialang.org/en/v1/manual/style-guide/#Don't-parenthesize-conditions) says:

> **Don’t parenthesize conditions** …Write:
> 
> ```julia
> if a == b
> 
> ```
> 
> instead of:
> 
> ```julia
> if (a == b)
> 
> ```

However, I recently discovered a potentially dangerous characteristic of Julia, which, as far as I know, can only be prevented by parenthesizing conditions. Starting with this simple `or` statement:

```julia
julia> true | false
true

```

Everything is working as expected. However, if we replace `true` and `false` with conditions that return `true` and `false`, respectively, the `or` statement now returns `false`:

```julia
julia> 1==1 | 1==2
false

```

Putting the conditions in parentheses is uglier, but fixes the problem:

```julia
julia> (1==1) | (1==2)
true

```

For comparison, MATLAB returns `true` for all of the above situations. Here’s the MATLAB:

```matlab
>> true | false
ans =
  logical
   1
>> 1==1 | 1==2
ans =
  logical
   1
>> (1==1) | (1==2)
ans =
  logical
   1

```

**My question:** _What’s the appropriate way to deal with conditionals? Ignore the Style Guide, or is there some other way to write conditionals that’s less prone to error or misinterpretation?_

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 25, 2022, 5:43pm UTC](https://discourse.julialang.org/t/parenthesizing-conditions/80000/2 "2022-04-25T17:43:41Z")

</div>

> [@chadagreene](#):
>
> `1==1 | 1==2`

Using logical or instead of bit wise or ‘fixes’ it, too:

```julia
julia> 1==1 || 1==2
true

```

But you got a point: a potential trap.

---

<div class="post-metadata">

### Author: ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)
#### Post date: [April 25, 2022, 5:54pm UTC](https://discourse.julialang.org/t/parenthesizing-conditions/80000/3 "2022-04-25T17:54:17Z")

</div>

> [@chadagreene](#):
>
> ```julia
> julia> (1==1) | (1==2)
> 
> ```

If this were the condition it would be written as

```julia
if (1==1) | (1==2)

```

instead of

```julia
if ((1==1) | (1==2))

```

so I don’t see the discrepancy. What you highlighted is a matter of order of operations.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [April 25, 2022, 5:54pm UTC](https://discourse.julialang.org/t/parenthesizing-conditions/80000/4 "2022-04-25T17:54:34Z")

</div>

I believe the style guide refers to an outer pair of parentheses, which is a required part of the `if` statement in some programming languages but unnecessary in Julia. You shouldn’t be afraid of using semantically significant or clarifying parentheses. I.e., the style guide is fine with

```julia
if (1 == 1) | (1 == 2)

```

but advises against

```julia
if ((1 == 1) | (1 == 2))

```
