# I don't understand the precedence in crystal

**URL:** <https://forum.crystal-lang.org/t/i-dont-understand-the-precedence-in-crystal/5926>\
**Category:** Help & Support\
**Created:** [August 11, 2023, 4:40pm UTC](https://forum.crystal-lang.org/t/i-dont-understand-the-precedence-in-crystal/5926 "2023-08-11T16:40:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![solvecodes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/solvecodes/32/2141_2.png) [@solvecodes](https://forum.crystal-lang.org/u/solvecodes)\
**Post date:** [August 11, 2023, 4:40pm UTC](https://forum.crystal-lang.org/t/i-dont-understand-the-precedence-in-crystal/5926/1 "2023-08-11T16:40:17Z")

</div>

```auto
Operator Precedence¶
This list is sorted by precedence, so upper entries bind stronger than lower ones.

Category	Operators
Index accessors	[], []?
Unary	+, &+, -, &-, !, ~
Exponential	**, &**
Multiplicative	*, &*, /, //, %
Additive	+, &+, -, &-
Shift	<<, >>
Binary AND	&
Binary OR/XOR	|,^
Equality and Subsumption	==, !=, =~, !~, ===
Comparison	<, <=, >, >=, <=>
Logical AND	&&
Logical OR	||
Range	.., ...
Conditional	?:
Assignment	=, []=, +=, &+=, -=, &-=, *=, &*=, /=, //=, %=, |=, &=,^=,**=,<<=,>>=, ||=, &&=
Splat	*, **

```

`**` has higher precedence than `+` and `*` why is it in the middle ?

---

<div class="post-metadata">

**Author:** ![botato](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@botato](https://forum.crystal-lang.org/u/botato)\
**Post date:** [August 11, 2023, 5:39pm UTC](https://forum.crystal-lang.org/t/i-dont-understand-the-precedence-in-crystal/5926/2 "2023-08-11T17:39:54Z")

</div>

Are you coming from a different language with an unusual precedence order? As far as I know it’s coming from standard math precedence and most languages follow, having exponentiation (\*\*) higher than multiplication (\*) higher than addition (+). For example the [python operator precedence](https://www.programiz.com/python-programming/precedence-associativity) is quite similar

---

<div class="post-metadata">

**Author:** ![solvecodes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/solvecodes/32/2141_2.png) [@solvecodes](https://forum.crystal-lang.org/u/solvecodes)\
**Post date:** [August 11, 2023, 7:11pm UTC](https://forum.crystal-lang.org/t/i-dont-understand-the-precedence-in-crystal/5926/3 "2023-08-11T19:11:56Z")

</div>

but look how they are sorted in the page low precedence and then higher and then low ???

---

<div class="post-metadata">

**Author:** ![npn](https://avatars.discourse-cdn.com/v4/letter/n/e68b1a/32.png) [@npn](https://forum.crystal-lang.org/u/npn)\
**Post date:** [August 11, 2023, 7:25pm UTC](https://forum.crystal-lang.org/t/i-dont-understand-the-precedence-in-crystal/5926/4 "2023-08-11T19:25:41Z")

</div>

`+` here is unary operator, like this: `+int`

---

<div class="post-metadata">

**Author:** ![botato](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@botato](https://forum.crystal-lang.org/u/botato)\
**Post date:** [August 11, 2023, 7:26pm UTC](https://forum.crystal-lang.org/t/i-dont-understand-the-precedence-in-crystal/5926/5 "2023-08-11T19:26:28Z")

</div>

the list is sorted from higher precedence to lower precedence going from top to bottom. Maybe you are confused why \*\* appears lower than + and \* despite having exponentiation having higher precedence? \*\* can mean two distinct things, exponentiation or splat, exponentiation has higher precedence and [splat](https://crystal-lang.org/reference/1.9/syntax_and_semantics/splats_and_tuples.html) has lower

---

<div class="post-metadata">

**Author:** ![solvecodes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/solvecodes/32/2141_2.png) [@solvecodes](https://forum.crystal-lang.org/u/solvecodes)\
**Post date:** [August 11, 2023, 7:30pm UTC](https://forum.crystal-lang.org/t/i-dont-understand-the-precedence-in-crystal/5926/6 "2023-08-11T19:30:47Z")

</div>

yes that what confuses me + higher than \*\* what !!! that is my confusion

---

<div class="post-metadata">

**Author:** ![solvecodes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/solvecodes/32/2141_2.png) [@solvecodes](https://forum.crystal-lang.org/u/solvecodes)\
**Post date:** [August 11, 2023, 7:40pm UTC](https://forum.crystal-lang.org/t/i-dont-understand-the-precedence-in-crystal/5926/7 "2023-08-11T19:40:26Z")

</div>

but it should not be on top because \*\* is higher

---

<div class="post-metadata">

**Author:** ![solvecodes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/solvecodes/32/2141_2.png) [@solvecodes](https://forum.crystal-lang.org/u/solvecodes)\
**Post date:** [August 11, 2023, 7:43pm UTC](https://forum.crystal-lang.org/t/i-dont-understand-the-precedence-in-crystal/5926/8 "2023-08-11T19:43:30Z")

</div>

i am really confused the precedence list is messed up

---

<div class="post-metadata">

**Author:** ![botato](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@botato](https://forum.crystal-lang.org/u/botato)\
**Post date:** [August 11, 2023, 7:55pm UTC](https://forum.crystal-lang.org/t/i-dont-understand-the-precedence-in-crystal/5926/9 "2023-08-11T19:55:26Z")

</div>

Operator precedence can be confusing at first, but this is not any different in crystal than in ruby or python that also have unary + operator and \*\* as splat operator.  
unary + as in +5 meaning just positive 5 is highest  
then exponentiation, ie 5\*\*2 == 25  
then + as in addition, as in 5 + 2 == 7  
then \*\* meaning splat, as in:

```auto
tuple = {chomp: true}
gets(**tuple)

```
