Click here to Skip to main content
15,436,489 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have confusion with i++ and ++i .
Please any one help me.
Posted

hi

once lookat this

C#
string[] items = {"a","b","c","d"};
int i = 0;
foreach (string item in items)
{
    Console.WriteLine(++i);
}
Console.WriteLine("");

i = 0;
foreach (string item in items)
{
    Console.WriteLine(i++);
}


output will be

C#
1
2
3
4

0
1
2
3


i hope it helps you...

Thanks&&Regards
Sandeep
 
Share this answer
 
If you are not sure about expressions then you should spend time studying the documentation[^]. It is important to get these concepts fixed in your mind.
 
Share this answer
 
First case
C++
int i = 10;
int j = i++;

After execution value of i would be 11 and value of j would be 10.
As this operator is POSTincrement , increament will be done AFTER the assignement.


Second case
C++
int i = 10;
int j = ++i;

After execution value of i would be 11 and value of j would be 11.
As this operator is PREincrement , increament will be done BEFORE the assignement.
 
Share this answer
 
v2
Comments
Nisarg S Shah 19-Jul-12 5:12am    
I think that "i would be 11 and value of j would be 10." and "i would be 11 and value of j would be 11." should be highlighted along with AFTER and BEFORE.
PrafullaVedante 20-Jul-12 2:54am    
Done :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900