Author Topic:   Operators and assignments shatabdigreenhorn posted February 16, 2000 06:28 PM              This is a program I saw in Jarowski's bookclass Sub { public static void main(String ards[]) { int x = 0; boolean b1, b2, b3, b4; b1 = b2 = b3 = b4 = true; x = ( b1 | b2 & b3 ^ b4) ? x++ : --x; System.out.print(x); } } The o/p is 0(zero). Can anybody pls explain me why ? Milindunregistered posted February 16, 2000 08:12 PM            x = (b1|b2&b3^4) ? x++ : --x;The expression can be solved in the following manner:Using the following operator preceence:1> & AND2> ^ XOR3> | ORx = (true | ( true & true ) ^ true) ----- 1x = (true | (true ^ true ) ) -------------2x = (true | false) -----------------------3x = truex evaluates to true so the answer is 0.Please correct if I am wrong!Regards,Milind shatabdigreenhorn posted February 16, 2000 09:14 PM              Hi Milind,The expression x = (b1|b2&b3^b4) ? x++ : --x; meansif(b1|b2&b3^b4) x = x++;else x = --x;System.out.print(x)Now the boolean codn (b1|b2&b3^b4) returns true. So x = x++ should execute. My question is then x should be 1 instead of zero(because of x++)in the o/p. I just don't know why it is showing zero !shatabdi maha annabartender posted February 16, 2000 09:32 PM              See this line CAREFULLY.x=x++; //x = 0 which means the LHS is assigned a value 0 which is happened to be x itself. x is incremented and then again assigned to the pre-increment value. shatabdigreenhorn posted February 16, 2000 09:57 PM              Thanks Maha Anna.
 |