Operator Precedence - D Programming Language
Description: D Programming Language
Keywords: D programming language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Discord (community-run)
  - Forums
  - IRC
  - Wiki
  - Issues
  - Contributing
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# Operator Precedence

As we have been using throughout the book, expressions can be chained with more than one operator. For example, the following line contains four expressions chained with three operators:

        a = b + c * d    // three operators: =, +, and *

Operator precedence rules specify the order that the chained operators are executed in and the expressions that they use. Operators are executed according to their precedences: first the higher ones, then the lower ones.

The following is D's operator precedence table. Operators are listed from the highest precedence to the lowest. The ones that are in the same table row have the same precedence. (Line wrapping inside table cells is insignificant; for example, `==` and `!is` have the same precedence.) Unless specified otherwise, operators are left-associative.

Some of the terms used in the table are explained later below.

Operators | Description | Notes
`!` | Template instantiation | Cannot be chained
`=>` | Lambda definition | Not a real operator; occurs twice in the table; this row is for binding power to the left
`. ++ -- ( [` | Postfix operators | `(` and `[` must be balanced with `)` and `]`, respectively
`^^` | Power operator | Right-associative
`++ -- * + - ! & ~ cast` | Unary operators |
`* / %` | Binary operators |
`+ - ~` | Binary operators |
`<< >> >>>` | Bit shift operators |
`== != > < >= <= in !in is !is` | Comparison operators | Unordered with respect to bitwise operators, cannot be chained
`&` | Bitwise and | Unordered with respect to comparison operators
`^` | Bitwise xor | Unordered with respect to comparison operators
`|` | Bitwise or | Unordered with respect to comparison operators
`&&` | Logical and | Short-circuit
`||` | Logical or | Short-circuit
`?:` | Ternary operator | Right-associative
`= -= += = *= %= ^= ^^= ~= <<= >>= >>>=` | Assignment operators | Right-associative
`=>` | Lambda definition | Not a real operator; occurs twice in the table; this row is for binding power to the right
`,` | Comma operator | Not to be confused with using comma as a separator (e.g. in parameter lists)
`..` | Number range | Not a real operator; hardwired into syntax at specific points

###### Chaining

Let's consider the line from the beginning of the chapter:

        a = b + c * d

Because binary `*` has higher precedence than binary `+`, and binary `+` has higher precedence than `=`, that expression is executed as the following parenthesized equivalent:

        a = (b + (c * d))    // first *, then +, then =

As another example, because postfix `.` has higher precedence than unary `*`, the following expression would first access member `ptr` of object `o` and then dereference it:

        *o.ptr      // ← dereferences pointer member o.ptr
        *(o.ptr)    // ← equivalent of the above
        (*o).ptr    // ← NOT equivalent of the above

Some operators cannot be chained:

        if (a > b == c) {      // ← derleme HATASI
            // ...
        }

    Error: found '==' when expecting ')'

The programmer must specify the desired execution order with parentheses:

        if ((a > b) == c) {    // ← compiles
            // ...
        }

###### Associativity

If two operators have the same precedence, then their associativity determines which operator is executed first: the one on the left or the one on the right.

Most operators are left-associative; the one on the left-hand side is executed first:

        10 - 7 - 3;
        (10 - 7) - 3;    // ← equivalent of the above (== 0)
        10 - (7 - 3);    // ← NOT equivalent of the above (== 6)

Some operators are right-associative; the one on the right-hand side is executed first:

        4 ^^ 3 ^^ 2;
        4 ^^ (3 ^^ 2);    // ← equivalent of the above (== 262144)
        (4 ^^ 3) ^^ 2;    // ← NOT equivalent of the above (== 4096)

###### Unordered operator groups

Precedence between bitwise operators and logical operators are not specified by the language:

        if (a & b == c) {      // ← derleme HATASI
            // ...
        }

    Error: b == c must be parenthesized when next to operator &

The programmer must specify the desired execution order with parentheses:

        if ((a & b) == c) {    // ← compiles
            // ...
        }

###### The precedence of `=>`

Although `=>` is not an operator, it takes part in the table twice to specify how it interacts with operators on its left-hand side and right-hand side.

        l = a => a = 1;

Although both sides of `=>` above have an `=` operator, `=>` has precedence over `=` on the left hand side so it binds stronger to `a` as if the programmer wrote the following parentheses:

        l = (a => a = 1);

On the right-hand side, `=>` has lower precedence than `=`, so the `a` on the right-hand side binds stronger to `=` as if the following extra set of parentheses are specified:

        l = (a => (a = 1));

As a result, the lambda expression does not become just `a => a` but includes the rest of the expression as well: `a => a = 1`, which means given a, produce a = 1. That lambda is then assigned to the variable `l`.

Note: This is just an example; otherwise, `a = 1` is not a meaningful body for a lambda because the mutation to its parameter `a` is seemingly lost and the result of calling the lambda is always 1. (The reason I say "seemingly" is that the assignment operator may have been overloaded for `a`'s type and may have side-effects.)

###### Comma operator

Comma operator is a binary operator. It executes first the left-hand side expression then the right-hand side expression. The values of both expressions are ignored.

        int a = 1;
        foo(), bar(), ++a;

        assert(a == 2);

The comma operator is most commonly used with `for` loops when the loop iteration involves mutating more than one variable:

        for ({ int i; int j; } i < 10; ++i, ++j) {
            // ...
        }

Copyright © 1999-2026 by the D Language Foundation | Page generated by Ddoc on Mon Aug 17 18:57:12 2026