Section 2.9. The while Control Structure


2.9. The while Control Structure

Like most algorithmic programming languages, Perl has a number of looping structures.[] The while loop repeats a block of code as long as a condition is true:

] Every programmer eventually creates an infinite loop by accident. If your program keeps running and running, you can generally stop it in the same way youd stop any other program on your system. Often, typing Ctrl-C will stop a runaway program; check with your system's documentation to be sure.

     $count = 0;     while ($count < 10) {       $count += 2;       print "count is now $count\n"; # Gives values 2 4 6 8 10     } 

As always in Perl, the truth value here works like the truth value in the if test. Like the if control structure, the block curly braces are required. The conditional expression is evaluated before the first iteration, so the loop may be skipped completely if the condition is initially false.



Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2003
Pages: 232

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net