Perl (Scripting) - while loop

by
Jeremy Canfield |
Updated: July 08 2024
| Perl (Scripting) articles
A while loop can be used to do something until a certain condition is true. For example.
#!/usr/bin/perl
use strict;
use warnings;
my $attempt = 0;
my $max_attempts = 5;
while ($attempt < $max_attempts) {
print "$attempt is less than $max_attempts \n";
++$attempt;
}
Or this.
#!/usr/bin/perl
use strict;
use warnings;
my $attempt = 0;
my $max_attempts = 5;
do {
print "$attempt is less than $max_attempts \n";
++$attempt;
} while ($attempt < $max_attempts);
. . . will produce the following output.
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
Did you find this article helpful?
If so, consider buying me a coffee over at