# 5 \< x != y \< 5

**URL:** <https://discourse.julialang.org/t/5-x-y-5/53844>\
**Category:** General Usage\
**Created:** [January 23, 2021, 9:48pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844 "2021-01-23T21:48:03Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![JimFromAZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jimfromaz/32/20720_2.png) [@JimFromAZ](https://discourse.julialang.org/u/JimFromAZ)\
**Post date:** [January 23, 2021, 9:48pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/1 "2021-01-23T21:48:03Z")

</div>

Hi All,  
I am reviewing [The Fast Track to julia](https://juliadocs.github.io/Julia-Cheat-Sheet/). In the Basics section Chaining is mentioned. Three lines of code are:

x = y = z = 1 # right-to-left  
0 \< x \< 3 # true  
5 \< x != y \< 5 # false

It’s the last line that I can’t figure out the logic flow. Can anyone provide me with sufficient details as to how I should evaluate this last line? The Fast Track states it is false as per their comment. I get the same result if I code just the third line and set x=1 and y=1. But if I set x=6 and y=2 it evaluates to true. Any help is greatly appreciated.

Thanks!

Jim

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 23, 2021, 9:54pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/2 "2021-01-23T21:54:33Z")

</div>

[https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity)

they all have the same precedence and are not associative

---

<div class="post-metadata">

**Author:** ![sz73](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sz73/32/15916_2.png) [@sz73](https://discourse.julialang.org/u/sz73)\
**Post date:** [January 23, 2021, 9:57pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/3 "2021-01-23T21:57:45Z")

</div>

rather this subsection [Mathematical Operations and Elementary Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Chaining-comparisons)

the last line should be equivalent to (5 \< x) && ( x != y) && (y \< 5)

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [January 23, 2021, 9:58pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/4 "2021-01-23T21:58:20Z")

</div>

> [@JimFromAZ](#):
>
> if I set x=6 and y=2 it evaluates to true

Yes, but from line 1: `x = y = z = 1`

---

<div class="post-metadata">

**Author:** ![sz73](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sz73/32/15916_2.png) [@sz73](https://discourse.julialang.org/u/sz73)\
**Post date:** [January 23, 2021, 10:04pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/5 "2021-01-23T22:04:40Z")

</div>

I think the question is more about how to arrive at the answer, than the very specific answer to this specific problem

---

<div class="post-metadata">

**Author:** ![JimFromAZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jimfromaz/32/20720_2.png) [@JimFromAZ](https://discourse.julialang.org/u/JimFromAZ)\
**Post date:** [January 23, 2021, 10:20pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/6 "2021-01-23T22:20:12Z")

</div>

jling, Thank you so much for the link! I should have checked the Manual as well.

Take care, Jim

---

<div class="post-metadata">

**Author:** ![JimFromAZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jimfromaz/32/20720_2.png) [@JimFromAZ](https://discourse.julialang.org/u/JimFromAZ)\
**Post date:** [January 23, 2021, 10:21pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/7 "2021-01-23T22:21:16Z")

</div>

sz73, You have the best answer. Thank you for providing the logic! Now it makes sense to me. The link helps as well.

Take care, Jim.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [January 23, 2021, 10:21pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/8 "2021-01-23T22:21:59Z")

</div>

I’d suggest the best way to figure this stuff out is to quote the expression and dump it out:

```julia
dump(:(5 < x != y < 5))

```

Then you can reason out why the expression was parsed the way it was after seeing unambiguously how it’s being parsed.

---

<div class="post-metadata">

**Author:** ![JimFromAZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jimfromaz/32/20720_2.png) [@JimFromAZ](https://discourse.julialang.org/u/JimFromAZ)\
**Post date:** [January 23, 2021, 10:22pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/9 "2021-01-23T22:22:39Z")

</div>

Thanks, pbayer! Yes, it’s the logic I’m trying to figure out and sz73’s equivalent logic definitely helps.

Take care, Jim.

---

<div class="post-metadata">

**Author:** ![JimFromAZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jimfromaz/32/20720_2.png) [@JimFromAZ](https://discourse.julialang.org/u/JimFromAZ)\
**Post date:** [January 23, 2021, 10:24pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/10 "2021-01-23T22:24:03Z")

</div>

JMW, I will definitely try the dump feature and see what I come up with. Thanks for the insight!

Jim

---

<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:** [January 23, 2021, 11:13pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/11 "2021-01-23T23:13:12Z")

</div>

You can mark his answer as “the answer”, if that helps.

---

<div class="post-metadata">

**Author:** ![JimFromAZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jimfromaz/32/20720_2.png) [@JimFromAZ](https://discourse.julialang.org/u/JimFromAZ)\
**Post date:** [January 24, 2021, 1:08am UTC](https://discourse.julialang.org/t/5-x-y-5/53844/12 "2021-01-24T01:08:47Z")

</div>

Thanks, briochemc. Newbie here. Will do.

Take care, Jim.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 24, 2021, 6:19am UTC](https://discourse.julialang.org/t/5-x-y-5/53844/13 "2021-01-24T06:19:04Z")

</div>

> [@johnmyleswhite](#):
>
> `dump(:(5 < x != y < 5))`

Or, more easily perhaps, `Meta.@dump 5 < x != y < 5`

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 24, 2021, 12:33pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/14 "2021-01-24T12:33:42Z")

</div>

Or even:

```julia
julia> Meta.@lower 5 < x != y < 5
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = 5 < x
└── goto #5 if not %1
2 ─ %3 = x != y
└── goto #4 if not %3
3 ─ %5 = y < 5
└── return %5
4 ─ return false
5 ─ return false
))))

```

🙂

One day I will find out what a thunk is. Perhaps not today.

---

<div class="post-metadata">

**Author:** ![JimFromAZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jimfromaz/32/20720_2.png) [@JimFromAZ](https://discourse.julialang.org/u/JimFromAZ)\
**Post date:** [January 24, 2021, 6:39pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/15 "2021-01-24T18:39:26Z")

</div>

Thanks, cormullion! Very helpful.

I had to search for “thunk” and found this: [Thunk - Wikipedia](https://en.wikipedia.org/wiki/Thunk). Not sure that helps.

Take care, Jim.

---

<div class="post-metadata">

**Author:** ![sz73](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sz73/32/15916_2.png) [@sz73](https://discourse.julialang.org/u/sz73)\
**Post date:** [January 25, 2021, 11:48am UTC](https://discourse.julialang.org/t/5-x-y-5/53844/16 "2021-01-25T11:48:20Z")

</div>

```julia
dump(:(5 < x != y < 5))
Expr
  head: Symbol comparison
  args: Array{Any}((7,))
    1: Int64 5
    2: Symbol <
    3: Symbol x
    4: Symbol !=
    5: Symbol y
    6: Symbol <
    7: Int64 5

```

How does this help to understand the behavior of the chaining of the operators?

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [January 25, 2021, 11:54am UTC](https://discourse.julialang.org/t/5-x-y-5/53844/17 "2021-01-25T11:54:13Z")

</div>

You’re right: in this example, it doesn’t help because the order of operations isn’t discernable from the parse structure.

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [January 25, 2021, 6:15pm UTC](https://discourse.julialang.org/t/5-x-y-5/53844/18 "2021-01-25T18:15:26Z")

</div>

As an aside, I hope people would avoid this in real code:  
`5 < x != y < 5 # false`  
which is difficult for the reader to understand without thinking a minute or even wondering if it’s a bug. It is more a brain-teaser to illustrate chaining, than a recommendation for actual code. The following is clearer and more explicit:

> [@sz73](#):
>
> (5 \< x) && ( x != y) && (y \< 5)

Chaining is great when it’s simple and close to what you’d write with math, as in the first examples above:

```julia
x = y = z = 1 # right-to-left
0 < x < 3 # true

```

The slowest compiler is the human reader, so try to be helpful to them, or even yourself if you ever return to the code months/years later.

As a cranky person, I also avoid the idiomatic

```julia
x == y && dosomething()

```

to avoid an `if` statement like

```julia
if x == y; dosomething(); end

```

I think the `if` makes intent clearer and doesn’t assume all readers to understand short-circuiting. (I also prefer the semi-colons even if they can be omitted, just to make it easier to read.) [edit: bug fix]

---

<div class="post-metadata">

**Author:** ![JimFromAZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jimfromaz/32/20720_2.png) [@JimFromAZ](https://discourse.julialang.org/u/JimFromAZ)\
**Post date:** [January 26, 2021, 3:37am UTC](https://discourse.julialang.org/t/5-x-y-5/53844/19 "2021-01-26T03:37:42Z")

</div>

apo383,  
Makes complete sense! A hacker (of the original sense) might appreciate the most minimal code, but mere mortals (me) need readability and ease of comprehension.
