codescracker


perl

Perl Loops



« Previous Tutorial Next Tutorial »


Loops in perl, are simply used to execute a block of code, required number of times. There are following types of loops available in perl:

Perl for Loop

Perl for loop is used to execute a sequence of statements, required number of times. Here is the general form of for loop in perl:

for(initialization; condition-checker; incrementation)
{
	statement(s);
}

Here is the explanation of the above general form of perl for loop:

Example

Here is an example program, illustrates the concept and use of for loop in perl:

#!/usr/local/bin/perl

for($i=1; $i<10; $i=$i+1)
{
	print("This time, value of i = $i\n");
}

Here is the output produced by the above perl program:

This time, value of i = 1
This time, value of i = 2
This time, value of i = 3
This time, value of i = 4
This time, value of i = 5
This time, value of i = 6
This time, value of i = 7
This time, value of i = 8
This time, value of i = 9

Perl while Loop

Perl while loop is used to repeat a statement or group of statement(s) until the given condition becomes false. Here is the general form of while loop in perl:

while(condition-checker)
{
	statement(s);
	incrementation;
}

Example

Here is an example program, demonstrating the while loop in perl:

#!/usr/local/bin/perl

$i=1;

while($i<10)
{
	print("This time, value of i = $i\n");
	$i = $i + 1;
}

Here is the output produced by the above perl program:

This time, value of i = 1
This time, value of i = 2
This time, value of i = 3
This time, value of i = 4
This time, value of i = 5
This time, value of i = 6
This time, value of i = 7
This time, value of i = 8
This time, value of i = 9

Perl do-while Loop

Perl do-while loop is similar to while loop, but in do-while loop, the condition checker is placed at the bottom position. So do-while loop must run its block of code at least one time. Therefore, do-while loop is mainly used in menu-driven program. Here is the general form to use do-while loop in perl:

do
{
	statement(s);
	incrementation;
}while(condition-checker);

Example

Here is an example, helps in understanding the do-while loop in perl:

#!/usr/local/bin/perl
		
$i=1;

do
{
	print("This time, value of i = $i\n");
	$i = $i + 1;
}while($i<10);

Here is the sample output of the above perl program:

This time, value of i = 1
This time, value of i = 2
This time, value of i = 3
This time, value of i = 4
This time, value of i = 5
This time, value of i = 6
This time, value of i = 7
This time, value of i = 8
This time, value of i = 9

Perl until Loop

Perl until loop is used to repeat a statement or group of statement until the given condition becomes true. Perl until loop looks like similar to perl while loop, just replace while with until to check its general form and example.

Perl foreach Loop

Perl foreach iterates over a normal list value and sets the variable to be each element of the list in turn. Here is the general form to use foreach loop in perl:

foreach var (list)
{
	statement(s);
}

Example

Here is an example program, helps you in understanding about foreach loop in perl:

#!/usr/local/bin/perl
		
@list = (1, 2, 3, 4, 5, 6, 7, 8 ,9);

foreach $i (@list)
{
	print("This time, value of i = $i\n");
}

Below is the output given, produced by the above perl program:

This time, value of i = 1
This time, value of i = 2
This time, value of i = 3
This time, value of i = 4
This time, value of i = 5
This time, value of i = 6
This time, value of i = 7
This time, value of i = 8
This time, value of i = 9

Perl nested Loop

Perl nested loop means one loop inside another in perl. You are free to use one loop inside another in perl. Here is the general form to use nested for loop in perl:

for(initialization; condition-checker; incrementation)
{
	for(initialization; condition-checker; incrementation)
	{
		statement(s);
	}
	statement(s);
}

Here is the general form of nested while loop in perl:

while(condition-checker)
{
	while(condition-checker)
	{
		statement(s);
		incrementation;
	}
	statement(s);
	incrementation;
}

Here is the general form of nested do-while loop in perl:

do
{
	statement(s);
	do
	{
		statement(s);
		incrementation;
	}while(condition-checker);
	incrementation;
}while(condition-checker);

Here is the general form to use nested until loop in perl:

until(condition-checker)
{
	until(condition-checker)
	{
		statement(s);
		incrementation);
	}
	statement(s);
	incrementation;
}

Here is the general form to use nested foreach loop in perl:

foreach var1 (list1)
{
	foreach var2 (list2)
	{
		statement(s);
	}
	statement(s);
}

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test