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
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.
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.
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) {
I am new to processing and I am having trouble with this. I keep getting an error message for the bolded part of the code below. Is my syntax wrong?
void block(int x, int y, int s, color tinto) {
fill(tinto);
for (int i = 0; i < 3; i++) {
triple(x, y+i*s, s, tinto);
}
if (0 < i < 3 && 6 < i < 9) { // HERE
tinto = 255;
}
else {
tinto = tinto - 200;
}
}
In Java, to check if a variable is in a range you have to divide the statement into two parts, like this:
if (0 < i && i < 3 && 6 < i && i < 9){
}
This specific code will never be true, however, because you're asking for it to be in two different ranges. Perhaps you meant to check for either range?
if (0 < i && i < 3 || 6 < i && i < 9){
}
Note the || or operator instead of the && and operator.
The syntax is not valid, and I think you're expression is wrong anyway. You say i has to be within a range AND within another. I think you mean to write that it could be between one OR the other.
Example of valid syntax: instead of 0 < i < 3, write i > 0 && i < 3.
Try this:
if ( (i > 0 && i < 3) || (i > 6 && i < 9) )
Note that the following (which is what you were trying to do apparently) will never be evaluated to true because it cannot be within both ranges.
if ( (i > 0 && i < 3) && (i > 6 && i < 9) ) // incorrect
This isn't valid java expression. Try:
if (0<i && i<3 && 6<i && i<9){
There are two different problems with this code snippet. First, you have defined the 'i' variable as in "int" inside of the for loop. This instance of 'i' is no longer defined once you exit that for loop -- so the if statement below does not refer to that instance. To overcome this, define 'i' before the for loop...
int i;
for ( i=0; i<3; I++ ) {
...
}
if ( i ...
which brings me to the second error. The syntax "0 < i < 3" is not correct. In c/c++ the operators are executed one at a time ... so in this case the first "<" operator will be evaluated as "0 < i" and the result will be a Boolean (which will always be 'true' in your particular code snippet). But the important point is that the result is a Boolean. Next the code will attempt to evaluate that result with the next part of the statement -- "true < 3", which just doesn't make any sense, and so that receives a compiler error.
In your code snippet, there is no exit to the for loop until the value of i reaches 3, so this second "if" statement is unneeded. But if you did want to test to see whether i was between 1 and 2 (inclusive), then you would have to break those up into individual tests...
if ( 0 < i && i < 3 ...
Lastly ... if the value of i is between 1 and 2 (inclusive), then it cannot also be between 7 and 8 (inclusive) .. therefore the if statement as you coded it will always be false even after we correct the syntax.
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.