Perl

While Loop in Perl

Perl supports three types of loops like the other standard programming languages. The “while” loop is one of them. Generally, a counter variable is used to initialize the starting value of the counter that is used to start the “while” loop iteration, and this counter value is incremented or decremented to reach the termination condition of the loop. Different uses of the “while” loop in Perl are shown in this tutorial.

Syntax:

The syntax of the “while” loop is given in the following. The block of script is iterated until the termination condition of the loop arises:

while (termination_condition)

{

  <--Block of Perl Script-->

  <--Modify the counter or set termination condition-->

}

Different Perl “While” Loop Examples

Different uses of the “while” loop are shown in this part of the tutorial.

Example 1: Print the Odd Number in Ascending Order

Create a Perl file with the following script that prints all odd numbers from 1 to 15 using a “while” loop. A counter variable is used in the script to start the iteration of the loop and the counter variable is incremented by 2 in each iteration of the loop to print the odd number.

#!/usr/bin/perl

use strict;
use warnings;

#Initialize the counter variable
my $counter = 1;

print "Odd numbers between 1 and 15 are:\n";

#Iterate the loop until the counter value is more than 15
while( $counter <= 15 )
{
    #Print the odd number
    print "$counter ";
    #Increment the counter by 2
    $counter += 2;
}

#Add a new line
print "\n"

Output:

The following output appears after executing the script. The odd numbers from 1 to 15 are 1, 3, 5, 7, 9, 11, 13, and 15 which are printed in the output:

Example 2: Print All Even Numbers in Descending Order

Create a Perl file with the following script that prints all even numbers from 1 to 14 in descending order. Here, the counter variable is initialized to 14 to start the loop and the counter is decremented in each iteration of the loop.

#!/usr/bin/perl

use strict;
use warnings;
use 5.34.0;

#Initialize the counter variable
my $counter = 14;

say "All even numbers between 1 and 15 in descending order are:";

#Iterate the loop until the counter value is more than 15
while( $counter >= 1 )
{
    #Print the even number
    print "$counter ";
    #Increment the counter by 2
    $counter = $counter - 2;
}

#Add a new line
print "\n"

Output:

The following output appears after executing the script. The even numbers from 1 to 14 in descending order are 14, 12, 10, 8, 6, 4, and 2 which are printed in the output:

Example 3: Using the Infinite “While” Loop

Create a Perl file with the following script that uses an infinite “while” loop to take input from the user and the loop is terminated when the user types “none”.

use strict;
use warnings;
use 5.10.0;

my $init = 1;
my $languages = '';

#Declare the infinite loop that will be terminated when
#the user will type 'none'
while ($init) {
       say "Enter your favorite programming language (Type 'none' to quit): ";
       
       #Take input from the user
       my $lang = <>;
       #Remove newline
       chomp $lang;
       
       #Check the input value of the user
       if ($lang eq 'none')
      {
    say "You have typed $languages and none.";
            #Exit from the loop if the user types 'none'
    last;
       }
       else
      {
    $languages .= $lang.", ";
       }

}

Output:

The following output appears after executing the script. Four input values are taken as input values which are “PHP”, “Python”, “Perl”, and “none”:


Example 4: Calculate the Sum of All Input Values

Create a Perl file with the following script that takes the numeric input from the user until the user types “0”. The sum of all input values is calculated in each iteration of the loop and the sum value is printed after terminating the loop.

#!/usr/bin/perl
use warnings;
use strict;

#Declare variable to take the input value
my $number;
#Declare variable to store the sum of input values
my $sum = 0;


print "Type '0' to terminate from the program.\n";
print "Enter the input numbers:\n";

#Take input from the user until '0' is taken from the user
while($number = <STDIN>)
{
     #Remove the newline
     chomp $number;
     if($number == 0)
    {
            #Terminate from the loop
    last;
    }
    else
    {
            #Calculate the sum of the input values
    $sum += int($number);
    }
}

print "The sum of all input numbers is $sum.\n";

Output:

The following output appears after executing the script. Five input values are taken as input which are 4, 2, 5, 8, and 0:

Conclusion

The “while” loop can be used for multiple purposes in the Perl programming. The uses of the “while” loop for finite and infinite times are shown in this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.