-2

I've started to learn java not to long ago, and recently I've come across this problem.

int i = 0;
boolean b = true;
if(b || (i++ == 1)) 
 System.out.println(i); //i is 0
System.out.println(i); // i is still 0

if(((i++) == 0) || ((i++) == 0))
 System.out.println(i); // i is now 1   
System.out.println(i); // i is 1

How come after the first if i is still zero but not after the second?

Ion
  • 11
  • 1
  • (A) include opening and closing braces in your code, and (B) please learn the difference in **i++ == 0** and **i++ = 0** – jbutler483 Mar 04 '15 at 17:30
  • Look up pre and post incrementing for the ++ operator. [See this other SO post](http://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java) Also, think about the double OR operator and it's short circuiting behaviour. – Dan Temple Mar 04 '15 at 17:30
  • 1
    `||` is short-circuiting; so in the first if condition; since `b` is `true` `i++ == 1` is not evaluated. – Alexis C. Mar 04 '15 at 17:32
  • What did you expect `||` to do? – Peter Lawrey Mar 04 '15 at 17:57

4 Answers4

2

The || conditional-OR operator is a "short-circuit" operator. According to JLS Section 15.24:

The conditional-or operator || operator is like | (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is false.

That means that if the left operand is true, then it doesn't evaluate the right operand. Logically, if one operand of an "or" operator is true, then the entire expression is true, so the evaluation of the other operand is unnecessary.

For

if(b || (i++ == 1)) 

i++ == 1 isn't evaluated, and i is still 0.

For

if((i++ == 0) || (i++ = 0))

it means that because i++ yields 0, then increments i to 1, the left side expression is still true, so i++ = 0 is not evaluated. i remains 1.

rgettman
  • 176,041
  • 30
  • 275
  • 357
2

That's because the first increment will never be executed:

if(b || (i++ == 1))

if b is true (which is) shortened execution will evaluate the expression to true, regardless of the 2nd part of the expression which will not be executed.

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
0

i++ is a post increment operator. So i++ == 1 will first check i with 1 and once that's done, post it will increment its value to +1 i.e. at this point i will be 1.

If you need value 1 at first if then use ++i == 1 which will first increment value by 1 and then will say 1 == 1 which will evaluate to true.

SMA
  • 36,381
  • 8
  • 49
  • 73
0

i think you have a problem with using this increment operations! isn't this makes an error

error: unexpected type if((i++ == 0) || (i++ = 0)) ^ required: variable found: value

and its simple right here inside if,first i is checked whether its equal to your value and then it will increased and in your case first if is running as you put a or condition so it will show zero after first condition satisfied and it won't go up to check next condition so value is still 0! and you can check this just check with if(i++==1) and you'll see the magic!

Rosh_LK
  • 680
  • 1
  • 15
  • 36