Easy Tutorial
❮ Perl If Statement Perl Error Handling ❯

Perl for Loop

Perl Loops

The Perl for loop is used to execute a sequence of statements multiple times, simplifying the code that manages loop variables.

Syntax

The syntax is as follows:

for ( init; condition; increment ){
   statement(s);
}

Here is the control flow analysis of the for loop:

Here, statement(s) can be a single statement or a block of several statements.

The condition can be any expression, and the loop executes when the condition is true and exits when the condition is false.

Flowchart

Example

#!/usr/bin/perl

# Execute the for loop
for( $a = 0; $a < 10; $a = $a + 1 ){
    print "a 的值为: $a\n";
}

Executing the above program will output:

a 的值为: 0
a 的值为: 1
a 的值为: 2
a 的值为: 3
a 的值为: 4
a 的值为: 5
a 的值为: 6
a 的值为: 7
a 的值为: 8
a 的值为: 9

Perl Loops

❮ Perl If Statement Perl Error Handling ❯