2

The following code in Java:

int a = 0, b = 0, c = 0;
boolean d = (a++ > 0 && b-- < 0) || --c < 0;

results in the values:

a = 1, b = 0, c = -1 and d = true

I don't understand why a is = 1, because it is a post-increment and should also react the same way that value b does. Also, if I change the b-- to --b it still has no effect on the value of b.

What is the best way of understanding this logic?

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
shomsky
  • 43
  • 8
  • Possible duplicate of [When Java evaluates a conjunction ( && ), does it eval exp2 if exp1 is false?](https://stackoverflow.com/questions/9445145/when-java-evaluates-a-conjunction-boolean-exp1-boolean-exp2-does-it-ev) – Joe Dec 11 '17 at 09:10
  • in general: such code should be avoided in real life programming, it is overhacking. I understand, here is educational context – Jacek Cz Dec 11 '17 at 09:26
  • 1
    See also [Java short-circuit operators](https://stackoverflow.com/a/8759917/8097737) –  Dec 11 '17 at 09:27

6 Answers6

13

a++ > 0 returns false, since a++ return the previous value of a (0).

Therefore b-- < 0 is not evaluated at all, since && is a short circuiting operator. The right operand is only evaluated if the left operand is true.

--c < 0 is evaluated, since the first operand of the || operator is false, so the second operand must be evaluated.

After d is evaluated, the value of a is 1, since a was incremented. b remains 0, since b-- wasn't executed. c is -1 since --c was executed. And d is true since --c < 0 is true.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

a++ at moment of comparing is NOT >0, so b part is not evaluated, and not incremented.

Second part of && and || expressions is not evaluated if cannot change result. For && first false determines result false.

Analogically, first true determines result of || (but not here)

Jacek Cz
  • 1,872
  • 1
  • 15
  • 22
  • At the time of comparing, `a` **is** 1, but the comparison is about `a++`, which returns the **previous** value of `a` (before the increment), not the **current** value (after increment). – Mark Rotteveel Dec 11 '17 at 09:06
0

After the expression runs then a will indeed be 1 as the post increment operator has then executed.

During the evaluation of the expression a is 0 as far as the expression is concerned (but a is now 1 for other code), so the first part of the logical OR, your logical AND, is evaluated as false and the b post decrement does not get evaluated.

The second part of the logical or then runs, and the pre-decrement executes to reduce c to -1, which then causes the comparison to evaluate to true, hence d being true.

Expand the different clauses to separate variables and run through a debugger for a more interactive explanation of what's happening.

James Fry
  • 1,133
  • 1
  • 11
  • 28
0

When you write a++ OR b--, it passes the value 'a' and 'b' currently holds, and then modifies the variables.

So your expression is simply,

boolean d = (0 > 0 && 0 < 0) || -1 < 0;

Now while evaluating, (0 > 0 && 0 < 0) -> first expression returns false and you have used short-circuit AND operator. Meaning don't evaluate the right hand side if it isn't necessary. As LHS of && returns false, b-- won't be evaluated.

And we have 'd' is true. Remember,

Post-increment within expressions --> assign current value first, modify later.

Pre-increment within expressions --> modify first, assign updated value later.

Refer this link for better understanding short circuit operators: Short-Circuit Explanation

Yash Soni
  • 456
  • 3
  • 11
  • This doesn't explain the [short-circuit operators `&&` and `||`](https://stackoverflow.com/a/8759917/8097737) which causes this behavior. –  Dec 11 '17 at 09:29
  • @devpuh edited the answer. Explains the behavior now. – Yash Soni Dec 11 '17 at 11:13
0

a++ effectively means a= a+1 So, the existing value of a (=0) is used for evaluating the expression, and later on the value of a is incremented. So, a becomes 1.

Regarding why b =0 and why --b has the same effect as b--, the value of b is unchanged because of &&. Since a++ > 0 is not satisfied, the next expression b-- < 0 is not evaluated and value of b remains 0. && stops evaluation once the expression evaluates to false, while || stops evaluation once the statement evaluates to false.

-1

Please Execute This code..

int a = 0, b = 0, c = 0;
System.out.println("b = " + --b);
b=0;
System.out.println("b = " + b--);
System.out.println("a = " + a++);
a=0;
System.out.println("a = " + ++a);
System.out.println("c = " + c--);
c=0;
System.out.println("c = " + --c);
a=b=c=0;
boolean d = (a++ > 0 && --b < 0) || --c < 0;

System.out.println("d = " + d);

I think you understand full logic.

Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
  • 1
    When a person asks a question is that it does not understand, it is better a detailed answer for this answer to be useful to others (it is the goal of StackOverflow ...) – F0XS Dec 11 '17 at 09:25
  • This doesn't explain the [short-circuit operators `&&` and `||`](https://stackoverflow.com/a/8759917/8097737) which causes this behavior. –  Dec 11 '17 at 09:26