Recent Articles

A Whole New World (Wide Web)
Although Perl may not be able to produce flying carpets, blue genies, or beautiful Arab princesses, it can be used for nearly limitless other applications.

Perl's Staying Power Advertised Again
ActiveState is a company that specializes in dynamic languages, and whether or not you've heard of it, it's quite successful, with a long list of customers that includes impressive organizations like Cisco, HP, and...

10 Perl Foundation/Parrot Foundation Projects...
Not too long ago, we talked about Google's annual Summer of Code program and its potential importance to Perl. Now, that importance has been more or less confirmed, as the Perl Foundation and the...

Perl 5.12.0 Released
A new version of Perl has arrived, and it should prove deserving of a very warm welcome. Perl 5.12.0 was a long time coming, and it represents a significantly...

BioPerl Makes Google Summer Of Code Cut
Google's annual Summer of Code is a rather big deal, bringing together excellent students from all over the world to work on open source software projects.

Perl-AdWords Crisis Foreseen, Averted
It looks like the Perl community suffered a close call at the hands of a certain search giant. Although allowances have now been made, John Napiorkowski stated...



06.15.10



PERL Loops

By Bryan Young

There are several different flavors of loops available when coding in PERL. The four most commonly used loops are the while loop, the until loop, the for loop, and the foreach loop. Below you will find a quick tutorial on how each is used.

The While Loop
The while loop is used the same way in PERL as the rest of the mainstream programming languages. Simply put you are testing a condition and while that condition returns true, the looped code is continually ran.

$count = 1;
while ($count <= 5)
{
print "$count, ";
$count++;
}
print "six!";

This will output "1, 2, 3, 4, 5, six!"

The Until Loop

The until loop is the logical opposite of the while loop. The condition is tested, and as long as the condition tests false, the code is repeated.

$count = 1;
until ($count == 5)
{
print "$count, ";
$count++;
}
print "six!";


This will output "1, 2, 3, 4, six!" Once the condition is met, the code sequence immediately picks up at the statement following the loop.

The For Loop

The for loop is basically a while loop with a built in counter. You initialize the counter variable, give the condition for the loop, and also the increment to be used all in one line of code.

for ($i = 0; $i < 10; $i += 2)
{
print "$i, ";
}
print "done!";

This loop will output "0, 2, 4, 6, 8, done!" The built in counter is useful when you know beforehand how many times you want the loop to propogate.

The Foreach Loop

The foreach loop is used exclusively when dealing with arrays. This loop will execute its code once for each element in the provided array. Let's initialize an array and print each element separated by a comma.

@array = ("element1", "element2", "element3");
foreach $element (@array)
{
print "$element ";
}

This will output "element1 element2 element3 ". You will notice the $element variable placed before the parenthesis. This variable stores the value of each index from the array as the loop processes.

About the Author:
Bryan Young is a staff writer WebProNews.
About PerlProNews
PerlProNews is a collection of news and commentary designed to keep you in step with the ever evolving landscape of Perl environments. News and Advice for Perl Professionals





PerlProNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
PerlProNews.com SQLProNews.com
SysAdminNews DevWebPro.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITCertificationNews.com






-- PerlProNews
is an iEntry, Inc. publication --
iEntry, 2549 Richmond Rd. Lexington KY, 40509
2010 iEntry, Inc. All Rights Reserved Privacy Policy Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article



Database Forum News and Advice for Perl Professionals PerlProNews News Archives About Us Feedback PerlProNews.com About Article Archive News Downloads WebProWorld Forums iEntry Advertise Contact Jayde