Register    Login    Forum    Search    FAQ

Board index » Programming » C#




 Page 1 of 1 [ 9 posts ] 



Author Message
 Post subject: pre and post increment
 Post Posted: Sun Jan 27, 2013 9:04 am 
Offline
Rookie
Rookie

Joined: Wed Jan 16, 2013 8:45 am
Posts: 10
Languages: urdu,english
hello i want to know the difference between pre and post increment. from what i know is that both thing performs addition over a variable .
How ever considering this example


The value is stored in a variable called a. Then the program will show the output as seen below:
The value of a is 10.
................................
The value of ++a is 11.
The value of a is 11.
The value of a++ is 11.
The value of a is 12.
The value of --a is 11.
The value of a is 11.
The value of a-- is 11.
The value of a is 10.

static void Main(string[] args)
{

int a = 10;
Console.WriteLine("a is {0}",a); // 10

int b = ++a;
Console.WriteLine("++a is {0}",b); //11
Console.WriteLine("a is {0}",a);//11

b =a++;
Console.WriteLine("a++ is {0}",b);//11
Console.WriteLine( "a is {0}", a);//12


b = --a;
Console.WriteLine("a-- is {0}",b);
Console.WriteLine("a is {0}",a);//11


b = a--;
Console.WriteLine("a -- is {0}",b );//11
Console.WriteLine("a is {0}",a );//10


why is it that in case b= a++ the increment does not show on the output however it shows for a ?


Top 
 Post subject: Re: pre and post increment
 Post Posted: Sun Jan 27, 2013 9:36 am 
Offline
Forum Veteran
Forum Veteran
User avatar

Joined: Wed Oct 26, 2011 2:41 am
Posts: 231
Location: South-Africa
Languages: c#, html, css, silverlight, java, arduino, xml, xaml, php, mysql, js, python
Text editor: visual studio 11
It is infact the same for a-- aswell.

b=++a; means to take a, plus one and then assign it to b.
b=a++; means take a, assign it to b and then add one to a.

same for --

_________________
www.seuntjie.com


Top 
 Post subject: Re: pre and post increment
 Post Posted: Sun Jan 27, 2013 11:52 am 
Offline
Rookie
Rookie

Joined: Wed Jan 16, 2013 8:45 am
Posts: 10
Languages: urdu,english
seuntjie wrote:

b=++a; means to take a, plus one and then assign it to b.
b=a++; means take a, assign it to b and then add one to a.

same for --



That is the Misconcept , most people think that way for instance consider this example :
tatic void Main(string[] args)
{
int x = 4;
Console.WriteLine("x++: {0}", x++); //after this statement x = 5
Console.WriteLine("x: {0}", ++x);

int y = 4;
Console.WriteLine("y--: {0}", y--); //after this statement y = 3
Console.WriteLine("--y: {0}", --y);

Console.ReadKey();
}
this prints out

x++: 4
x: 6
y--: 4
--y: 2

Although the increment / decrement is done(in case of post ) but still the output on console is not the incremented or decremented value but the original value that variable holds ?
now my question is why ?


Top 
 Post subject: Re: pre and post increment
 Post Posted: Mon Jan 28, 2013 9:53 pm 
Offline
Forum Veteran
Forum Veteran
User avatar

Joined: Wed Oct 26, 2011 2:41 am
Posts: 231
Location: South-Africa
Languages: c#, html, css, silverlight, java, arduino, xml, xaml, php, mysql, js, python
Text editor: visual studio 11
No that is not the misconception.
++a means increment the value and then use it. a++ means use the value then increment it, Wether your using int x =a++; or console.writeline(a++);

_________________
www.seuntjie.com


Top 
 Post subject: Re: pre and post increment
 Post Posted: Sat Feb 02, 2013 4:36 am 
Offline
Rookie
Rookie

Joined: Wed Jan 16, 2013 8:45 am
Posts: 10
Languages: urdu,english
seuntjie wrote:
No that is not the misconception.
++a means increment the value and then use it. a++ means use the value then increment it, Wether your using int x =a++; or console.writeline(a++);

i just wanted to enlighten u with the correct information now what ever u believe is your choice :-)


Top 
 Post subject: Re: pre and post increment
 Post Posted: Sat Feb 02, 2013 7:15 am 
Offline
Expert Poster
Expert Poster
User avatar

Joined: Fri Aug 10, 2012 7:02 am
Posts: 108
Languages: Dutch... To Lazy to update this every time
Text editor: A Self made one (Coded in C#, recoded in VB)
a++ known as postfix.
postfix:: First use its expression, then increase the value
++a known as prefix.
prefix:: First increase the value, then use its expression

so: Seuntje is right.

csharp code:
 
static void Main(string[] args) {
            int a = 10;
            Console.WriteLine(a++); // Writes 10
            Console.WriteLine(a); // Writes 11
            Console.ReadLine();
        }
 


Top 
 Post subject: Re: pre and post increment
 Post Posted: Tue Feb 05, 2013 12:46 pm 
Offline
Rookie
Rookie

Joined: Wed Jan 16, 2013 8:45 am
Posts: 10
Languages: urdu,english
Hilia wrote:
a++ known as postfix.
postfix:: First use its expression, then increase the value
++a known as prefix.
prefix:: First increase the value, then use its expression

so: Seuntje is right.

csharp code:
 
static void Main(string[] args) {
            int a = 10;
            Console.WriteLine(a++); // Writes 10
            Console.WriteLine(a); // Writes 11
            Console.ReadLine();
        }
 



in both the operations (prefix and post fix)the execution is exactly in same order but the only difference is that in postfix ,the result of the operation is the temporary copy of the original value that is why the answer is the same old value in case of a++ ,although the increment has taken place yet we cant see and the proof of it (i have already mentioned)

that is how i look at it i am not compelling u to believe it infact i was myself dubious about this thing that lead me to study about it


Top 
 Post subject: Re: pre and post increment
 Post Posted: Tue Feb 05, 2013 1:41 pm 
Offline
Forum Veteran
Forum Veteran
User avatar

Joined: Wed Oct 26, 2011 2:41 am
Posts: 231
Location: South-Africa
Languages: c#, html, css, silverlight, java, arduino, xml, xaml, php, mysql, js, python
Text editor: visual studio 11
@Nagisa dude if you ask a question like this and get a good answer from a qualified programmer ( I have a B.Sc in programming), don't argue.

for all practical purposes my original answer is correct. If you want detailed information on what happens inside, ask how it happens inside a++ and ++a that makes them different, not why the values are different, as in your first post.

_________________
www.seuntjie.com


Top 
 Post subject: Re: pre and post increment
 Post Posted: Wed Feb 06, 2013 2:35 am 
Offline
Rookie
Rookie

Joined: Wed Jan 16, 2013 8:45 am
Posts: 10
Languages: urdu,english
@seuntjie i am not doubting ur abilities neither do i need an info about ur qualification..
but yes the execution steps in case of a++ and ++a is something that mattersand i tend to seek insight knowledge about all cores and basics
Like i said i am not challenging anyone, i am just sharing my knowledge on the subject


Top 
Display posts from previous:  Sort by  
 
 Page 1 of 1 [ 9 posts ] 




Board index » Programming » C#


Who is online

Users browsing this forum: TurnitinBot [Bot] and 1 guest

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: