Looping


A loop is a way to repeat a program action multiple times. A very simple example is a countdown timer that performs a task (waiting for one second) 300 times before telling you that your egg is done boiling.

Looping constructs (also known as control structures) can be used to iterate a block of code as long as certain conditions apply, or while the code steps through (evaluates) a list of values, perhaps using that list as arguments. Perl has four looping constructs: for, foreach, while, and until.

for

The for construct performs a statement (block of code) for a set of conditions defined as follows:

for (start condition; end condition; increment function) {   statement(s) }


The start condition is set at the beginning of the loop. Each time the loop is executed, the increment function is performed until the end condition is achieved. This looks much like the traditional for/next loop. The following code is an example of a for loop:

for ($i=1; $i<=10; $i++) {       print "$i\n" }


foreach

The foreach construct performs a statement block for each element in a list or array:

@names = ("alpha","bravo","charlie"); foreach $name (@names) {   print "$name sounding off!\n"; }


The loop variable ($name in the example) is not merely set to the value of the array elements; it is aliased to that element. That means if you modify the loop variable, you're actually modifying the array. If no loop array is specified, the Perl default variable $_ may be used:

@names = ("alpha","bravo","charlie"); foreach (@names) {   print "$_ sounding off!\n"; }


This syntax can be very convenient, but it can also lead to unreadable code. Give a thought to the poor person who'll be maintaining your code. (It will probably be you.)

Note

foreach is frequently abbreviated as for.


while

while performs a block of statements as long as a particular condition is true:

while ($x<10) {      print "$x\n";      $x++; }


Remember that the condition can be anything that returns a true or false value. For example, it could be a function call:

while ( InvalidPassword($user, $password) )        {         print "You've entered an invalid password. Please try again.\n";         $password = GetPassword; }


until

until is the exact opposite of the while statement. It performs a block of statements as long as a particular condition is falseor, rather, until it becomes true:

until (ValidPassword($user, $password)) {   print "You've entered an invalid password. Please try again.\n";   $password = GetPassword; }


last and next

You can force Perl to end a loop early by using a last statement. last is similar to the C break commandthe loop is exited. If you decide you need to skip the remaining contents of a loop without ending the loop itself, you can use next, which is similar to the C continue command. Unfortunately, these statements don't work with do ... while.

On the other hand, you can use redo to jump to a loop (marked by a label) or inside the loop where called:

$a = 100; while (1) {   print "start\n";   TEST: {     if (($a = $a / 2) > 2) {       print "$a\n";       if (--$a < 2) {       exit;     }     redo TEST;   }  } }


In this simple example, the variable $a is repeatedly manipulated and tested in an endless loop. The word "start" will only be printed once.

do ... while and do ... until

The while and until loops evaluate the conditional first. The behavior is changed by applying a do block before the conditional. With the do block, the condition is evaluated last, which results in the contents of the block always executing at least once (even if the condition is false). This is similar to the C language do ... while (conditional) statement.



Red Hat Fedora 5 Unleashed
Red Hat Fedora 5 Unleashed
ISBN: 067232847X
EAN: 2147483647
Year: 2004
Pages: 362

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