473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

increment difference

what's the difference between " i++ " and " ++i " ?

Thanks

Sep 10 '07 #1
2 1588

Vinnie --

++i = pre-statement increment and
i++ = post-statement increment

When used as a statement by itself the effective difference is none.

i.e.

int i = 0;
++i; // i = 1

and

int i = 0;
i++; // i = 1

both result in i being incremented by 1 and the resulting value is 1.

However when used in a more complex statement the position of the ++ can
make a difference.

The following statement results in the 2nd array value being assigned to x
because i is incremented before the assignment to x takes place.

int i = 0;
int[] a = { 1, 2, 3 };
int x = a[++i]; // x = 2; i is incremented pre-statement (before execution
of the statement).

....

In this case however the 1st array value is assigned to x because i is
incremented after the assignment to x takes place.

int i = 0;
int[] a = { 1, 2, 3 };
int x = a[i++]; // x = 1; i is incremented post-statement (after execution
of the statement).

HTH
--
Gregg Walker
Sep 10 '07 #2
Lee
On Sep 11, 6:59 am, vinnie <centro.ga...@gmail.comwrote:
what's the difference between " i++ " and " ++i " ?

Thanks
Assuming the appropriate declarations, then

i = 1;
x = i++;

assigns 1 to x -- "++" is post-increment operator.

i = 1;
x = ++i;

assigns 2 to x -- "++" is pre-increment operator.
Sep 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
98
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
13
by: kailasam | last post by:
Hello, Iam having a doubt. Which is more efficient post increment or Pre increment? I have read that preincrement is efficient than Post increment. Iam not able to think how it is? For an...
19
by: Kasya | last post by:
Difference between x++ and ++x; Difference between x-- and --x;
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
8
by: bintom | last post by:
Why doe the following C++ code behaves as it does? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3 i=1; cout << (i++)++; Output: Error. LValue required...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.