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?
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.
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.
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)
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.
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
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.
Related
b = 5;
loopiterations = 0;
while (b-- > 0) { // Use a postfix decrement
loopiterations++;
}
System.out.println("Postfix decrement operator used, loopiterations = " +
loopiterations + ", b = " + b);
The result is
Postfix decrement operator used, loopiterations = 5, b = -1
I don't understand why the value of b is -1. The value of b starts from 5 (after while loop it's value is 4) and then ends at 1 ( after the while loop it is 0) and then the iteration from 5 to 1 is 5.If how I think is right, why is the value of b is -1 after looping. Thank you.
This block of code
while (b-- > 0) {
// do something
}
is semantically the same as
while (b > 0) {
b--;
// do something
}
b--;
The loop while (b-- > 0) exits as soon as b == 0, but because of the post decrement operation b will be one last time decrement and hence b will have the value of -1 when printing.
**postfix increment or decrement actually happens after using value**, the initial value of b is 5 so in the first iteration 5 is checked for condition after the last iteration, b becomes 0 after its value (1 at time of comparison) has been decremented, then 0 is checked but the condition is false but due to postfix decrement it's value becomes -1.
postfix -> first use value then decrement or increment that's how it works
This is because when b == 0, the postfix will return false and your while loop will terminate. However, the postifx will still subtract one from b. Therefore, the loop is executed 5 times, and iterations = 5. However, b is decremented 6 times.
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t || ((i++) == 0));
b = (f || ((i+=2) > 0));
System.out.println(i);
After the above code is executed, the print result is 2, not 3, why?
I find "i" was 0 not 1 after "b = (t || ((i++) == 0))" executed by debuging.Well,I'm confused why "i++" not changes "i".
Well,I'm confused why "i++" not changes "i".
Because i++ doesn't execute in the code you've provided.
In an expression of the form a || b, first a is evaluated, and if it's true (which it is in this case), the expression b isn't evaluated. This is known as short-circuiting.
This is described in JLS section 15.24.
If you change the code to use the non-short-circuited | operator instead, like this:
b = (t | ((i++) == 0));
... then it will evaluate both operands regardless.
I was taking a java course on edx. For this question, the condition is x++==4. I tried and found out that only when i enter an x value of 4, the condition evaluates to true. But why?
1) Why is it that when i enter x = 3 or x = 2 the condition evaluates to false?
2) Why is x++==4 even a condition? it's like x = x + 4. How can it ever be true or false? It is an equation, not like x > 3 etc.
if (x++==4) { ... }
could simply be replace by the following lines
boolean b = (x == 4);
x = x + 1;
if (b) { ... }
You evaluate if x is equals to 4 and then increment it using the Postfix increment Operator ++
Why is it that when i enter x = 3 or x = 2 the condition evaluates to
false?
Because
3 == 4; // false
2 == 4; // false
It is because x==4 IS a condition? therfore x++ is just an increment on the value if the variable
Dont let the ++ to trick your mind...
the same can happen even on ugliest ways like
if (x---5==0) { which just happens to be
if ((x--)-5==0) {
int i = 0;
boolean b = true;
System.out.println(b && !(i++ > 0))
When I compile the above code I get a value true back.
But how can that be, since the second part of the argument (since b is true already) basically translates to
(0 + 1 > 0) => (1 > 0)
which should return true. Then the statement would be true && false, which is false.
What am I missing?
Java behaving correctly :)
i++
That is postfix increment.
It generated result and then incremented that value later.
!(i++ > 0) // now value is still zero
i++ will use the previous value of i and then it will increment it.
When you use ++ ,it's like
temp=i;
i += 1;
i=temp; // here old value of i.
language specification on Postfix Increment Operator ++
the value 1 is added to the value of the variable and the sum is stored back into the variable. ......
The value of the postfix increment expression is the value of the variable before the new value is stored.
Possible solution would be ++i, which is as per your requirment,
Prefix Increment Operator ++
The value of the prefix increment expression is the value of the variable after the new value is stored.
You want to use ++i if you want to increment i and return the incremented value. i++ returns the non incremented value.
b && !(i++ > 0)
i++ is post increment so value of i here is still 0
0>0 false
b && 1 is true(since !(0) is 1)
So you are getting true.
i++
the increment happens after the line is executed
so you better keep
++i
You can see how ++ operator works on following example:
public static void main(String[] argv) {
int zero = 0;
System.out.println(zero++);
zero = 0;
System.out.println(++zero);
}
Result is:
0
1
public class AndOperator {
public static void main(String[] arg) {
int value = 8;
int count = 10;
int limit = 11;
if (++value % 2 == 0 && ++count < limit) {
System.out.println("here");
System.out.println(value);
System.out.println(count);
} else{
System.out.println("there");
System.out.println(value);
System.out.println(count);
}
}
}
i am getting output as
there
9
10
explain how count is 10....?
&& is short-circuit operator. It will only evaluate the 2nd expression if the 1st expression evaluates to true.
Since ++value % 2 == 0 is false, hence it doesn't evaluate the 2nd expression, and thus doesn't increment count.
++value = 9 so ++value % 2 == 0 is false so ++count < limit is not evaluated.
This is called Short circuit evaluation. See the wikipedia page : http://en.wikipedia.org/wiki/Short-circuit_evaluation
Since you are using &&(logical and) operator.
logical and evaluate second condition only if first condition is evaluated to true
Here in your code first condition ++value % 2 == 0 is evaluated to false,so second condition ++count < limit won't be evaluated.
If you want to execute ++count < limit also use &.
more information read Difference between & and &&
Because ++value % 2 == 0 is false, thus it won't return the first statement. The reason ++value % 2 is false is because value is incremented by one before the mod operator is evaluated. So ++value % 2 is 9 % 2 which != 0.