Is (4 y 1) a valid statement in C++? How do you evaluate it if so?
The statement (4 > y > 1)
is parsed as this:
((4 > y) > 1)
The comparison operators <
and >
evaluate left-to-right.
The 4 > y
returns either 0
or 1
depending on if it's true or not.
Then the result is compared to 1.
In this case, since 0
or 1
is never more than 1
, the whole statement will always return false.
There is one exception though:
If y
is a class and the >
operator has been overloaded to do something unusual. Then anything goes.
For example, this will fail to compile:
class mytype{
};
mytype operator>(int x,const mytype &y){
return mytype();
}
int main(){
mytype y;
cout << (4 > y > 1) << endl;
return 0;
}
if statements and relational and comparison operators: Exceptions When comparing three values/variables
The 2 < n < 1
ain't does what you think it should:
Because these operators group left-to-right, the expression
a<b<c
is
parsed(a<b)<c
, and nota<(b<c)
or(a<b)&&(b<c)
- cppreference.com
The 2 < n
part will return a Boolean value which in turn will be compared in the < 1
.
2 < n < 1;
// equal to
(2 < n) < 1;
So, in total, the 2 < n < 1
flows like so:
(2 < n) < 1
. Is n greater than 2? No, returnfalse
.false < 1
. Thefalse
is promoted to an integer, to 0. Is 0 less than 1? Yes, theif
condition istrue
.
That's why when n == 3
in the 2 < n < 1 < 1 < 1
, the overall you get false
:
(2 < 3) < 1 < 1 < 1
. Is 3 greater than 2? Yes! Returns1
(true
)(1 < 1) < 1 < 1
. Is 1 less than 1? No! Return0
(false
)(0 < 1) < 1
. Is 0 less than 1? Yes! Return1
(true
)1 < 1
. Is 1 less than 1? No! Return0
(false
)
It is nonsensical as you can see. In order to make it work, you will have to make explicit checks:
(n < 2) && (n > 1)
// is n less than 2 AND is n greater than 1
Is (val1 val2 val3) a valid comparison in C?
This is valid code, but it won't do what you expect.
The >
operator is left-associative, so the expression effectively becomes:
((sizeAllocated > type_get_binary_size(data)) > spaceAvailable)
The inner portion will evaluate to 0 if the condition is false or 1 if the condition is true. This value is then compared against spaceAvailable
.
In C, the results of a comparison operator have an integer type, so comparing this result to an integer is valid.
So what you're actually doing is either 0 > spaceAvailable
or 1 > spaceAvailable
, depending on how the first conditional evaluates.
What you probably want is this:
int size = type_get_binary_size(data);
if ((sizeAllocated > size) && (size > spaceAvailable))
Note that the function call is done first before the if
so it isn't called twice in the conditional.
Checking if a value is within a range in if statment
The first version would be evaluated like this:
if( ( '0' <= infix.at(i) ) <= '9' )
which is why you get a warning about bool
- you wind up with true/false <= '9'
which doesn't make sense.
Some languages allow you to chain comparison operators, but C++ is not one of them.
I can do x = y = z. How come x y z is not allowed in C++?
It is because you see those expressions as "chain of operators", but C++ has no such concept. C++ will execute each operator separately, in an order determined by their precedence and associativity (https://en.cppreference.com/w/cpp/language/operator_precedence).
(Expanded after C Perkins's comment)
James, your confusion comes from looking at x = y = z;
as some special case of chained operators. In fact it follows the same rules as every other case.
This expression behaves like it does because the assignment =
is right-to-left associative and returns its right-hand operand. There are no special rules, don't expect them for x < y < z
.
By the way, x == y == z
will not work the way you might expect either.
See also this answer.
Related Topics
Why Doesn't a Simple "Hello World"-Style Program Compile With Turbo C++
How Does C++ Handle &&? (Short-Circuit Evaluation)
Inline Functions VS Preprocessor Macros
What Happens When I Print an Uninitialized Variable in C++
Why Does C++ Require a Cast For Malloc() But C Doesn'T
How to Write a Large Buffer into a Binary File in C++, Fast
Why Does Printf Not Print Out Just One Byte When Printing Hex
How to Generate Different Random Numbers in a Loop in C++
What Does Casting to 'Void' Really Do
How to Use the Conditional (Ternary) Operator
Shared_Ptr to an Array: Should It Be Used
Difference Between Static and Shared Libraries
Sending a Sequence of Commands and Wait For Response
Rules For C++ String Literals Escape Character
Declaring Variables Inside Loops, Good Practice or Bad Practice