Try it out.
j = i++;
k = i + 1;
The variable j will be the original value of i, while i will have been incremented. k will be the value of i + 1, but i will not have changed. The post-increment operator will return the original value prior to the increment. The pre-increment operator (++i) will return the incremented value of i.
In practice, i++, ++i, and i = i + 1 are generally equivalent, but it's good to know the difference between i++ and ++i to avoid subtle bugs.
2006-10-13 21:54:07
·
answer #1
·
answered by toddos1 3
·
0⤊
0⤋
1. In i++, i's value is incremented by one, whereas (i+1) does not affect the value of i.
2. Many instruction sets have more efficient increment for 1 than for general cases. Although the following is compiler dependent, typically the following happens:
i++ translates to INC A ; increment register A by 1. Efficient.
i+1 translates to ADD A,1; Same result but not so efficient.
3. As many people have pointed out the post increment nature of i++ will come to fore when an assignment happens. For e.g. if i has value 10 the expression:
t = i++; // t has 10
t = i + 1; // t has 11
2006-10-14 03:42:06
·
answer #2
·
answered by swami060 3
·
0⤊
0⤋
When working with numerical variables in C++, if we want to increase the value of a variable i by 1, we need to use either of the following two statements:
i = i + 1;
or
i++;
What each of them mean are as follows:
i = i + 1; means evaluate the value i + 1 using the current value of i and then assign that value to i.
i++; is relatively shorter to write but does the same work as i = i + 1;
Coming to your doubt, i+1 is only a value which is 1 greater than the current value of i, but i++ is statement which first calculates the value i+1 and then stores that value in(or assigns that value to) i.
2006-10-13 21:19:37
·
answer #3
·
answered by akaustav 1
·
0⤊
0⤋
i++ & i=i+1 is the same
but i+1 is quite different
this is usually done to assign a value to a variable
eg.-
a=i+1;
but if you are talking about i=i+1, then it is the same
2006-10-14 20:43:29
·
answer #4
·
answered by AYUSH J 1
·
0⤊
0⤋
i++ is a post incrementer which increases the value of i by 1.
i+1 does not increment the value of i
2006-10-13 22:05:59
·
answer #5
·
answered by arpit k 2
·
0⤊
0⤋
i++ is i=i+1 whereas i+1 is not that,
if u write i= i+1,it will be same as i++
2006-10-13 20:41:35
·
answer #6
·
answered by bhuvan 2
·
0⤊
0⤋
i++ is called post-increment operator returns the value i has after the incrementpublic class Test {
public static void main(String[] args) {
int pre = 1;
int post = 1;
System.out.println("++pre = " + (++pre));
System.out.println("post++ = " + (post++));
}
and i+1 is called pre-increment
2006-10-13 21:06:52
·
answer #7
·
answered by rajni k 1
·
1⤊
0⤋
i++ increments the value of i by one and sets that value to i
i+1 increments i by one but doesn't set i to anything
2006-10-13 20:46:03
·
answer #8
·
answered by poop 2
·
0⤊
1⤋
i++ is the same as i = i + 1, it's just shorter to type.
i++ is not "equivalent" to i+1, as you can't simply tell the compiler to "i+1". i++ is the same as typing "i = i + 1".
2006-10-13 20:40:52
·
answer #9
·
answered by mattomynameo 4
·
2⤊
0⤋