Increment

java Belgium
  • 13 years ago

    Can someone explain me the difference between pre and post increment please?

     Thank you

  • 13 years ago

    Assuming I understand what you are asking about,..

    For a variable called X,

    X++   is post incremented,

     ++X is pre-incremented.

    If these are the entire statements the end result is the same.

    X is greater than it was by the value of 1.

     

    However, it makes a difference if you do the increment in the same statement as an assignation.

    Thus, (assuming X starts with a value of 1)

    Y == X++;       

    ... results in Y holding 1  .. Y is given the value of X, THEN X is incremented. (POST)

    Y== ++X; 

    ...results in Y holding  2   ... the increment is done first, then the value is given to Y (PRE)

     

  • 13 years ago

    So in this case a pre incremented x will not be changed, only y will be given another value

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone” - Bjarne Stroustrup