# Short circuit operators

**URL:** <https://discourse.julialang.org/t/short-circuit-operators/3846>\
**Category:** General Usage\
**Created:** [May 22, 2017, 10:54am UTC](https://discourse.julialang.org/t/short-circuit-operators/3846 "2017-05-22T10:54:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [May 22, 2017, 10:54am UTC](https://discourse.julialang.org/t/short-circuit-operators/3846/1 "2017-05-22T10:54:06Z")

</div>

is the last line intended behaviour?

```julia
julia> false || println("Here")
Here

julia> true || println("Here")
true

julia> false && println("Here")
false

julia> true && println("Here")
Here

julia> true && false || println("Here")
Here

julia> false && true || println("Here")
Here

```

---

<div class="post-metadata">

**Author:** ![shorty66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shorty66/32/3254_2.png) [@shorty66](https://discourse.julialang.org/u/shorty66)\
**Post date:** [May 22, 2017, 11:21am UTC](https://discourse.julialang.org/t/short-circuit-operators/3846/2 "2017-05-22T11:21:31Z")

</div>

> [@gdkrmr](#):
>
> julia\> (true && false) || println(“Here”)  
> Here
> 
> julia\> (false && true) || println(“Here”)  
> Here

The expression in parantheses evaluates to `false`. I think this is intended.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 22, 2017, 11:46am UTC](https://discourse.julialang.org/t/short-circuit-operators/3846/3 "2017-05-22T11:46:40Z")

</div>

Its documented here: [https://docs.julialang.org/en/stable/manual/control-flow/#short-circuit-evaluation](https://docs.julialang.org/en/stable/manual/control-flow/#short-circuit-evaluation)

Specifically this:

> Both `&&` and `||` associate to the right, but `&&` has higher precedence than `||` does.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [May 22, 2017, 11:49am UTC](https://discourse.julialang.org/t/short-circuit-operators/3846/4 "2017-05-22T11:49:58Z")

</div>

In many programming languages, including Julia, `&&` has higher precedence than `||`.

Other programming languages, like bash, give these operators equal precedence, so the result would be different.

Some style guides recommend using parentheses in expressions involving both `&&` and `||`, to help the uncertain reader.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [May 22, 2017, 12:57pm UTC](https://discourse.julialang.org/t/short-circuit-operators/3846/5 "2017-05-22T12:57:55Z")

</div>

so if `&&` and `||` are right-associative, then why does the following not print anything, shouldn’t `true && println("Here")` get evaluated first?

```julia
julia> false && true && println("Here")
false

julia> parse("""false && true && println("Here")""")
:(false && (true && println("Here")))

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 22, 2017, 1:02pm UTC](https://discourse.julialang.org/t/short-circuit-operators/3846/6 "2017-05-22T13:02:50Z")

</div>

The compiler is very smart.

```julia
function f()
    false && true && println("Here")
end

```

```julia
julia> @code_warntype f()
Variables:
  #temp#

Body:
  begin 
      return $(QuoteNode(false))
  end::Bool

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [May 22, 2017, 1:29pm UTC](https://discourse.julialang.org/t/short-circuit-operators/3846/7 "2017-05-22T13:29:02Z")

</div>

> [@gdkrmr](#):
>
> so if && and || are right-associative, then why does the following not print anything, shouldn’t true && println(“Here”) get evaluated first?

right-associative means that `:(A && B && C)` parses as `:(A && (B && C))` (as opposed to `:((A && B) && C)`). `A` is evaluated first because these expressions are evaluated left-to-right in Julia, and since it’s false, the whole expression is known to be false and there’s no need to evaluate the rest of it.
