Why does (0 < 5 < 2) result true in C++?

Viewed 97

The following code std::cout << (0 < 5 < 2); outputs 1 (true) to the console, even though mathematically speaking, 5 < 2 is false. What is the logic behind this?

Hypothesis: This is because 0 < 5 is calculated as 1, therefore 0 < 5 < 2 is calculated as 1 < 2. Am I correct?

1 Answers

You have answered the question yourself, you are 100% correct. Evaluation is done on the basis of precedence. Might help you:-C++ operators Precedence

Related