All combinations/cases of equality of 3 variables

186 Views Asked by At

I have three integer variables like:

c0, c1, c2

I don't have the focus and have a difficult time determining all the possible cases of their equality:


if c1 == c0 && c2 == c0 {
    // ...
} else if c1 != c0 && c2 != c0 && c1 != c2 {
    // ...
} else if c1 != c0 && c2 != c0 && c1 == c2 {
    // ...
} else if c1 == c0 && c2 != c0 {
    // ...
} else if ... ?

Can anybody help?

2

There are 2 best solutions below

0
On BEST ANSWER

They are either all equal (one possibility), or two are equal and one isn't (three possibilities), or none are equal (one possibility). This gives us five unique possibilities in total:

if c0 == c1 && c1 == c2 {
    // ...
} else if c0 == c1 && c1 != c2 {
    // ...
} else if c1 == c2 && c0 != c1 {
    // ...
} else if c0 == c2 && c1 != c2 {
    // ...
} 

And now use else to account for the last possibility of none of them being equal:

else {
    // ...
}
5
On
  1. All three are equal, that is, $c_0 = c_1 = c_2$

  2. Only two of them are equal, that is, $c_i = c_j \neq c_k$ where $\{i,j,k\} = \{0,1,2\}$. This case has three possibilities without repetition, since when you choose $k$, the pair $(i,j)$ and $(j,i)$ corresponds to the same thing.

  3. None of them are equal to one another.

You can program it as follows :

if c0 == c1 && c1 == c2 : 
  //
elif c0 == c1 && c1 != c2 : 
  // ..   
elif c1 == c2 && c2 != c3 : 
  // ..
elif c2 == c3 && c3 != c1 : 
  // ..    
else : 
  // ..

(the else part corresponds to the last possibility, when $c0 \ != c1 \land c1 \ != c2 \land c2 \ != c3$)