Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

while loops

by root (Monk)
on Nov 05, 1999 at 04:22 UTC ( #948=perltutorial: print w/replies, xml ) Need Help??

while loops allow you to execute a group of statements so long as a statment still evaluates to true.
Here's a quick example;
$num=20; while($num<30){ print "num is $num\n"; $num=$num+1; } this will print out num is 20 num is 21 ... num is 29

then num is incremented to 30 which is no longer less than 30 so the code within the while statement is not executed and execution jumps to the next statement outside of the while statement.

If you think you know all about while statements learn about his less popular brother until.

Edit Wed Aug 1 07:07:10 EDT 2001 - Petruchio Changed PRE tags to CODE tags, added whitespace.

Edit Mon Dec 02:27:42 EST 2004 - Petruchio changed '$while' to 'while', because this is in our Tutorials section, and should be accurate.

Replies are listed 'Best First'.
RE: while loops
by Anonymous Monk on May 24, 2000 at 04:58 UTC
    the $ before while should be deleted
RE: while loops
by IainB (Initiate) on Oct 27, 2000 at 16:31 UTC
    I think something's missing from this description.

    It just doesn't look right to me.

      ... nor me. There is definitely some data corruption.
        i think it should go like this

        $num = 20 ;

        while ($num < 30 ){
        $num = $num + 10 ;
Re: while loops
by rlivings (Acolyte) on Aug 01, 2001 at 14:02 UTC
    initial example is still incorrect
      drop the $ before the while!
Re: while loops
by Glyndwr (Initiate) on Jan 11, 2002 at 18:35 UTC

    $num=20; $while($num<30){ print "num is $num\n"; $num=$num+1;\n ; }
    should be

    $num=20;<BR> while($num<30){ print "num is $num\n"; $num+=$num;\n; }
      No, not at all - Your code will only pass through a single iteration of the loop as on the first pass, the value of $num is added to $num. The result is that $num equals 40 and the while loop is exited. I think you may have been meaning, ignoring formatting syntax errors ...

      $num = 20; while ($num < 30) { print "num is $num\n"; ++$num; }

      Although, good pick up on the syntax error with the line - $while($num<30){ - Although this had been addressed previously within this thread.


      perl -e 's&&[@.]/&&s&.com.&_&&&print'

        yes , thanks

        in this case would $num++ also work

      Still not there.

      $num=20; while($num<30) { print "num is $num\n"; <b><u>$num+=$num;\n</b></u> } #I think you ment: $num += 1; #Though these should work also: $num++; ++$num;
      There is also an extra "\n;" in the code in the reply.
Re: while loops
by Anonymous Monk on Oct 21, 2004 at 04:31 UTC
    I think the example code is bad. The correct code is
    #!/usr/bin/perl $num=20; while ($num<30) { print "num is $num\n"; $num=$num+1; }


    El codigo que esta de ejemplo esta mal este es el correcto

    #!/usr/bin/perl $num=20; while ($num<30) { print "num is $num\n"; $num=$num+1; }

    20041020 Edit by ysth: use p, code, hr tags; fix tilde-n

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2023-12-07 16:44 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (33 votes). Check out past polls.