Precision in language is crucial to writing your code. In this case, you're saying a present value is that value, post-incremented. Strictly speaking, that's true, but writing the code in this fashion is apt to trip the unwary -- author included:
#!/usr/bin/perl =w use strict; #1137973 my $number=8; for(my $i=0; $i<3; $i++) { print "present value of \$i is $i and the number is: $number \n"; print " now printing \$number++: " . $number++ . "\n"; # NOTE O +UTPUT! print "!! NOW, NOT EARLIER the post-increment occurs AFTER Ln 10\ +n"; print " and after post-increment, \$i is STILL $i but number +is: $number\n\-----------\n"; } =head present value of $i is 0 and the number is: 8 now printing $number++: 8 # ie, unchanged! !! NOW, NOT EARLIER the post-increment occurs AFTER Ln 10 and after post-increment, $i is STILL 0 but number is: 9 ----------- present value of $i is 1 and the number is: 9 now printing $number++: 9 !! NOW, NOT EARLIER the post-increment occurs AFTER Ln 10 and after post-increment, $i is STILL 1 but number is: 10 ----------- present value of $i is 2 and the number is: 10 now printing $number++: 10 !! NOW, NOT EARLIER the post-increment occurs AFTER Ln 10 and after post-increment, $i is STILL 2 but number is: 11 ----------- =cut
You're cheese-paring the size of your script by a few bytes at the expense of clarity; better to post-increment in a standalone line of code -- for clarity.
UPDATE: ... or, as Athanasius pointed out in a message, a (standalone) pre-increment (++$number)could also be used effectively ... but that would REALLY require precise language... as in something like "the next number is: " or maybe "oops, off by one."
In reply to Re: usage of "my" keyword (quibble re clarity)
by ww
in thread usage of "my" keyword
by ravi45722
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |