Videos
Can relational operators be overloaded in C?
Can we use relational operators with Boolean values in C?
Can relational operators be chained in C?
In C, you cannot write a condition like
if (-4 <= X <= 8) {
// ...
} else {
// ...
}
Instead, you will have to split this into two separate checks:
if (-4 <= X && X <= 8) {
// ...
} else {
// ...
}
This code is now totally fine - you can have whatever operands you'd like on either side of the <= operator.
Yes you can have constants as the left hand argument of a logical check in C.
However the pseudocode you have listed would need to be split into two expressions:
if ((-1 <= X) && (X <= 8))
Side Note:
Many developers prefer the "constant on the left" style of logical statement because it will cause a compilation error in certain error-prone comparisons. For example:
Let's say you wanted to evaluate if (X == 3) but accidentally typed if (X = 3).
The latter is a perfectly valid C expression because the assignment operation returns True.
If you were to use the "constant on the left" style:
if (3 = X) would cause a compilation error, thus save a lot of time.