Control Loops in PERL - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

Control Loops in PERL

Share This

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.

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/perl
my $counter = 10;
while($counter > 0){
print("$counter\n");
$counter--;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


1
2
3
4
5
6
7
8
9
10
#!/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 = <>) {
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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.


1
2
3
4
5
6
7
8
9
10
#!/usr/bin/perl
my $counter = 5;
until($counter == 0){
print("$counter \n");
$counter--;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


1
2
3
4
5
6
7
8
9
10
#!/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");
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

for loop

This loop also executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.


1
2
3
4
5
6
7
8
#!/usr/bin/perl
my @a = (1..9);
for my $i (@a){
print("$i","\n");
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


1
2
3
4
5
6
7
8
#!/usr/bin/perl
my @c = (1..6);
for(my $i = 0; $i <= $#c; $i++){
print("$c[$i] \n");
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

foreach loop

The foreach loop iterates over a normal list value and sets the variable to be each element of the list in turn.


1
2
3
4
5
6
7
8
#!/usr/bin/perl
my @a = (1..9);
foreach(@a){
print("$_","\n");
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

do...while loop

Like a while statement, except that it tests the condition at the end of the loop body


1
2
3
4
5
6
7
8
9
10
#!/usr/bin/perl
my $command;
print("Enter a command, bye to quit.\n");
do {
print(">");
chomp($command = <STDIN>);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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.


1
2
3
4
5
6
7
8
9
10
#!/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";
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

last statement

It terminates the loop statement and transfers execution to the statement immediately following the loop.


1
2
3
4
5
6
7
8
9
10
#!/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");
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

continue statement

Let us try the following code to understand continue statement.


1
2
3
4
5
6
7
8
9
10
$i=1;
while ($i <= 10) {
if ($i == 5) {
print "\$i == $i\n";
next;
}
print "$i ";
}continue {$i++;}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

redo statement

The redo command restarts the loop without evaluating the condition. The continue block, if any, is not executed.


1
2
3
4
5
6
7
8
9
10
$number = 1;
while ( $number <= 5 ) {
if ( $number <= 10 ) {
print "$number ";
++$number;
redo;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

goto statement

Perl supports a goto command with three forms: goto label, goto expr, and goto &name.


1
2
3
4
5
6
7
8
9
#!/usr/local/bin/perl
NEXTLINE: $line = <STDIN>;
if ($line ne "") {
print ($line);
goto NEXTLINE;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The Infinite Loop

If the condition of a loop never becomes false, the loop runs for infinite time.


1
2
3
4
5
6
7
#!/usr/local/bin/perl
for( ; ; ) {
printf "This loop will run forever.\n";
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.