Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

while loops

by root (Scribe)
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.

Comment on while loops
Download Code
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 NodeReaper (Curate) on Jan 11, 2002 at 18:28 UTC

    Reason: (rob_au)

    For more information on this node visit: this

      In what language? Not Perl, I'm sure.

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$


        Reason: (blakem) delete -- obvious troll bait

        For more information on this node visit: this

Re: while loops
by Glyndwr (Initiate) on Jan 11, 2002 at 18:35 UTC
    Yours

    $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&&rob@cowsnet.com.au&&&split/[@.]/&&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; }

    Español

    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-09-23 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (216 votes), past polls