Operator Precedence help in if statements

Have a question that is bugging me a bit in regards to Precedence order. After reading the book, it says that the operator "&&" reads before "||". I understand that a bit, but was wondering if it applies when you use it for parenthesis also.

1. First set, it reads "true && false" which is "false". Then, reads "true || false" which is true. I understand this.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
	if (true || true && false)
	{
		cout << "Test1\n";
	}
 
	return 0;
}


2. My question is here, the book says parenthesis are read first, but even following the operator precedence, is (true && false) still read before
(true || false)?

Thank you very much.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
	if ((true || false) && (true && false))
	{
		cout << "Test 2\n";
	}
 
	return 0;
}
In lhs && rhs where lhs and rhs are expressions evaluated in a boolean context:
a. evaluation of lhs is sequenced before evaluation of rhs
b. short-circuit evaluation; so if lhs evaluates to false, rhs is not evaluated.

For example, where i and j are integers, evaluate: ( ++i ) && ( j = i+1 )
1. evaluate ( ++i ) (increment i)
2. if the result of the evaluation in a bool context is true (ie. i is non-zero after it is incremented),
evaluate ( j = i+1 ) (assign the value of i+1 to j)


In contrast, consider: evaluate: ( ++i ) + ( j = i+1 )
Here, the evaluations of ( ++i ) and ( j = i+1 ) are unsequenced
This results in undefined behaviour (unsequenced modification and access to scalar i)

The details: http://en.cppreference.com/w/cpp/language/eval_order
Thank you very much.
Topic archived. No new replies allowed.