For example, i++
can be written as i=i+1
.
Similarly, how can ++i
be written in the form as shown above for i++
?
Is it the same way as of i++
or if not please specify the right way to write ++i
in the form as shown above for i++
?
For example, i++
can be written as i=i+1
.
Similarly, how can ++i
be written in the form as shown above for i++
?
Is it the same way as of i++
or if not please specify the right way to write ++i
in the form as shown above for i++
?
You actually have it the wrong way around:
i=i+1
is closer to ++i
than to i++
The reason for this is that it anyway only makes a difference with surrounding expressions, like:
j=++i
gives j the same value as j=(i+=1)
and j=(i=i+1)
There's a whole lot more to the story though:
1) To be on the same ground, let's look at the difference of pre-increment and post-increment:
j=i++
vs j=++i
The first one is post-increment (value is returned and "post"=afterwards incremented), the later one is pre-increment (value is "pre"=before incremented, and then returned)
So, (multi statement) equivalents would be:
j=i; i=i+1;
and i=i+1; j=i;
respectively
This makes the whole thing very interesting with (theoretical) expressions like j=++i++
(which are illegal in standard C though, as you may not change one variable in a single statement multiple times)
It's also interesting with regards to memory fences, as modern processors can do so called out of order execution, which means, you might code in a specific order, and it might be executed in a totally different order (though, there are certain rules to this of course). Compilers may also reorder your code, so the 2 statements actually end up being the same at the end.
-
2) Depending on your compiler, the expressions will most likely be really the same after compilation/optimization etc.
If you have a standalone expression of i++;
and ++i;
the compiler will most likely transform it to the intermediate 'ADD' asm command. You can try it yourself with most compilers, e.g. with gcc it's -s
to get the intermediate asm output.
Also, for many years now, compilers tend to optimize the very common construct of
for (int i = 0; i < whatever; i++)
as directly translating i++
in this case would spoil an additional register (and possible lead to register spilling) and lead to unneeded instructions (we're talking about stuff here, which is so minor, that it really won't matter unless the loop runs trillion of times with a single asm instruction or the like)
-
3) Back to your original question, the whole thing is a precedence question, as the 2 statements can be expressed as (this time a bit more exact than the upper explanation):
something = i++
=> temp = i; i = i + 1; something = temp
and
something = ++i
=> i = i + 1; something = i
( here you can also see why the first variant would theoretically lead to more register spilled and more instructions )
As you can see here, the first expression can not easily be altered in a way I think would satisfy your question, as it's not possible to express it using precedence symbols, e.g. parentheses, or, simpler to understand, a block). For the second one though, that's easy:
++i
=> (++i)
=> { i = i + 1; return i }
(pseudo code)
for the first one that would be
i++
=> { return i; i = i + 1 }
(pseudo code again)
As you can see, this won't work.
Hope I helped you clear up your question, if anything may need clarification or I made an error, feel free to point it out.
Technically j = ++i
is the same as
j = (i = i + 1);
and j = i++;
is the same as
j = i;
i = i + 1;
You can avoid writing this as two lines with this trick, though ++ doesn't do this.
j = (i = i + 1) - 1;
++i
and i++
both have identical results if written as a stand alone statement (as opposed to chaining it with other operations.
That result is also identical to i += 1
and to i = i + 1
.
The difference only comes in if you start using the ++i
and i++
inside a larger expression.
The real meaning of this pre and post increment, you 'll know only about where you are using that.?
Main difference is
pre condition will increment first before execute any statement. Post condition will increment after the statement executed.
Here I've mention a simple example to you.
void main()
{
int i=0, value;
value=i++; // Here I've used post increment. Here value of i will be assigned first then It'll be incremented.
// After this statement, now Value will hold the value 0 and i will hold the value 1
// Now I'm going to use pre increment
value=++i; // Here i've used pre increment. So i will be incremented first then value will be assigned to value.
// After this statement, Now value will hold the value 2 and i will hold the value 2
}
This may be done using anonymous methods:
int i = 5;
int j = i++;
is equivalent to:
int i = 5;
int j = new Func<int>(() => { int temp = i; i = i + 1; return temp; })();
However, it would make more sense if it were expanded as a named method with a ref
parameter (which mimics the underlying implementation of the i++
operator):
static void Main(string[] args)
{
int i = 5;
int j = PostIncrement(ref i);
}
static int PostIncrement(ref int x)
{
int temp = x;
x = x + 1;
return temp;
}
There is no 'equivalent' for i++
.
In i++
the value for i
is incremented after the surrounding expression has been evaluated.
In ++i
and i+1
the value for i
is incremented before the surrounding expression is evaluated.
++i will increment the value of 'i', and then return the incremented value. Example:
int i=1;
System.out.print(++i);
//2
System.out.print(i);
//2
i++ will increment the value of 'i', but return the original value that 'i' held before being incremented.
int i=1;
System.out.print(i++);
//1
System.out.print(i);
//2