cornel jew

Ranch Hand

Posts: 37

posted 14 years ago

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

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

Thomas Paul

mister krabs

Ranch Hand

Ranch Hand

Posts: 13974

posted 14 years ago

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.

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.

Associate Instructor - Hofstra University

Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog

Blake Minghelli

Ranch Hand

Posts: 331

posted 14 years ago

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.

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.

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

Hope that helps.

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

Posts: 37

posted 14 years ago

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

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

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |