Operator ++ used in java for the cycle for() - java

i have a question because i don't find any document that can confirm this:
i am taking an example of ++ operator but it can be the same for the other.
first for-loop: LOOP
for(int i=0 ; i < 5; ) {
i = i++;
System.out.println("Hello World");
}
second for-loop: 5 iteration
for(int i=0 ; i < 5; i++) {
i = i++;
System.out.println("Hello World");
}
My question is :
the operator ++ is acting in one way when we asign it to other variable ???
and it act in another way when we use it inside a for-loop ?
Rules to excute:
for(int i=0; i < 5 ; i++) {
//some code here to excetute
}
order of execution:
int i= 0
i < 5
//execute the code in the body
operator of update ( i++)
Thanks to all.

Here's an example:
int x = 5;
int y = x++;
The result at this point is y = 5 and x = 6.
Why?
x++ is a post-increment operator: x itself is incremented, but the value of the expression (think return value of sorts) is the original value.
In contrast, ++x is a pre-increment operator: x itself is incremented and the value of the expression is the resulting value. Hence if you do y = ++x instead, y and x will result in the same value.

i just found the response in this article:
http://introcs.cs.princeton.edu/java/11precedence/
for(int i=0; i<5;)
i = i++;
}
it is a post-increment operator so the Associativity is Left-To-Right.
i++ in it's left has an the operator = .
Cause of Associativity ( and not the level of Precedence ) i assign the value of the i in the right to the variable i in the left. After doing this i do the increment but it doest have a reference variable whent o go for the moment so it remain in the memory for the next row of code that will call the i variable.
Precedence of operator:
hightl level of precedence is ++
lower level of precedence is =
Associativity of the operator:
Left to Right for the operator ++
Right to left for the operator =
So:
java will read all the line with the text till when java will found the semicolon (;). After finding a semicolon (;) java will call this a statement and will go for first to understand the operator and the variable that has inside.
Java see that it has 2 operators , one is ++ and the other one is =.
Cause of precedence the operator ++ will decide the associativity.
So the operator ++ has the associativity from Left to Right.
So:
i++ (Left to Right) ----------------> (execute everything before i for first)
++1 ( Right to Left ) <--------------- (execute everything after i for first)
i++ will calculate first what will be in the left and later it will go in the right.
i++ will execute everything in the left first (.. i)++ till i
found another operator with hight precedenze that will change the
direction of the flow cause of level precedence.
The next operator is = and it is with lower level of precedence so i
will execute what the operator ++ was saying. For this reason i
valutate ( i = i ).
After assign the ( i = i ) i dont have other operator and i will
turn back the controlo to the operator ++ so i will execute the
remain. For this reason i increment of 1 value the i variable.

Related

JAVA program always gives the wrong output for the first iteration and then works correctly [duplicate]

I was going through some exercises but I am confused in this one:
public static int f (int x, int y) {
int b=y--;
while (b>0) {
if (x%2!=0) {
--x;
y=y-2;
}
else {
x=x/2;
b=b-x-1;
}
}
return x+y;
}
What is the purpose of b=y--?
So, for example, x=5 and y=5
when we first go inside of while loop (while (b>0)) will b = 4 or 5? When I am running the code in my computer b is 5. And the return is 3. It is really unclear to me. Sorry if I am unclear in my question.
int b=y--; first assignes b=y and then decrements y (y--).
Also take a look at the prefix/postfix unary increment operator.
This example (taken from the linked page) demonstrates it:
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
// prints 4
System.out.println(i);
++i;
// prints 5
System.out.println(i);
// prints 6
System.out.println(++i);
// prints 6
System.out.println(i++);
// prints 7
System.out.println(i);
}
}
The difference between a post-increment/decrement and a pre-increment/decrement is in the evaluation of the expression.
The pre-increment and pre-decrement operators increment (or decrement) their operand by 1, and the value of the expression is the resulting incremented (or decremented) value. In contrast, the post-increment and post-decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand's original value prior to the increment (or decrement) operation.
In other words:
int a = 5;
int b;
b = --a; // the value of the expression --a is a-1. b is now 4, as is a.
b = a--; // the value of the expression a-- is a. b is still 4, but a is 3.
Remember that a program must evaluate expressions to do everything. Everything is an expression, even just a casual mention of a variable. All of the following are expressions:
a
a-1
--a && ++a
System.out.println(a)
Of course, in the evaluation of expressions, operator precedence dictates the value of an expression just as the PEMDAS you learned in grade school. Some operators, such as increment/decrement, have side effects, which is of course great fun, and one of the reasons why functional programming was created.
I believe b would equal 5 entering the loop because
b=y--;
When the "--" is behind the variable it decrements it after the action.
It's poor coding, as it can confuse new programmers.
The function, assuming it is passing by value, like in the example above (as opposed to passing by reference) takes a copy of y, decrements it, and assigns it to b. It does not alter the argument passed to the function when it was called.
Post increment
x++;
x += 1;
Post decrement
x--;
x -=1;
Pre increment : ++x;
Pre decrement : --x;
According to the Head First Java:
Difference between x++ and ++x :
int x = 0; int z = ++x;
Produces: x is 1, x is 1
in x = 0; int z = x++;
Produces: x is 1, z is 0

x = x++ doesn't increment because the ++ is applied after the assignment?

From page 280 of OCP Java SE 6 Programmer Practice Exams, question 9:
int x = 3;
x = x++;
// x is still 3
In the explanation we can read that:
The x = x++; line doesn't leave x == 4 because the ++ is applied
after the assignment has occurred.
I agree that x is 3, I understand post-incremenation.
I don't agree with the explanation. I would replace "after" with "before".
I thought it works like this:
x is 3.
x++ is executed. I see this post-increment operator as a function:
int operator++() {
int temp = getX();
setX(temp + 1);
return temp;
}
So, after x++ execution, x is 4, but the value returned from x++ expression is 3.
Now, the assignment =. Simply write returned 3 to x.
So, in my eyes ++ is applied before the assignment has occurred. Am I wrong?
...the ++ is applied after the assignment has occurred.
Okay, hold on. This is actually confusing and perhaps suggests an incorrect behavior.
You have the expression†:
x = ( x++ )
What happens is (JLS 15.26.1):
The expression on the left-hand side of the assignment x is evaluated (to produce a variable).
The expression on the right-hand side of the assignment ( x++ ) is evaluated (to produce a value).
The evaluation of the right-hand side is: x is post-incremented, and the result of the expression is the old value of x.
The variable on the left-hand side, x, is assigned the value produced by the evaluation of the right-hand side, which is the old value of x.
So the post-increment happens before the assignment.
(Therefore, as we know, the value of x after executing the statement x = x++; is the same as the value of x before executing the statement, which is 3.)
So, in my eyes ++ is applied before the assignment has occurred. Am I wrong?
You are right.
Technically, the way it is specified is that x++ is evaluated before its result is stored and during the evaluation of the assignment operator. So x++ can be interpreted as happening either before or during the assignment. Not after, so the book appears to be wrong either way.
Just for the heck of it, we may also look at some bytecode:
/* int x = 3; */
iconst_3 // push the constant 3 on to the stack : temp = 3
istore_0 // pop the stack and store in local variable 0 : x = temp
/* x = x++; */
iload_0 // push local variable 0 on to the stack : temp = x
iinc 0 1 // increment local variable 0 by 1 : x = x + 1
istore_0 // pop the stack and store in local variable 0 : x = temp
The iinc happens before the istore.
†: The parentheses have no effect on the evaluation, they are just there for clarity.
Reaching over to the official spec:
postfix ++ section: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.14.2
assignment operator section: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26.1
In terms of assignment, this is simple assignment, so we hit case 3:
First, the left-hand operand is evaluated to produce a variable -- x
no errors -> the right-hand operand is evaluated -- x++
"the value of the right-hand operand is converted to the type of the left-hand variable, is subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable"
So in step 2, the postfix operator should get resolved, and then in step 3, x gets assigned its original value again:
call: x = x++;
interframe: LHS is variable 'x'
interframe: RHS caches return value as 3
interframe: x is incremented to 4
interframe: RHS cached value '3' is returned for assignment
interframe: variable x is assigned value 3
call result: x = 3
So I think you're right in that the book is wrong, and it might be worth contacting the authors or publisher to have a correction published (or added to an errata page, etc)
x++ means "use x and then increment x."
You can also use ++x, which means "increment x and then use x."
More easily, however, you can simply do
x += 1;
++x is called preincrement while x++ is called postincrement.
int x = 3;
x = ++x;
exemple
int x = 5, y = 5;
System.out.println(++x); // outputs 6
System.out.println(x); // outputs 6
System.out.println(y++); // outputs 5
System.out.println(y); // outputs 6

Rotate Squared Matrix java solution: Why it works?

I want to solve the following problem: You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise) in-place. I found one very nice solution here: http://n00tc0d3r.blogspot.de/2013/05/rotate-image.html Namely,
public void rotate(int[][] matrix) {
for (int level = 0, len = matrix.length; level < len; ++level, --len) {
int end = len - 1;
for (int pos = level; pos < end; ++pos) {
int tail = matrix.length - pos - 1;
int tmp = matrix[level][pos];
// left -> top
matrix[level][pos] = matrix[tail][level];
// bottom -> left
matrix[tail][level] = matrix[end][tail];
// right -> bottom
matrix[end][tail] = matrix[pos][end];
// top -> right
matrix[pos][end] = tmp;
}
}
}
I see that it works, and I understand the idea, but my question is why we use ++level instead of level++ and why we use --len instead of len--. I tried both was and it worked fine for me. However for such problems (non trivial matrix traversal) people always use ++level instead of level++. Again my question is why?
Consider this :
i = 0;
System.out.println(i++); // postfix increment
This prints 0, but i is updated to 1 as you expect.
i = 0;
System.out.println(++i); // prefix increment
This prints 1.
From Java Langauge Specification,
The value of the postfix increment expression is the value of the variable before the new value is stored.
And
The value of the prefix increment expression is the value of the variable after the new value is stored.
In your case of a for loop, it does not make a difference on which one you use, but it's really a matter of choice and habit.
There is no difference between using ++level or level++ / --len or len-- in an increment/decrement part of the for loop. The len or level values are not used in that section of the loop. so, the values are going to be very same after the execution of that part of the for loop. It is just programmer's decision. That's all
++level is evaluated after the increment and level++ before. The difference is subtle.
As the values of ++level and ++len aren't used in any expressions it's just a matter of style.
It does not have any effect in this "for" loop, but the prefix operator is applied before evaluation of the expression it is used within and the suffix operator after evaluation.
An example where it would make a difference is
while (i++ < 5)
{
foo(bar);
}
or
while (++i < 5)
{
foo(bar);
}
(The former one will go through one additional iteration compared to the latter one since it increments after evaluation of the comparison)

Why can't I use ?: operators in the 3rd argument of for loops in Java?

