Perl Loops
Loop is very important part of in a every programming language. Because loop is capable to iterates given group of statement under the particular condition.
Type of Loops
while loop
While loop is executing statement under certain condition expression. When expression result are valid or not zero then this loop execute statements. Otherwise this are terminate the loop execution process. This loop are very useful when we are no know about how many times are loop statement are work. We can define while-loop using of this syntax.
until loop
until loop are opposite to while-loop. When given expression are not valid then this loop statement are executed.
#!/usr/bin/perl
use warnings;
use strict;
my $counter = 1;
until($counter>5){
print "$counter \n";
$counter++;
}
1
2
3
4
5
for loop
for(initialization;condition_expression;update_expression) statements;
#!/usr/bin/perl
use warnings;
use strict;
for (my $counter = 1; $counter <= 5; $counter++) {
print "$counter \n";
}
1
2
3
4
5
foreach loop
#!/usr/bin/perl
use warnings;
use strict;
my @alphabet = ('A'...'G');
foreach(@alphabet){
print("$_ ");
}
A B C D E F G
do while loop
#!/usr/bin/perl
use warnings;
use strict;
my $counter=1;
#Displaying numbers from 1 to 5
do{
print("$counter ");
$counter++;
}while($counter<=5);
1 2 3 4 5
Loop Control Statements
Loop statements such as next, last, redo and goto are manage flow execution in Perl Programming Loops. This statement are very useful to change the state of loop.
next statement
#!/usr/bin/perl
use warnings;
use strict;
my @record =(1,"One",2,3,5,"Two","Three",1,2,"Four");
#get text element of given array
for my $text (@record){
if($text =~ /^[0-9,.E]+$/){
next;
}
print "$text\n";
}
One
Two
Three
Four
last statement
last statement are used to terminate execution flow of loop. That is similar in break statement in other programming.
#!/usr/bin/perl
use warnings;
use strict;
my @record =("Fruits","Water","Milk","Poison","Medicine");
for my $item (@record){
if($item eq "Poison"){
last;#terminate the loop
}
print "$item is Good to human\n";
}
Fruits is Good to human
Water is Good to human
Milk is Good to human
redo statement
This statement are used to repeat current iteration in loop.
#!/usr/bin/perl
use warnings;
use strict;
my $errors=5;
for my $data (0...3){
if($errors > 0){
print "Error Status : $errors \n";
$errors--;
redo;
}
print "Take Action : $data \n";
}
Error Status : 5
Error Status : 4
Error Status : 3
Error Status : 2
Error Status : 1
Take Action : 0
Take Action : 1
Take Action : 2
Take Action : 3
goto statement
#!/usr/bin/perl
use warnings;
use strict;
my $errors=5;
TEST:
if( $errors !=0){
print "$errors Errors Found\n";
$errors--;
goto TEST;
}
else{
print "Good No Error Found\n";
}
5 Errors Found
4 Errors Found
3 Errors Found
2 Errors Found
1 Errors Found
Good No Error Found
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment