Perl nested Loop
w3hello.com logo
Perl Basics
Perl Introduction
Perl Installation
Perl Comments
Perl String
Perl Operators
Perl Variables
Perl Arrays
Perl do...while Loop
Perl Environment
Perl Syntax Overview
Perl while Loop
Perl Array with Loops
Perl IF...ELSE statement
Perl IF Statement
Perl if-else
Perl foreach Loop
Perl for Loop
Perl continue Statement
Perl nested Loop
Perl until Loop
Perl next Statement
Perl Array
Perl Loops
Perl File I/O
Perl goto
Perl Socket Programming
Perl Error Handling
Perl Directories
Perl Process Management
Perl stdin
Real Device Vs Emulator Testing: Ultimate Showdown
Perl Formats Writing Reports
Perl Regular Expressions
Perl Create Excel File
A complete guide to CSTE certification
Perl Hashes
Perl Special Variables
Perl do-while Loop
Perl Chop() Vs Chomp()
The Perl DBI
Perl IF...ELSIF statement
Perl print() and say()
Perl Database Access
Perl Formats
Perl last Statement
Perl UNLESS...ELSE statement
Perl Scalars
How to organize Requirements as a Business Analyst
Perl Packages and Modules
Perl goto Statement
Perl Object Oriented
Perl Subroutines
Perl redo Statement
Perl Command Line Arguments | argv
Perl String Escaping Characters
Perl Date and Time
Perl Modules and namespaces
Perl UNLESS...ELSIF statement
Perl Conditional Statements IF...ELSE
Perl Functions and Subroutines
Perl Sending Email
Perl DBI
Perl Versions
Top 100 QTP Interview Questions & Answers
Perl Operator Types
Perl switch Statement
Perl File handling
Perl References
Perl CGI Programming
Object Oriented Programming in PERL
Perl switch
Perl Interview Questions
How PERL is used in Automation Testing
Perl Functions References
Perl Multidimensional Array
Perl unless Statement
Perl Basics
Previous : Perl continue Statement Next : Perl until Loop

Perl nested Loop


A loop can be nested inside of another loop. Perl allows to nest all type of loops to be nested.

Syntax

The syntax for a nested for loop statement in Perl is as follows −

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

The syntax for a nested while loop statement in Perl is as follows −

while(condition){
   while(condition){
      statement(s);
   }
   statement(s);
}

The syntax for a nested do...while loop statement in Perl is as follows −

do{
   statement(s);
   do{
      statement(s);
   }while( condition );

}while( condition );

The syntax for a nested until loop statement in Perl is as follows −

until(condition){
   until(condition){
      statement(s);
   }
   statement(s);
}

The syntax for a nested foreach loop statement in Perl is as follows −

foreach $a (@listA){
   foreach $b (@listB){
      statement(s);
   }
   statement(s);
}

Example

The following program uses a nested while loop to show the usage −

#/usr/local/bin/perl
   
$a = 0;
$b = 0;

# outer while loop
while($a < 3){
   $b = 0;
   # inner while loop
   while( $b < 3 ){
      print "value of a = $a, b = $b
";
      $b = $b + 1;
   }
   $a = $a + 1;
   print "Value of a = $a

";
}

This would produce the following result −

value of a = 0, b = 0
value of a = 0, b = 1
value of a = 0, b = 2
Value of a = 1

value of a = 1, b = 0
value of a = 1, b = 1
value of a = 1, b = 2
Value of a = 2

value of a = 2, b = 0
value of a = 2, b = 1
value of a = 2, b = 2
Value of a = 3
Previous : Perl continue Statement Next : Perl until Loop
© Copyright 2018 W3Hello Publishing Limited. All rights reserved.
Contact us | Sitemap