Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by jesuashok (Curate)
on May 14, 2007 at 11:07 UTC ( #615270=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

monks,

#!/usr/bin/perl use strict; my $val = 10 ; my $re = ++$val - $val-- + --$val; print $re; output :- 8
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.

Comment on Doubt in the result which I got from (In|De)crement operators
Download Code
Re: Doubt in the result which I got from (In|De)crement operators
by shmem (Canon) 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 (Canon) 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 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.
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.

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-07-14 09:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (257 votes), past polls