Bootstrap FreeKB - Perl (Scripting) - while loop
Perl (Scripting) - while loop

Updated:   |  Perl (Scripting) articles

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 Buy Me A Coffee



Comments


Add a Comment


Please enter 0a2089 in the box below so that we can be sure you are a human.