On Mar 30, WC -Sx- Jones said:
>my $count;
>
>while(1) {
> (++$count) ? $count += $count-- : $count += $count++;
>
> print "$count\n"; exit if $count > 60_000;
> sleep 1;
>}
The main problem is the PRECEDENCE. Your ? : line is run like so:
((++$count) ? ($count += $count--) : $count) += $count++;
That is, no matter what branch is used, += $count++ always happens. As it
turns out, ++$count is always true, so your code is:
while (1) {
++$count;
$count += $count--;
$count += $count++;
}
This is tricky business. When $count is, for instance, 10,
$count += $count--;
what happens is this: first, the right-hand side is evaluated, so the
value of $count is returned (10), and then $count has one subtracted from
it, so $count is 9. Then, 10 is added to $count's value, so $count ends
up being 19. On the next line,
$count += $count++;
it works very similarly: first, $count's value is returned on the
right-hand side, and then $count's value is incremented by 1. This means
we're adding 19 to 20, to get 39.
So the first line adds 1 to $count. The second line multiplies by 2 and
subtracts 1. The third line multiplies by 2 and adds 1. The net result
is adding one to $count, and then multiplying it by 4 and subtracting 1.
my $count = 0;
while (1) {
$count = ($count + 1) * 4 - 1;
print "$count\n";
exit if $count > 60_000;
}
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
CPAN ID: PINYAN [Need a programmer? If you like my work, let me know.]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>