in reply to post-increment/pre-increment blues

The subject of autoincrement and side effects was discussed in this thread. Although we are not dealing with lvalue assignment to $i, as ariels has said, the result of the calculation is defined to be ambiguous in the C standard, and I guess the same is true of Perl. Hence you can get different results depending on platform, version and what mood the optimiser is feeling in :).

Replies are listed 'Best First'.
To get 14...
by SteveRoe (Novice) on Jun 19, 2002 at 16:13 UTC
    print ++$i + $i++ + 1;

    Entering this line with i==5 prints (6+7+1) or (7+6+1). Sooo ++ has higher precedence than +. Certainly fun, but really not so hard to figure out why 14, eh?

      ++ has higher precedence than +, but that's irrelevant. Precedence only determines how things are parsed. It does not determine the order of evaluation.

      As pointed out to people before, the value of the expression ++$i + $i++ + 1 is undefined. Any reasoning about what the value would be is a futile exercise. Any answer is as good (or wrong) as any other.

      Abigail