Note that there are some explanatory texts on larger screens.

plurals
  1. PODouble structural equality operators: if(a==b==c)
    primarykey
    data
    text
    <p>I wrote some code by accident today and was surprised when Eclipse did not yell at me, for once. The code had a double use of the structural equality operator (<code>==</code>) similar to the below with the <code>if(a==b==c)</code> structure.</p> <pre><code>public class tripleEqual { public static void main(String[] args) { boolean[] a = { true, false }; boolean[] b = { true, false }; boolean[] c = { true, false }; for (int aDex = 0; aDex &lt; 2; aDex++) { for (int bDex = 0; bDex &lt; 2; bDex++) { for (int cDex = 0; cDex &lt; 2; cDex++) { if (a[aDex] == b[bDex] == c[cDex]) { System.out.printf("Got a==b==c with %d %d %d\n", aDex, bDex, cDex); } } } } } } </code></pre> <p><strong>The output is</strong> </p> <pre><code>Got a==b==c with 0 0 0 Got a==b==c with 0 1 1 Got a==b==c with 1 0 1 Got a==b==c with 1 1 0 </code></pre> <p>Playing around, I notice I can't do <code>if(a==b==c)</code> with any type but <code>boolean</code>. From that the boolean expression is </p> <pre><code>( A'. B'. C') + ( A'. B . C ) + ( A . B'. C ) + ( A . B . C') </code></pre> <p>which simplifies to <code>(A=B).'C + (A&lt;&gt;B).C</code>. </p> <p>Thus, ignoring side-effect, <code>if(a==b==c)</code> is equal to <code>if(a==b &amp;&amp; !c) || (a!=b &amp;&amp; c))</code>. </p> <p>Can anyone explain how the <code>if(a==b==c)</code> syntax suggests that?</p> <p><strong>Edit 1:</strong></p> <p>I found where my confusion was after so many people explained the left-associativity. Usually I write '1' for true and '0' for false but my minimized truth table/output in the above test, I had '0' for true and '1' for false. The negation of the expression <code>( A'. B'. C') + ( A'. B . C ) + ( A . B'. C ) + ( A . B . C')</code> is <code>(A=B)=C</code>!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload