I know there must be some problems with the boolean expression in each if-statement. A friend tells me that it is wrong to write like this. However, it gives the values of b and c (i.e. 20, 0) when I run it.
#include <iostream>
usingnamespace std;
int main()
{
int a = 10;
int b = 20;
int c = 0;
if (a < b < c)
{
cout << a << endl;
}
if (b < c < a)
{
cout << b << endl;
}
if (c < b < a)
{
cout << c << endl;
}
return 0;
}
Less-than is a binary operator. Binary operator has two operands and returns one value. Less-than is a relational operator. The return value of a relational operator is bool.
Operators in same grouping have order of evaluation. Concatenated relational operators evaluate from left to right. Therefore, a < b < c means same as (a < b) < c.
Lets evaluate the leftmost part. Substitute values first: (10 < 12) < c
and evaluate: true < c
Now we can substitute the rest: true < 0
What does that evaluate to?
Different operators have different precedence. Less-than has higher precedence than logical and. Therefore, a < b && b < c
is same as (a < b) && (b < c)
PS. Your original program does not handle all orderings of a, b, c; just three cases.