Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Doubt in the result which I got from (In|De)crement operators

by jesuashok (Curate)
on May 14, 2007 at 11:07 UTC ( [id://615270]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Doubt in the result which I got from (In|De)crement operators
by shmem (Chancellor) on May 14, 2007 at 11:23 UTC
    Quoting Abigail-II:
    Modifying the same variable using auto-increment/decrement more than once in the same statement gives undefined behaviour.

    See Quantum Weirdness and the Increment Operator

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Doubt in the result which I got from (In|De)crement operators
by Tomte (Priest) on May 14, 2007 at 11:24 UTC

    Right out of perlop:

    Note that just as in C, Perl doesn’t define when the variable is incremented or decremented. You just know it will be done sometime before or after the value is returned. This also means that modifying a variable twice in the same statement will lead to undefined behaviour. Avoid statements like: $i = $i ++; print ++ $i + $i ++;

    regards,
    tomte

    An intellectual is someone whose mind watches itself.
    -- Albert Camus

Re: Doubt in the result which I got from (In|De)crement operators
by talexb (Chancellor) on May 14, 2007 at 13:34 UTC

    This is cool from an academic standpoint, but what valid piece of code actually uses this? To put it another way, what are you trying to do, apart from turn Perl into a pretzel?

    Another question that comes to mind is, did you try the same thing, using brackets so as to guide the compiler to the order you want the operations performed?

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Doubt in the result which I got from (In|De)crement operators
by blazar (Canon) on May 15, 2007 at 08:26 UTC
    How the result becomes as 8. If I execute this manually based on the operator precedence I suspect it should be 9. but I am wondering about the end result, which is 8. I hope, monks will have a better explanation.

    Sorry for posting another "meta" reply, but... by any chance... are you doing (link @ GG) it again, jesuashok? Well, if not just don't take any offence, and the referenced links above may be of some interest to you.

Re: Doubt in the result which I got from (In|De)crement operators
by bones (Acolyte) on May 14, 2007 at 14:39 UTC
    humm, precedence question again. If you add parentheses so: "my $re = (++$val - ($val-- + --$val))" then return value is -11. I dont understand the why of the 8 value.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-03-28 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found