There may be a situation when you need to execute a block of code
several times. Perl provide various control structures that allow
for more complicated execution paths.
Perl provides the following types of loop to handle the looping
requirements.
while loop
This loop is used to repeat a statement or group of statements
while a given condition is true. It tests the condition before
entering the block.
#!/usr/bin/perl
my $counter = 10;
while($counter > 0){
print("$counter\n");
$counter--;
}
#!/usr/bin/perl
my $num;
my @numbers = ();
print "Enter numbers, each per line :\n";
print "ctrl-z (windows) or ctrl-d(Linux) to exit\n>";
while(my $input = <>) {
print(">");
chomp $input;
$num = int($input);
push(@numbers, $num);
}
print "You entered: @numbers\n";
until loop
This loop is also used to repeat a statement or group of
statements until a given condition becomes true. It tests the
condition before entering the block.
#!/usr/bin/perl
my $counter = 5;
until($counter == 0){
print("$counter \n");
$counter--;
}
#!/usr/bin/perl
my $cmd;
print("Enter a command, enter exit to quit.\n");
do {
print(">");
chomp($cmd = <STDIN>);
$cmd = lc($cmd);
print("You entered:$cmd\n");
}until($cmd eq "exit");
for loop
This loop also executes a sequence of statements multiple times
and abbreviates the code that manages the loop variable.
#!/usr/bin/perl
my @a = (1..9);
for my $i (@a){
print("$i","\n");
}
#!/usr/bin/perl
my @c = (1..6);
for(my $i = 0; $i <= $#c; $i++){
print("$c[$i] \n");
}
foreach loop
The foreach loop iterates over a normal list value and sets the
variable to be each element of the list in turn.
#!/usr/bin/perl
my @a = (1..9);
foreach(@a){
print("$_","\n");
}
do...while loop
Like a while statement, except that it tests the condition at the
end of the loop body
#!/usr/bin/perl
my $command;
print("Enter a command, bye to quit.\n");
do {
print(">");
chomp($command = <STDIN>);
$command = lc($command);
# display the command
print("$command\n");
}while($command ne "bye");
Loop Control Statements
Different loop control statements used in Perl are discussed
below.
next statement
It is used to skip the remaining lines of it's enclosing loop and
moves the control to the next iteration immediately.
#!/usr/bin/perl
use warnings;
use strict;
my @data = (1,4,3,2,5,6,8,7,9);
my $key = 0;
do{
print "Enter a number to search(1-9):\n";
$key = int(<STDIN>);
}until($key >= 1 && $key <= 9);
my $pos = -1;
for my $i (@data){
$pos++;
next if($i != $key);
print("Found number $key at position $pos\n");
}
last statement
It terminates the loop statement and transfers execution to the
statement immediately following the loop.
#!/usr/bin/perl
use warnings;
use strict;
my ($key, $value);
my %h = ("apple" => 1, "orange" => 2, "mango" => 3, "coconut" => 4);
print("Please enter a key to search:\n");
$key = <STDIN>;
chomp($key);
$value = 0;
# searching
foreach(keys %h){
if($_ eq $key){
$value = $h{$_};
last; # stop searching if found
}
}
# print the result
if($value > 0){
print("element $key found with value: $value\n");
}else{
print("element $key not found\n");
}
continue statement
Let us try the following code to understand continue statement.
$i=1;
while ($i <= 10) {
if ($i == 5) {
print "\$i == $i\n";
next;
}
print "$i ";
}continue {$i++;}
redo statement
The redo command restarts the loop without evaluating the
condition. The continue block, if any, is not executed.
$number = 1;
while ( $number <= 5 ) {
if ( $number <= 10 ) {
print "$number ";
++$number;
redo;
}
}
print "\nStopped when \$number became $number.\n";
goto statement
Perl supports a goto command with three forms: goto label, goto
expr, and goto &name.
#!/usr/local/bin/perl
NEXTLINE: $line = <STDIN>;
if ($line ne "") {
print ($line);
goto NEXTLINE;
}
The Infinite Loop
If the condition of a loop never becomes false, the loop runs for
infinite time.
#!/usr/local/bin/perl
for( ; ; ) {
printf "This loop will run forever.\n";
}
You can terminate the above infinite loop by pressing the
Ctrl + C
keys.
When the conditional expression is absent, it is assumed to be
true. You may have an initialization and increment expression, but
as a programmer more commonly use the for (;;) construct to signify
an infinite loop.
Happy Exploring!
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.