Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Not understanding while loop counting

by pDaleC (Sexton)
on Aug 10, 2020 at 14:57 UTC ( [id://11120545]=note: print w/replies, xml ) Need Help??


in reply to Not understanding while loop counting

You probably want
$count = 0; while (++$count < 10) { print "count is now $count\n"; }
But I would suggest using the exact loop style for this situation:
For ($count=1; $count<10; ++$count) { print "count is now $count\n"; }

Replies are listed 'Best First'.
Re^2: Not understanding while loop counting
by haukex (Archbishop) on Aug 10, 2020 at 15:09 UTC
    I would suggest using the exact loop style for this situation

    Or the perlish for my $count (1..9) { ... } (personally I reserve for (;;) for more complex cases like modifying the loop variable in the loop body, but TIMTOWTDI).

Re^2: Not understanding while loop counting
by LanX (Saint) on Aug 10, 2020 at 16:34 UTC
    Addendum: see also perlsyn#For-Loops

    Perl's C-style for loop works like the corresponding while loop; that means that this:
    for ($i = 1; $i < 10; $i++) { ... }
    is the same as this:
    $i = 1; while ($i < 10) { ... } continue { $i++; }

    NB I have to admit I practically never use continue ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found