Why is the following code giving me an error?
int n = 30000; // Some number
for (int i = 0;
0 <= n ? (i < n) : (i > n);
0 <= n ? (i++) : (i--)) { // ## Error "not a statement" ##
f(i,n);
}
It's because the for loop has been defined that way in the Java Language Specification.
14.14.1 The basic for statement
BasicForStatement:
for ( ForInit ; Expression ; ForUpdate ) Statement
ForStatementNoShortIf:
for ( ForInit ; Expression ; ForUpdate ) StatementNoShortIf
ForInit:
StatementExpressionList
LocalVariableDeclaration
ForUpdate:
StatementExpressionList
StatementExpressionList:
StatementExpression
StatementExpressionList , StatementExpression
So it needs to be a StatementExpression or multiple StatementExpressions, and StatementExpression is defined as:
14.8 Expression statements
StatementExpression:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
0 <= n ? (i++) : (i--) is none of those, so it is not accepted. i += ((0 <= n) ? 1 : -1) is an assignment, so it works.
First of all, I would recommend against writing the code this way. The purpose of the code is "count up from zero to n if n is positive, count down from 0 to n if n is negative", but I would be inclined to instead write:
for (int i = 0; i < abs(n); i += 1)
{
int argument = n < 0 ? -i : i;
f(argument, n);
}
But that does not answer your question, which is:
Why can't I use ?: operators in the 3rd argument of for loops in Java?
A for loop has the structure for ( initialization ; condition ; action ).
The purpose of an expression is to compute a value.
The purpose of a statement is to take an action.
There are some expressions which by design both compute a value and take an action. i++, i += j, new foo(), method() and so on.
It is bad style to have any other expression that both computes a value and takes an action. Such expressions are difficult to reason about.
Therefore the action of the for loop is restricted to be only those expressions which by design both compute a value and take an action.
Basically, by forbidding this code the compiler is telling you that you've made a bad stylistic choice. b?i++:i-- is a legal expression but it is really bad style because it makes what is supposed to be computing a value into producing a side effect and ignoring the value.
replace
0 <= n ? (i++) : (i--)
with
i += ((0 <= n) ? 1 : -1)
that should work
Your code is giving you an error mostly because you're trying to solve your problem with invalid algorithm. The fact that JLS doesn't allow ternary as a condition in for loop doesn't help either - but the main problem is that you miss the valid solution of the task at hand.
Let's start with a common statement, prematureOptimization == sqrt(sum(evil)) - first you should consider what you want to do, not how to do it or why the code doesn't work.
the loop should just execute n times, using i as a counter
i step should be 1 if n is >= 0, otherwise -1
(side note: if n is invariant (and it is here) using e.g. abs(n) or n < 0 in the condition is a bad practice; although most compiler will try to factor the invariant out of the loop, you should usually simply use a temporary var to store the result and use the result in the comparison instead)
So, the code at hand should be:
void doSomething( int n ) {
if ( n >= 0 )
for( int i = 0; i < n; i++ )
f( i, n );
else
for( int i = 0; i > n; i-- )
f( i, n );
}
Factoring out invariants and separating distinct code branches are basic techniques used to increase algorithms efficiency (not a premature optimization, mind me); there's no faster nor more clean way to do this. Some may argue this is a case of loop unwinding - it very well would be, if not for the fact that those two loops shouldn't be wound together in the first place...
Another thing: third op in for loop was always an ordinary statement; let's try to guess why doesn't the following code compile?
0 <= n ? (i++) : (i--); // error: not a statement
... maybe because following code won't compile either?
0 <= n ? i : i; // error: not a statement
... and that's for the very same reason code below won't work in Java either?
i; // error: not a statement
Your answer is: ternary is not a statement - ternary just returns the value, and value is not a statement (at least in Java); i++ and i-- are allowed in ternary just because they return a value, but they also produce side effects here.

java for loop pre-increment vs post-increment [duplicate]

