1. Home
  2. Computing & Technology
  3. Perl

for loop - Beginning Perl Tutorial, Control Structures
How to use a for loop in Perl

From , former About.com Guide

Next: Examples of Perl's for loop

Perl's for loop, or for statement is used to loop through a designated block of code until a specific condition is met. The statement is made in three parts - the initial expression, the test expression, and a re-initialization of the initial expression:

for (initial; test; re-initialization) {
...
}
The initial expression is only evaluated once - at the start of the whole process. From then on, the test expression determines whether or not the code inside the block is executed. If the test evaluates to true the code is executed. If the test expression evaluates false the block of code is not executed and the program continues past the for loop.

If the code block is executed, the re-initialization of the initial expression occurs afterwards. The for loop process looks something like this when you break down each of the steps:

  1. Set the initial expression.
  2. Does the test evaluate to true? If so, continue, otherwise exit the for loop.
  3. Execute the code block inside the for loop.
  4. Reset the initial expression in some way.
  5. Return to step 2.
The for loop in Perl works much like the for loop in Java, C, and even Javascript.

Next: Examples of Perl's for loop

Explore Perl
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. Perl Tutorials
  5. Perl for loop tutorial - Control structures in Perl, learn how to use a for loop.>

©2009 About.com, a part of The New York Times Company.

All rights reserved.