The “while” Loop Keyword in Perl : An Introduction

perlstringfunctionsPerl is a general purpose, dynamic, highly capable programming language developed by Larry Wall. Perl’s family includes Perl 5 and Perl 6. Developed in 1987, PERL stands for Practical Extraction and Report Language. Though Perl has borrowed quite a lot of features from other languages such as C, it continues to be very popular among programmers since it is better than other programming languages in many aspects. Perl 5 is widely used in network programming, graphics programming and system administration. Perl has features for supporting object-oriented programming models and complex data structures.

Looping in PERL:

Computers are always great for performing repetitive tasks. All programming languages provide certain ways for handling iterative tasks. PERL language comes with a variety of loops such as “for”, “while”, “do..while” and many more.

The “While” loop is a control flow statement that iterates in a code based on a particular Boolean condition. A repetitive “if” statement can be considered as a while loop. The syntax below will help you understand it better:

New to Perl? Take an online class at Udemy.com

General syntax:

While (expression)

{

Your code

……

}

Example:

Use featureqw/say/;

# print numbers from 1 to 5

$c=1;

While($c<=5)

{

Say “$c”;

$c++;

}

Let’s examine the code mentioned above. Note that “say” that works in the same manner as “printf” is imported here. However, the “say” command comes with one additional feature since it adds a new line into the printed string. After that, a variable “c” with value 1 is initialized. The “while” loop will start and check if the variable “c” is lesser than or equal to 5. If this condition is false then the loop will terminate; however, if it’s true, the loop will continue and will print the value of c as 1. Perl will increase the value of “c” by 1 and again go to the condition part and check the condition. This will continue till the value of “c” becomes greater than 5 and when it does, the loop will terminate and you will have the values printed from 1 to 5.

Similarly, you can print the values in decreasing order as shown below:

Use featureqw/say/;

# print numbers from 5 to 1

$c=5;

While($c>=1)

{

Say “$c”;

$c--;

}

The above shown code will print the values from 5 to 1.

Arrays in while loop:

Use featureqw/say/;

my @interest=(‘skating’,’surfing’,’anime’,’movies’); //defining the array

$i=0; // counts every element of array

While($interest[$i])

{

say $interest[$i];

$i++;}

Output:

skating

surfing

anime

movies

In the above example, Perl will iterate through every element of the array and will assign the current element value to the $interest. When the loop stops, the scope of $interest will run out and it will be added to the garbage collection.

Using next command with while loop in PERL:

The next command stops the current iteration and moves to the next one.

Learn Perl Programming from scratch through Udemy.com

Example:

Use featureqw/say/;

# print numbers from 1 to 5 but skip 3

$c=1; //initializing counter

While($c<=5)

{

If($c==3)

{

say “skip three”

next; 

}

 

Say “$c”; //printing the counter

$c++; //increasing the counter by 1

}

Output:

1

2

Skip three

4

5

In the above example, when the value of “c” becomes equal to 3, the program will print the message “skip three” and the “next” command will stop the current iteration as shown in the output.

Infinite loop in while:

If the condition in your loop never becomes false for any reason, you will get an infinite loop. In this condition, the program gets stuck in a block of code and thus it can never escape. If it happens unintentionally, it will be considered as a bug. However, the infinite loop is also used intentionally sometimes and that makes the program easier to read and write. If you want the infinite loop, you can use any condition that stays true at all times. Consider the example shown below:

While(1)

{

Your code

…

}

In the above example the condition will always be true.

There are many ways to stop infinite loops. The most common way is to call the “last” statement within the while loop. The last statement will not check the condition repeatedly and simply skips the rest of the block. It can be well explained by the code shown below:

Example:

use strict;

use warnings;

use 5.010;

 

while (1) {

print "who is your favorite Tennis player ? ";

my $player_name = <STDIN>;

chomp$player_name;

if ($player_nameeq 'Nadal') {

last;

  }

say 'Wrong answer! Let’s Try again!';

}

say 'you are right';

In the above example, the user is asked a question. The user will be stuck with the question forever if he doesn’t type the answer ‘Nadal’.

The conversation might look like:

who is your  favorite tennis player ?

>Sampras

Wrong answer! Let’s Try again!

who is your  favorite tennis player ?

>Agassi

Wrong answer! Let’s Try again!

who is your favorite Tennis player ?

>Nadal

you are right

In this example we have used “use strict” which is known as pragma. “use strict” will allow you to declare all variables(“strict vars”) and Perl will not mistake your intentions when you use subs(“strict subs”). Note that “<STDIN>” is also used and it will take the user input. In this particular example, you can see that as the user types the correct answer the “last” statement will be called and the loop will terminate.  The execution will continue after the loop.

Learn more about Perl by taking an online class at Udemy.com

Conclusion:

The while loop is easy to read and simple to write when compared to the other loops. However, most programmers prefer to use “for” loop instead of “while” loop. Again, a programmer can use “for” or “while” loop depending on the situation. The  while loop continues its execution till the specified condition is met and thus, it is popular among newbies who try to achieve the infinite loop. Therefore, it is better to use “while” loop in a situation where the infinite loop is needed.