Question

I need the if statement to only execute if the 5 value in the statement are the same. e.g if a[0] = a[1] = a[2] = [a3] = [a4] = 1

This is just a matter of boolean algebra but I cant get it right.

I've tried:

if( (A[0]) && (A[1]) && (A[2]) && (A[3]) == (A[4]))
        {
            Flag =1;
        }   

and

if( (A[0]),(A[1]),(A[2]),(A[3]) == (A[4]))
        {
            Flag =1;
        }   

and

if( (A[0]) == (A[1]) == (A[2]) == (A[3]) == (A[4]))
        {
            Flag =1;
        }   

But none of these work.

Can someone please help?

Thanks!

Was it helpful?

Solution

Several people have given you the right answer; here's an explanation of why your attempts didn't work.

Why A[0] && A[1] && A[2] && A[3] == A[4] didn't work:

The expression A && B will evaluate to 0 if either A or B are zero, or to 1 if both A and B are non-zero. The && operator is left-associative, meaning expressions like A && B && C are parsed as (A && B) && C; IOW, C is AND-ed with the result of A && B (0 or 1). Thus, A[0] && A[1] && A[2] && A[3] == A[4] will only evaluate to true if all of A[0] through A[3] are non-zero and A[4] == 1, or any one of A[0] through A[3] are 0 and A[4] == 0. Also note that the && operator won't evaluate the right-hand operand if the left-hand operand is 0.

Why (A[0]),(A[1]),(A[2]),(A[3]) == (A[4]) didn't work:

The comma operator evaluates each expression in sequence, and the result is the value of the last expression, so only the value of A[3] is being compared to A[4]. Each of A[0] through A[2] is evaluated, but the result is discarded.

Why (A[0]) == (A[1]) == (A[2]) == (A[3]) == (A[4]) didn't work:

Similar to the && operator, the result of A == B will be 0 or 1. Also similar to the above, the == operator is left-associative, so A == B == C is parsed as (A == B) == C, so C is being compared to the result of A == B, which will be either 0 or 1. So the above will only evaluate to true if A[0] and A[1] are the same, and A[2] through A[4] all equal 1.

Therefore, if you want to check that multiple expressions are all equivalent, you must write something like

A == B && B == C && C == D 

OTHER TIPS

From boolean logic we have if A == B AND A == C then B == C

In C/C++ you can not combine the evaluation of an expression, so each condition has to be done individually.

So you can do it like this:

 if( (A[0] == A[1] && A[0] == A[2]  && A[0] == A[3]  && A[0]== A[4])
     flag = 1;

If this is an array you can do it in a loop as well like this (pseudocode):

 v = A[0]
 flag = 1; // Asume that all are equal.
 for(i = 0; i < maxindex; i++)
 {
      // If one entry doesn't match the whole expression is false.
      if(v != A[i])
      {
           flag = 0;
           break;
      }
}

Each comparison needs to be done individually.

if (A[0] == A[1] && 
    A[1] == A[2] &&
    A[2] == A[3] &&
    A[3] == A[4])
{
    Flag = 1;
}

Since this happens to be an array, you could take advantage of that:

const int N = 4;
bool everything_is_equal = true;

for(int i=0; i<(N-1); i++)
{
  if(A[i] != A[i+1])
  {
    everything_is_equal = false;
    break;
  }
}

Depending on the number of elements, this may or may not be more readable than a long if statement using the && operator.

I think the easiest way to do this is:

if( A[0] == A[1] && A[1] == A[2] && A[3] == A[4] && A[4] == A[5])

Other than for booleans, there is no general way to write this shorthand. You'll need to do something like:

if (A[0] == A[1] && A[1] == A[2] && ...)

or something equivalent, or encapsulate this behaviour in a helper function (something like bool areAllTheThingsEqual(T *things, int num)).

that would check if all are equal A4 which would mean all 5 are equal:

if( A[0] == A[4] && A[1] == A[4] && A[2] == A[4] && A[3] == A[4] )
{
         Flag =1;
} 

I just want to add something, you may study the operator priority first; (1) "==" and "&&" which one is first (2) May cause some compilation error (3) Notice that "==" OPERATOR will return true or false you cannot connect them together If you want to explore, you can try to use xor to solve this problem

As was stated, this is the usual way

if ( A[0] == A[1] && A[1] == A[2] && A[2] == A[3] && A[3] == A[4] )
{
    Flag = 1;
}

Your first attempt however made me think of an alternative way, using bitwise logical operations, since & and | on equal numbers gives the very same number.

But as soon as some of the numbers are different, i.e. there is some bit set to 0 in some number and 1 in another, | will yield 1 and & will yield 0 on this position in the resulting numbers, making them different.

if ( (A[0] & A[1] & A[2] & A[3] & A[4]) == (A[0] | A[1] | A[2] | A[3] | A[4]) )
{
    Flag = 1;
} 

This seems to run about twice as fast (under -O2 optimisation)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top