Why does this
int x = 2;
for (int y =2; y>0;y--){
System.out.println(x + \" \"+ y + \" \");
x++;
}
prints the s
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.
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.
There's many similar posts at Stackoverflow:
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:
i
is an integer (const int
, int
, etc.):
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).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:
for
loops, you almost always want the prefix version (i.e., ++i
).++i
and i++
can't always be done, but it'll try to do it for you if it can.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.
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.
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.