Iteration with foreach and for Loops
while loops provide a general way to repeat a block of code—that block will just keep executing until some test is met. The for loop, a second form of loop, is a slightly different approach to the same problem. With for loops, the loop is executed a specific number of times, and then stops. This is sometimes called iteration because the focus is on the specific number of loops as opposed to the more vague “loop until true” that the while provides.
In just about every case, you could write a while loop to do iteration, or you could write a for loop to do the same thing as a while. But some tasks lend themselves better to one form or another, and so we have multiple kinds of loops.
Perl provides two for loops: ...