Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: porting C code to Perl (updated)

by haukex (Archbishop)
on Oct 23, 2017 at 20:08 UTC ( [id://1201929]=note: print w/replies, xml ) Need Help??


in reply to Re^2: porting C code to Perl
in thread porting C code to Perl

the third part of the Perl's C style for loop is a statement on it's own: ie ++$var; or $var++; are identical.

Yes, that's correct*, when they're written standalone as in for(;;$var++). ++$var vs. $var++ only makes a difference when its return value is used: ++$var will first increment the variable and then return the new value, while $var++ will return the old value and then increment the variable.

$ perl -le 'for ( my $x=0; $x<3; print $x++ ) { }' 0 1 2 $ perl -le 'for ( my $x=0; $x<3; print ++$x ) { }' 1 2 3

* Update: In fact, note what Perl does here (edited for brevity):

$ perl -MO=Deparse -e 'for ( my $x=0; $x<3; print $x++ ) { }' for (my $x = 0; $x < 3; print $x++) { (); } $ perl -MO=Deparse -e 'for ( my $x=0; $x<3; print ++$x ) { }' for (my $x = 0; $x < 3; print ++$x) { (); } $ perl -MO=Deparse -e 'for ( my $x=0; $x<3; ++$x ) { }' for (my $x = 0; $x < 3; ++$x) { (); } $ perl -MO=Deparse -e 'for ( my $x=0; $x<3; $x++ ) { }' for (my $x = 0; $x < 3; ++$x) { (); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found