Perl While: The While Conditional Loop in Perl

perl while starsPerl is one of the most used languages in the world. The language is sometimes called the Practical Extraction and Reporting Language (not an official acronym). Perl has undergone numerous revisions since it was introduced in the year 1987. The language was first designed to be a general purpose scripting language for UNIX systems. The functionality of the language has increased greatly since then, and today the language has extensive libraries that can accomplish almost any task you can think of. The language is a favorite with web developers and application designers because of its open source nature. It’s possible to modify any of the modules found in Perl, or even the language itself. It’s also a favorite with IT managers, because the costs involved in using Perl to perform tasks are not very high.

Perl is also a very good choice as a first programming language. The syntax of the language, at least at the beginning, is easy because it has strong roots in linguistics. It has an auto garbage collector, which makes writing programs easy. Perl is an easy language for beginners to learn, as whole. If you are a programming newbie, don’t hesitate to choose Perl. If you already have programming experience and know languages like C++, you’ll find it even easier to learn (though it is very different from C++, as in interpreted language. You can learn more about C programming in this course.). If you’re interested,we offer an introductory Perl course that you can take to learn the language. You can also take a look at our Perl tutorials to learn some of the important basic concepts.

In this tutorial, we’re going to go over the details of the while conditional loop in Perl.

The While Conditional Loop

The while conditional loop is used to perform a task repetitively as long as a given condition is true, that is, a block of code gets executed repeatedly until the condition turns false. The general syntax for the while conditional loop is:

while (expression is true)
{
Execute this
}

The expression parameter generates a Boolean value. If the value is false, the code in the block (between the curly braces) won’t get executed and Perl will move on to remaining part of the program (if there is any). If the Boolean value is true, then the code is executed. Then the expression is checked again. If it continues to be true, the code block will continue being executed. Sounds simple, doesn’t it? Usually, the expression parameter keeps getting modified in the code block. This is to prevent an infinite loop from occurring.

Examples of the While Conditional Loop

You’ll understand the Perl while loop better with the help of a few examples. First, let’s see how the conditional evaluation really works. Let’s take a false condition and see what happens:

#!/usr/bin/perl
$month = "January";
while ($month eq "February") {
  print "The month is February\n";
}
print "The month is January\n";

Output

The month is January

This is a very basic example of the while loop. Here, we’ve created a simple variable called “month” and set its value to “January”.  In the conditional expression, Perl will check if the month has the value of “February”. Because it obviously doesn’t, the code within the block is skipped and following line of code will get executed (which was a simple print line statement). This is why the output of the program is “The month is January”.

Now, let’s take a true condition and have the program execute the code in the block:

#!/usr/bin/perl
$month = "June";
while ($month eq "June") {
  print "The month is June\n";
}
print "The month is January\n";

Output:

The month is June
The month is June
The month is June
…

Do you see what has happened here? Because the condition was true, that is, the month was “June”, the code in the block got executed and we obtained the output: “The month is June”. Then, Perl checked the conditional statement again. Because the month was still June, it executed the code in the block again and we got the output: “The month is June” a second time. Because the conditional expression will never be false, the code block will keep getting executed an infinite number of times. Perl will never get to the line next line of code: “The month is January”. This is known as an infinite loop and it’s considered bad programming if your script includes one (unless it’s for learning purposes). You can try executing this program for yourself to see the infinite loop in action. You will have to exit the loop manually by hitting “Ctrl + Z” if you’re using a UNIX system or “Ctrl + C” in Windows. If you’re unfamiliar with the basic syntax of a script in Perl or how to execute a Perl script, sign up for our Perl scripting course. We will teach you all the basics in our course.

Those familiar with C, will see that the while loop in Perl, is pretty similar to that in C. If you’re new to programming, you should definitely check this course on C programming, to learn more about it.

So how do you avoid an infinite loop? You can add a constantly incrementing counter that, when it reaches a certain value, makes the Boolean expression parameter false. This way, you can get the code in the block executed a limited number of times:

#!/usr/bin/perl
$counter = 0;
while ($counter <= 5) {
print "$counter<br />";
$counter ++;
}
print "You have now exit the loop";

Output:

0
1
2
3
4
5
You have now exit the loop

In this script, we’ve used a variable called “counter” that has the value of 0. As long as the value of the “counter” is less than or equal to 5 (<=), the value of the “counter” will get printed on screen. The critical part of the code block is the increment operator (++). The increment operator increases the value of the “counter” by 1. So, every time the code block gets executed, the value of the “counter” keeps increasing by 1. When it becomes 6, the while loop stops executing and Perl moves on to the next part of the program.

Let’s write a slightly more complicated program, using the for conditional loop inside the body of the while loop:

#!/usr/bin/perl
$counter = 0;
while ($counter <= 5) {
if ($counter = = 3) {
print “The counter is now at 3”;
next;
}
print "$counter<br />";
}
continue {
$counter ++;
};
print "You have now exit the loop";

Output:

1
2
The counter is now at 3
4
5
You have now exit the loop

In this script, we have used the “next” and “continue” functions. The next function lets Perl skip to the next iteration. The continue function will contain code that is to be executed before the condition is evaluated again. What we have done is interrupted the “counter” (and the loop) when it reaches 3. At 3, we printed the statement “The counter is now at 3”. Then, using the next function, we were able to get back to the loop again. The continue statement, even if it was outside the body of the while loop, was used to increment the value of the counter by 1.

You will understand these scripts better if you try them out for yourself. As an aside, you may also want to check out other scripting languages. This course covers the basics of bash scripting and Python. This will help you compare and choose the best scripting language for your needs.