Hi all Why is the following code printing 1 int i = 0; boolean t = true; boolean f = false, b; b = (t && ((i++) == 0)); b = (f && ((i+=2) > 0)); System.out.println(i); this one 2 int i = 0; boolean t = true; boolean f = false, b; b = (t || ((i++) == 0)); b = (f || ((i+=2) > 0)); System.out.println(i); and this one 3 int i = 0; boolean t = true; boolean f = false, b; b = (t & ((i++) == 0)); b = (f & ((i+=2) > 0)); System.out.println(i); Can someone please explain it to me.I found it in a mock exam. Thanks Cornel

b = (t && ((i++) == 0)); b = (f && ((i+=2) > 0)); In the second statement, since there is no chance of the statement being true, the second half of the statement is never executed. That is what the && does - it short circuits the "and" as soon as it is impossible for it to be true. b = (t || ((i++) == 0)); b = (f || ((i+=2) > 0)); The first statement will definitely be true as soon as the first half is analyzed so there is no reason for it to look at the second half. The second statement may or may not be true after analyzing the first half so it has to continue and analyze the second half. b = (t & ((i++) == 0)); b = (f & ((i+=2) > 0)); The single & does not short circuit and always causes both halfs to be analyzed.

Originally posted by cornel jew: Hi all Why is the following code printing 1 int i = 0; boolean t = true; boolean f = false, b; b = (t && ((i++) == 0)); b = (f && ((i+=2) > 0)); System.out.println(i);

The key here is the behavior of &&. The second expression (the one to the right of the &&) is evaluated ONLY if the first expression (the one to the left of the &&) evaluates to true. So, in the first line, "i++" is evaluated because "t" is true, therefore the value of "i" is incremented to 1. In the next line, "f" is false, so the expression "i+=2" is skipped.

Originally posted by cornel jew:

this one 2 int i = 0; boolean t = true; boolean f = false, b; b = (t || ((i++) == 0)); b = (f || ((i+=2) > 0)); System.out.println(i);

The key here is the behavior of ||. The second expression (the one to the right of the ||) is evaluated ONLY if the first expression (the one to the left of the ||) evaluates to false. So, in the first line, "i++" is NOT evaluated because "t" is true, therefore the value of "i" is NOT incremented to 1. In the next line, "f" is false, so the "i+=2" gets evaluated, therfore the value of "i" becomes 2.

Originally posted by cornel jew:

and this one 3 int i = 0; boolean t = true; boolean f = false, b; b = (t & ((i++) == 0)); b = (f & ((i+=2) > 0)); System.out.println(i);

When using the "&" operator, BOTH sides get evaluated no matter what, that's why the result is 3. Hope that helps.

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"

cornel jew
Ranch Hand

Joined: Jul 13, 2002
Posts: 37

posted

0

Hi again Thanks for the wonderful forum and quick replies. After reading your post i understand the questions,test myself on the other similar ones and scores full marks. Yours Corn