Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: post-increment/pre-increment blues

by ariels (Curate)
on Jun 18, 2002 at 09:48 UTC ( [id://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).

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-29 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found