while loop statement
Perl while loop statement executes a code block repeatedly as long as the
test condition remains true. The test condition is checked at the beginning
of each iteration.
while (condition) {
# code block
}
If the condition evaluates to true, the code block inside while loop
executes.
At the beginning of each iteration, the condition is reevaluated. The loop is
terminated if the condition evaluates to false.
At some point in the loop, you have to change some variables that make the
condition false to stop the loop. Otherwise, you will have an indefinite loop
that makes your program execute until the stack overflow error occurs.
The while loop statement has an optional block: continue, which executes
after each current iteration. In practice, the continue block is rarely used.
If you want to execute a code block as long as the condition is false, you
can use until statement.
In case you want to check the condition at the end of each iteration, you use
the do…while or do…until statement instead.
To control the loop, you use the next and last statements.
Happy New Year Count Down program
#!/usr/bin/env perl
use warnings;
use strict;
my $counter = 10;
while ($counter > 0) {
print("$counter\n");
$counter--; # count down
sleep(1); # pause program for 1 second
if ($counter == 0) {
print("Happy New Year!\n");
}
}
<>You often use the while loop statement with the diamond operator <> to get
the user’s input from the command line:
#!/usr/bin/perl
use warnings;
use strict;
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";
-
First, assign the user’s input to the
$inputvariable using the diamond operator (<>). Because it doesn’t specify any filehandle for the diamond operator, Perl checks the special array@ARGV, which is empty in this case, hence instructs the diamond operator to read fromSTDINi.e., from the keyboard. -
Second, remove the newline character from the
$inputvariable using thechomp()function and convert$inputto an integer. -
Third, add the integer into the
@numberarray.
let’s take a look at the following example:
#!/usr/bin/perl
use warnings;
use strict;
my $i = 5;
print($i--,"\n") while ($i > 0);
The while loop statement is placed after another statement.
|
Perl evaluates the statements from right to left. |
It means that Perl evaluates the condition in the while statement at the
beginning of each iteration.
You use the while loop statement modifier only if you have one statement to
execute repeatedly based on a condition like the above example.