Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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).

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2020-09-18 14:55 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (112 votes). Check out past polls.