segmentation fault has asked for the wisdom of the Perl Monks concerning the following question:

This may be more of a general programming question but it's something that's been bugging me for a little while. What is the difference in using post as opposed to pre for increment/decrement. Personally I've always used post and it has worked out fine for me. I've read some explanations that were a little unclear. Also, if someone could show me some examples in which you would want to use one over the other, I would truly appreciate it. Sorry for the n00bish question.....

Replies are listed 'Best First'.
Re: pre v.s. post (increment/decrement)
by The Mad Hatter (Priest) on Jul 10, 2003 at 21:33 UTC
    If you do a pre-increment/decrement, the value is incremented/decremented and then used wherever. In a post-increment/decrement, the value is used first, and then it is incremented/decremented.
    my $i; $i = 0; print "\nPost-increment: ", $i++, "\nNow equals: ", $i, "\n\n"; $i = 0; print "\nPre-increment: ", ++$i, "\nNow equals: ", $i, "\n";
    output
    Post-increment: 0 Now Equals: 1 Pre-increment: 1 Now Equals: 1
Re: pre v.s. post (increment/decrement)
by BrowserUk (Patriarch) on Jul 10, 2003 at 21:54 UTC

    The clearest way of describing the difference I can think of is to fully expand the syntax

    print ++$i;

    is equivalent to

    $i = $i +1; print $i;

    whereas

    print $i++;

    is equivalent to

    print $i; $i = $i + 1;

    which I think emphasises the order in which things occur. The difference only becomes material when you use the pre- or post-increment notation as part of another statement. Ie. ++$i; and $i++; as stand-alone statements have identical effect.

    The difference only becomes material when the syntax is used as a part of another statement.

    Which can be exemplified by

    my @a = (1..10); my $i = 0; print $a[$i++] while $i < @a; 12345678910 $i = 0; print $a[++$i] while $i < @a; 2345678910 Use of uninitialized value in print at (eval 8) line 1, <> line 7.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: pre v.s. post (increment/decrement)
by sauoq (Abbot) on Jul 10, 2003 at 21:36 UTC
    What is the difference in using post as opposed to pre for increment/decrement.

    A post-increment (or decrement) is done after the containing expression is evaluated. A pre-increment (or decrement) is done prior to the containing expression being evaluated. An example will help.

    $ perl -le 'my $x = 0; print $x++' 0 $ perl -le 'my $x = 0; print ++$x' 1

    As for when you might want to use one instead of another, the one thing that springs to mind is when you want to increment or decrement an array index within the expression using it. The code below is contrived and I'd never actually do it like this, but it illustrates the point.

    # Fill @array with ten random numbers. my $i = 0; $array[$i++] = rand() while $i < 10;
    If we'd used a pre-increment, we would have populated the indices from 1 to 10 rather than 0 to 9 as desired.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: pre v.s. post (increment/decrement)
by revdiablo (Prior) on Jul 10, 2003 at 21:36 UTC

    Relevant snippet from perlop:

    "++" and "--" work as in C. That is, if placed before a variable, they increment or decrement the variable before returning the value, and if placed after, increment or decrement the variable after returning the value.

    The only difference is in what value is returned. Maybe the following code will demonstrate the difference more clearly:

    $a = 1; print ++$a, ":", $a, "\n"; $a = 1; print $a++, ":", $a, "\n";

    Update: yikes. 2 answers were posted in the time it took me to compose mine. I had a feeling this would happen with a question like this... :)

      I fail to see the problem. Pre-increment means that the value will be incremented before the value is returned. Post-increment means the value will be incremented after it is returned. It's very important to realize that it is not defined exactly when the increment occurs. It will be done after execution of the current statement started, and will be done before the statement is finished, but that's all you get.

      So, for your first line, you could get "2:2", or "2:1" and for the second line, you could get "1:2" or "1:1". Or some other weird value for the second $a.

      As a rule, never use a variable you are pre- or post incrementing or decrementing a second time in the same statement. And most certainly, don't modify the variable a second time. The following is undefined:

      my $i = 0; $i = ++ $i;
      meaning that if perl decides to erase your hard disk, it's still well inside the boundaries of its specification.

      Abigail

Re: pre v.s. post (increment/decrement)
by segmentation fault (Initiate) on Jul 10, 2003 at 22:23 UTC
    Ahhh ok. I think I finally get it. So it's like this: with pre-increment/decrement $i is immediately incremented/decremented and the value returned. Whereas with post-increment/decrement $i is evaluated first and then incremented/decremented accoringly, correct? So...
    $i = 0; $j = ++$i; $i and $j are both 1 $i = 0; $j = $i++; $i is now 1 and $j is 0
    P.S. Thanks for the speedy replies ^_^

      Yes, that is correct. One cool use I've seen for post-increment is in object initialization when the Damien Conway prevents multiple inheritance:

      return if ($self->{_init}{Object}++);

      This returns control to the calling execution if the initializing function has already been called in the inheritance hierarchy. If it hasn't already been initialized, it increments and proceeds. If it has already been initialized, it returns.