Say I wanted to test not just one, but several variables for equivalence in an if statement:
if(x1==x2==y1==y2){
printf("Input values shouldn't be equal!");
}
But this doesn't seem to work. What other approach can do this?
Say I wanted to test not just one, but several variables for equivalence in an if statement:
if(x1==x2==y1==y2){
printf("Input values shouldn't be equal!");
}
But this doesn't seem to work. What other approach can do this?
Doesn't work because (x1 == x2) evaluates to the number 1, if true, and 0 if false, by the way. (x1 == x2 == x3) ends up evaluating as ((x1 == x2) == x3).
You'd need to write your own function to do this, I think.
if (x1 == x2 && x1 == y1 && x1 == y2)
{
printf("Input values shouldn't be equal!");
}
Your problem is the incorrect usage of the == operator. You would need to use something like:
if ((x1 == x2) && (x1 == y1) && (x1 == y2)) {
printf("Input values shouldn't be equal!");
}
if(x1==x2 && x1 == y1 && x1 == y2 &&
x2==y1 && x2 == y2 &&
y1 == y2)
{ printf("Input values shouldn't be equal!"); }
return 0;
}
if (x1 == x2 && x2 == y1 && y1 == y2) { ... }
The result of the expression a == b
will be an integer value of either 0 or 1. the ==
operator is left-associative, so the expression a == b == c
will be evaluated as (a == b) == c
; that is, the result of a == b
(0 or 1) will be compared against the value of c. So in the code below
if (a == b == c) { ... }
the expression will only evaluate to true if a == b and c == 1 or a != b and c == 0.
Here's a different approach, using a helper variable ( count_equals
) so that it's easy to do (and understand) exactly what you want.
int count_equals = 0;
if (x1 == x2) count_equals++;
if (x1 == y1) count_equals++;
if (x1 == y2) count_equals++;
if (x2 == y1) count_equals++;
if (x2 == y2) count_equals++;
if (y1 == y2) count_equals++;
if (count_equals == 0) /* all values are different */;
else if (count_equals == 6) /* all values are equal */;
else /* some values are equal */;
if they are integers you can use bitwise operations:
if ((x1 & x2 & x3 & x4) == (x1 | x2 | x3 | x4))
// all are equal
It will evaluate to true IFF they are all the same
The solutions presented here where mostly correct and easy to understand.
But I would prefer the solution provided by Sany Huttunen:
quoted:
if (x1 == x2 && x1 == y1 && x1 == y2)
{
printf("Input values shouldn't be equal!");
}
Here is the reason why:
Although it seems not to be possible in this concrete case it directed me immediately into thinking about refactoring, like in:
(a*b) + (a*c) + (a*d)
which could be simplified into
a*(b+c+d)
as mentioned it's not possible to simplify in this concrete case however, because
(a*b) + (a*c) + (a*d)
follows different mathematical rules then
(a+b) * (a+c) * (a+d)
This has something to do with commutative, associative and distributive rules if I remember correctly.
Here is another method using Boolean logic
bool all_equal(false)
all_equal = x1 == x2;
all_equal = all_equal && (x2 == x3);
all_equal = all_equal && (x3 == x4);
A good compiler can code this using conditional assembly instructions and not disrupt the instruction prefetch queue.