Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: $i=$i++

by dsb (Chaplain)
on Apr 29, 2002 at 15:41 UTC ( [id://162841]=note: print w/replies, xml ) Need Help??


in reply to $i=$i++

++Ovid, but I have one more point.

I ran the following code:

$i = $i++; print "Test 1: $i\n"; $i = 0; $i = 0 + $i++; $i = ++$i + $i++; print "Test 2: $i\n"; $i = 0; $i = ++$i + $i++; print "Test 3: $i\n"; $i = 0; $i = $i++ + ++$i; print "Test 4: $i\n";
And got the following output:
Test 1: 0 Test 2: 3 Test 3: 3 Test 4: 2
Remebering what Ovid said about pre vs. post increment, when you examine this output you may realize that by the time the print statements in Test2, Test3, and Test4 have been executed, $i has actually been incremented one last time by the post increment operator.

So if you thought your output was weird maybe this has something to do with it.

UPDATE: I was going through my posts and found this one. The final incrementation I mention I believe is ultimately irrelevant since the expression's return value i assigned to $i thereby over-writing the value $i has after the ++ operator.




Amel

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-19 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found