Why does this
int x = 2;
for (int y =2; y>0;y--){
System.out.println(x + " "+ y + " ");
x++;
}
prints the same as this?
int x = 2;
for (int y =2; y>0;--y){
System.out.println(x + " "+ y + " ");
x++;
}
As far, as I understand a post-increment is first used "as it is" then incremented. Are pre-increment is first added and then used. Why this doesn't apply to the body of a for loop?
The loop is equivalent to:
int x = 2;
{
int y = 2;
while (y > 0)
{
System.out.println(x + " "+ y + " ");
x++;
y--; // or --y;
}
}
As you can see from reading that code, it doesn't matter whether you use the post or pre decrement operator in the third section of the for loop.
More generally, any for loop of the form:
for (ForInit ; Expression ; ForUpdate)
forLoopBody();
is exactly equivalent to the while loop:
{
ForInit;
while (Expression) {
forLoopBody();
ForUpdate;
}
}
The for loop is more compact, and thus easier to parse for such a common idiom.
To visualize these things, expand the for loop to a while loop:
for (int i = 0; i < 5; ++i) {
do_stuff(i);
}
Expands to:
int i = 0;
while (i < 5) {
do_stuff(i);
++i;
}
Whether you do post-increment or pre-increment on the loop counter doesn't matter, because the result of the increment expression (either the value before or after the increment) isn't used within the same statement.
There's no difference in terms of performance, if that's your concern. It can only be used wrongly (and thus sensitive to errors) when you use it during the increment.
Consider:
for (int i = 0; i < 3;)
System.out.print(++i + ".."); //prints 1..2..3
for (int i = 0; i < 3;)
System.out.print(i++ + ".."); //prints 0..1..2
or
for (int i = 0; i++ < 3;)
System.out.print(i + ".."); //prints 1..2..3
for (int i = 0; ++i < 3;)
System.out.print(i + ".."); //prints 1..2
Interesting detail is however that the normal idiom is to use i++ in the increment expression of the for statement and that the Java compiler will compile it as if ++i is used.
++i and i++ makes a difference when used in combination with the assignment operator such as int num = i++ and int num = ++i or other expressions.
In above FOR loop, there is only incrementing condition since it is not used in combination with any other expression, it does not make any difference. In this case it will only mean i = i + 1.
This loop is the same as this while loop:
int i = 0;
while(i < 5)
{
// LOOP
i++; // Or ++i
}
So yes, it has to be the same.
Because that statement is just on it's own. The order of the increment doesn't matter there.
Those two cases are equivalent because the value of i is compared after the increment statement is done. However, if you did
if (i++ < 3)
versus
if (++i < 3)
you'd have to worry about the order of things.
And if you did
i = ++i + i++;
then you're just nuts.
Because nothing in your examples is using the value returned from the pre- or post-increments. Try wrapping a System.out.println() around the ++x and x++ to see the difference.
From the Java Language Specification chapter on for loops:
BasicForStatement:
for ( ForInit ; Expression ; ForUpdate ) Statement
... if the ForUpdate part is present,
the expressions are evaluated in
sequence from left to right; their
values, if any, are discarded. ...
If the ForUpdate part is not present,
no action is taken.
(highlight is mine).
The output is the same because the 'increment' item in the 'for (initial; comparison; increment)' doesn't use the result of the statement, it just relies on the side-effect of the statement, which in this case is incrementing 'i', which is the same in both cases.
Because the value of y is calculated in for statement and the value of x is calculated in its own line, but in the System.out.println they are only referenced.
If you decremented inside System.out.println, you would get different result.
System.out.println(y--);
System.out.println(--y);
The check is done before the increment argument is evaluated. The 'increment' operation is done at the end of the loop, even though it's declared at the beginning.
Try this example:
int i = 6;
System.out.println(i++);
System.out.println(i);
i = 10;
System.out.println(++i);
System.out.println(i);
You should be able to work out what it does from this.
There are a lot of good answers here, but in case this helps:
Think of y-- and --y as expressions with side effects, or a statement followed by an expression. y-- is like this (think of these examples as pseudo-assembly):
decrement y
return y
and --y does this:
store y into t
decrement y
load t
return t
In your loop example, you are throwing away the returned value either way, and relying on the side effect only (the loop check happens AFTER the decrement statement is executed; it does not receive/check the value returned by the decrement).
If the for loop used the result of the expression i++ or ++i for something, then it would be true, but that's not the case, it's there just because its side effect.
That's why you can also put a void method there, not just a numeric expression.
The increment is executed as an independent statement. So
y--;
and
--y;
are equivalent to each other, and both equivalent to
y = y - 1;
Because this:
int x = 2;
for (int y =2; y>0; y--){
System.out.println(x + " "+ y + " ");
x++;
}
Effectively gets translated by the compiler to this:
int x = 2;
int y = 2
while (y > 0){
System.out.println(x + " "+ y + " ");
x++;
y--;
}
As you see, using y-- or --y doesn't result in any difference. It would make a difference if you wrote your loop like this, though:
int x = 2;
for (int y = 3; --y > 0;){
System.out.println(x + " "+ y + " ");
x++;
}
This would yield the same result as your two variants of the loop, but changing from --y to y-- here would break your program.
It's a matter of taste. They do the same things.
If you look at code of java classes you'll see there for-loops with post-increment.
in your case, it's the same, no difference at all.
You are right. The difference can be seen in this case:
for(int i = 0; i < 5; )
{
System.out.println("i is : " + ++i);
}
Yes it does it sequentially. Intialisation, then evaluation condition and if true then executing the body and then incrementing.
Prefix and Postfix difference will be noticable only when you do an Assignment operation with the Increment/Decrement.
There is no differences because every part of the for "arguments" are separated statements.
And an interesting thing is that the compiler can decide to replace simple post-incrementations by pre-incrementations and this won't change a thing to the code.
They DON'T behave the same. The construct with i++ is slightly slower than the one with ++i because the former involves returning both the old and the new values of i. On the other side, the latter only returns the old value of i.
Then, probably the compiler does a little magic and changes any isolated i++ into a ++i for performance reasons, but in terms of raw algorithm they are not strictly the same.
There's many similar posts at Stackoverflow:
Difference between i++ and ++i in a loop?
Is there any performance difference between ++i and i++ in C#?
Is there a performance difference between i++ and ++i in C?
However, it seems your question is more generic because it's not specific to any language or compiler. Most of the above questions deal with a specific language/compiler.
Here's a rundown:
if we are talking about C/C++/Java (probably C# too) and a modern compiler:
if i is an integer (const int, int, etc.):
then the compiler will basically replace i++ with ++i, because they are semantically identical and so it doesn't change the output. this can be verified by checking the generated code / bytecode (for Java, I use the jclasslib bytecode viewer).
else:
else:
all bets are off, because the compiler cannot guarantee that they are semantically identical, so it does not try to optimize.
So if you have a class in C++ that overrides the postfix and prefix operators (like std::iterator), this optimization is rarely, if ever, done.
In summary:
Mean what you say, and say what you mean. For the increment part of for loops, you almost always want the prefix version (i.e., ++i).
The compiler switcheroo between ++i and i++ can't always be done, but it'll try to do it for you if it can.
in a loop, first initialization, then condition checking, then execution, after that increment/decrement. so pre/post increment/decrement does not affect the program code.
About i++ (post-incrementation) vs. ++i (pre-incrementation) #me: "In both cases, the expression gets evaluated, and the result is used to check against the condition. In the pre-increment case, the increment expression increments the variable and returns the resulting value. For post-increment, the increment expression also increments the variable, but it returns the previous value. As a result, pre-increment compares against the incremented value, whereas post-increment compares against the original value; in both cases, the variable has been incremented when the condition is checked." – tdammers
There is quite confusion in between post and pre increment operator, this can be easily understand from this excerpt of "Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne"
Increment/decrement operators: i++ is the same as i = i + 1 and has the value
i in an expression. Similarly, i-- is the same as i = i - 1. The code ++i and
--i are the same except that the expression value is taken after the increment/
decrement, not before.
for example
x = 0;
post increment:
x++;
step 1:
assign the old value (0) value of the x back to x.So, here is x = 0.
step 2:
after assigning the old value of the x, increase the value of x by 1. So,
x = 1 now;
when try to print somthing like:
System.out.print(x++);
the result is x : 0. Because only step one is executed which is assigning
old value of the x back and then print it.
But when, we do operation like this:
i++;
System.out.print(i);
the result is x: 1. which is because of executing Step one at first
statement and then step two at the second statement before printing the
value.
pre increment:
++x;
step 1:
increase the value of x by 1. So, x = 1 now;
step 2:
assign the increased value back to x.
when try to print something like:
System.out.print(++1)
the result is x : 1. Because the value of the x is raised by 1 and then
printed. So, both steps are performed before print x value. Similarly,
executing
++i;
system.out.print(i);
Both steps are executed at statement one. At second statement, just the
value of "i" is printed.

Categories