Perl for loop is also known as C-style for loop. The for loop iterates the statement or a part of the program several times.
It has three parameters:
The syntax of for loop in Perl language is given below:
for(initialization;condition;incr/decr){ //code to be executed }
Let's see the simple program of for loop that prints table of 1.
for( $a = 1; $a <= 10; $a++ ){ print " $a\n"; }
In this example, one for loop is nested inside another for loop. Inner loop is executed completely while outer loop is executed only once. It means if loop is running for 3 times, outer loop will execute 3 times but inner loop will execute 9 times.
for( $i = 1; $i <= 3; $i++ ){ for( $j = 1; $j <= 3; $j++ ){ print " $i $j\n"; } }
If double semicolon (;;) is used in for loop, the loop will execute for infinite times. You can stop the execution using ctrl + c.
for( ; ; ) { printf "Infinite For Loop\n"; }