# Unexpected precedence of \`&\` and \`|\`?

**URL:** <https://discourse.julialang.org/t/unexpected-precedence-of-and/63351>\
**Category:** New to Julia\
**Created:** [June 22, 2021, 8:36am UTC](https://discourse.julialang.org/t/unexpected-precedence-of-and/63351 "2021-06-22T08:36:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![scheidan1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheidan1/32/24771_2.png) [@scheidan1](https://discourse.julialang.org/u/scheidan1)\
**Post date:** [June 22, 2021, 8:36am UTC](https://discourse.julialang.org/t/unexpected-precedence-of-and/63351/1 "2021-06-22T08:36:49Z")

</div>

In my code I found recently an innocent looking bug of this type:

```nohighlight
x=1
y=2
if x < 3 & y < 6
... do important stuff ...
end

```

What I meant to check in the `if` condition was of course

```nohighlight
 (x < 3) & (y < 6)
## or
 x < 3 && y < 6

```

My current take-way is that I should never use `&` but always `&&` except I really want know what I’m doing (i.e. fiddling with low-level stuff). Is this reasoning correct or did I miss a subtle point?

PS: Maybe it we should emphasize this difference more in the [Noteworthy differences from R](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-R) section

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 22, 2021, 8:39am UTC](https://discourse.julialang.org/t/unexpected-precedence-of-and/63351/2 "2021-06-22T08:39:36Z")

</div>

`&` is bitwise AND, `&&` is short-circuiting logical AND. On booleans, these of course behave semantically the same, but if you chain things, precedence of `&` takes over compared to e.g. `<` and having some non-boolean on either side will lead to an unexpected result.

I usually avoid `&` for control flow for that reason and exclusively use `&&` and `||` for control flow.
