Questions tagged [operator-precedence]

Operator Precedence refers to the rules governing the order in which operators are evaluated within an expression or statement in a programming language. Distinct from [order-of-execution] which covers the sequencing of execution events in a software system.

In mathematics and computer programming, operators such as "+", "*", and "!" are applied in a defined order when they appear together in an expression so that every expression will be evaluated the same way across multiple environments. The order in which they are applied is called the precedence of the operators. This tag is distinct from which concern the order in which things like callback functions or event handlers are executed in a software system.

For example, the expression

a + b > a * b || !skip

would be ambiguous if the order in which operators are evaluated was undefined. In most computer languages those operators have the following precedence

  • ! (logical not)
  • * (multiplication)
  • + (addition)
  • > (boolean "greater than")
  • || (boolean "or")

So the expression would be evaluated

  • !skip
  • a * b
  • a + b
  • (a + b) > (a * b)
  • ((a + b) > (a * b)) || (!skip)

This tag is for questions that concern how such expressions are evaluated in a programming language.

1808 questions
906
votes
15 answers

Why are these constructs using pre and post-increment undefined behavior?

#include int main(void) { int i = 0; i = i++ + ++i; printf("%d\n", i); // 3 i = 1; i = (i++); printf("%d\n", i); // 2 Should be 1, no ? volatile int u = 0; u = u++ + ++u; printf("%d\n", u); // 1 u = 1; …
PiX
  • 9,705
  • 4
  • 19
  • 11
490
votes
8 answers

Why does "not(True) in [False, True]" return False?

If I do this: >>> False in [False, True] True That returns True. Simply because False is in the list. But if I do: >>> not(True) in [False, True] False That returns False. Whereas not(True) is equal to False: >>> not(True) False Why?
Texom512
  • 4,785
  • 3
  • 16
  • 16
341
votes
10 answers

'AND' vs '&&' as operator

I have a codebase where developers decided to use AND and OR instead of && and ||. I know that there is a difference in operators' precedence (&& goes before and), but with the given framework (PrestaShop to be precise) it is clearly not a…
ts.
  • 10,510
  • 7
  • 47
  • 73
245
votes
12 answers

Post-increment and pre-increment within a 'for' loop produce same output

The following for loops produce identical results even though one uses post increment and the other pre-increment. Here is the code: for(i=0; i<5; i++) { printf("%d", i); } for(i=0; i<5; ++i) { printf("%d", i); } I get the same output for…
theReverseFlick
  • 5,894
  • 8
  • 32
  • 33
228
votes
5 answers

SQL Logic Operator Precedence: And and Or

Are the two statements below equivalent? SELECT [...] FROM [...] WHERE some_col in (1,2,3,4,5) AND some_other_expr and SELECT [...] FROM [...] WHERE some_col in (1,2,3) or some_col in (4,5) AND some_other_expr Is there some sort of truth table I…
nc.
  • 7,179
  • 5
  • 28
  • 38
212
votes
14 answers

Why is x == (x = y) not the same as (x = y) == x?

Consider the following example: class Quirky { public static void main(String[] args) { int x = 1; int y = 3; System.out.println(x == (x = y)); // false x = 1; // reset System.out.println((x = y) == x);…
John McClane
  • 3,498
  • 3
  • 12
  • 33
173
votes
7 answers

Is short-circuiting logical operators mandated? And evaluation order?

Does the ANSI standard mandate the logical operators to be short-circuited, in either C or C++? I'm confused for I recall the K&R book saying your code shouldn't depend on these operations being short circuited, for they may not. Could someone…
Joe Pineda
  • 5,521
  • 3
  • 31
  • 40
156
votes
1 answer

Why does (1 in [1,0] == True) evaluate to False?

When I was looking at answers to this question, I found I didn't understand my own answer. I don't really understand how this is being parsed. Why does the second example return False? >>> 1 in [1,0] # This is expected True >>> 1 in…
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
139
votes
7 answers

Enforcing statement order in C++

Suppose I have a number of statements that I want to execute in a fixed order. I want to use g++ with optimization level 2, so some statements could be reordered. What tools does one have to enforce a certain ordering of statements? Consider the…
S2108887
  • 1,263
  • 2
  • 9
  • 7
122
votes
7 answers

Why in Python does "0, 0 == (0, 0)" equal "(0, False)"?

In Python (I checked only with Python 3.6 but I believe it should hold for many of the previous versions as well): (0, 0) == 0, 0 # results in a two element tuple: (False, 0) 0, 0 == (0, 0) # results in a two element tuple: (0, False) (0, 0) ==…
Piotr Zakrzewski
  • 3,591
  • 6
  • 26
  • 28
120
votes
5 answers

Why does the ternary operator with commas evaluate only one expression in the true case?

I'm currently learning C++ with the book C++ Primer and one of the exercises in the book is: Explain what the following expression does: someValue ? ++x, ++y : --x, --y What do we know? We know that the ternary operator has a higher precedence…
120
votes
5 answers

C++ execution order in method chaining

The output of this program: #include class c1 { public: c1& meth1(int* ar) { std::cout << "method 1" << std::endl; *ar = 1; return *this; } void meth2(int ar) { std::cout << "method 2:"<< ar…
Moises Viñas
  • 1,073
  • 2
  • 7
  • 9
118
votes
8 answers

Priority of the logical operators (order of operations) for NOT, AND, OR in Python

As far as I know, in C & C++, the priority sequence for NOT AND & OR is NOT>AND>OR. But this doesn't seem to work in a similar way in Python. I tried searching for it in the Python documentation and failed (Guess I'm a little impatient.). Can…
117
votes
7 answers

Operator precedence with JavaScript's ternary operator

I can’t seem to wrap my head around the first part of this code ( += ) in combination with the ternary operator. h.className += h.className ? ' error' : 'error' The way I think this code works is as follows: h.className = h.className + h.className…
111
votes
3 answers

What are the evaluation order guarantees introduced by C++17?

What are the implications of the voted in C++17 evaluation order guarantees (P0145) on typical C++ code? What does it change about things like the following? i = 1; f(i++, i) and std::cout << f() << f() << f(); or f(g(), h(), j());
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
1
2 3
99 100