Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: post-increment/pre-increment blues

by ariels (Curate)
on Jun 18, 2002 at 09:48 UTC ( #175320=note: print w/ replies, xml ) Need Help??


in reply to post-increment/pre-increment blues

my $i = 0;

  1. "print ++$i + 1;" -- increments and prints 2.
  2. "print $i++ + 1;" -- prints 2 and increments.
  3. "print $i + $i++;" -- isn't documented to do anything particularly useful in Perl. "perl" might have some particular behaviour, but it is in no way guaranteed in any way or form. Other languages have multiple implementations; that makes separating implementation peculiarities from the language definition easier.

    In fact, the equivalent C code "i+(i++)" is undefined behaviour in ISO C: the compiler is allowed to return (assuming the variable starts off with 2) any value, or write code to format your computer's disks, or write code to format everybody else's computers' disks, or cause daemons to fly out of your nose.

  4. "print ++$i + ++$i;" -- You guessed it! Same as above!
  5. "print ++$i + $i++ + 1;" -- Yup. Same again.

For related fun, try the various "$i=$i++" (also undefined).


Comment on Re: post-increment/pre-increment blues
Select or Download Code
Re: Re: post-increment/pre-increment blues
by Juerd (Abbot) on Jun 18, 2002 at 14:56 UTC

    or write code to format your computer's disks, or write code to format everybody else's computers' disks, or cause daemons to fly out of your nose.

    Of course, it could also trigger a nuclear explosion. Can we please avoid useless exaggeration?

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      This particular piece of exaggeration is kind of traditional though...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://175320]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-08-01 10:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (2 votes), past polls