The Perl language allows the use of a loop within another loop. Here are a few examples to illustrate this concept.
Syntax for nested for loop statements:
for ( init ; condition ; increment ) { for ( init ; condition ; increment ) { statement ( s ) ; } statement ( s ) ; }Syntax of nested while loop statement:
while ( condition ) { while ( condition ) { statement ( s ) ; } statement ( s ) ; }Syntax of nested do...while loop statements:
do { statement ( s ) ; do { statement ( s ) ; } while ( condition ) ; } while ( condition ) ;Syntax for nested until loop statements:
until ( condition ) { until ( condition ) { statement ( s ) ; } statement ( s ) ; }Syntax of nested foreach loop statement:
foreach $a ( @listA ) { foreach $b ( @listB ) { statement ( s ) ; } statement ( s ) ; }Executing the above program, the output result is:
a = 0, b = 0a = 0, b = 1a = 0, b = 2a = 1a = 1, b = 0a = 1, b = 1a = 1, b = 2a = 2a = 2, b = 0a = 2, b = 1a = 2, b = 2a = 3