1. Computing

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

©2013 About.com. All rights reserved.