Previous
For and Foreach
For and Foreach
Next
Functions or Subroutines
Functions or Subroutines
Beginner's Tutorial for CGI using Perl Language
While
While
While loop
Now we arrive at the while loop. A loop not to be taken lightly. If you ever see your program perpetually executing something, a while loop is most likely the culprit. I have lost count how many times I have stared at the screen when it was scrolling lines and lines of data endlessly forever when only a couple of times was necessary. You guessed it - a badly written while loop.
Syntaxwhile ( condition ) {
body
}
Execute script repeatedly as long as a given condition is true. OK! Here comes the example...
#!/usr/local/bin/perl
print "Top 10 Surprises in Windows 2000\n\n";
#Make a list of surprises
my @surprises = ( "You get $10 off if you voluntarily give them your soul,
but it's full price if they have to force it out of you.",
"It is still possible to fix a sandwich and something to drink while
waiting for Start Up.",
"When you peel off the label on the CD, there's Window'95 label under it.",
"To open Netscape : Press ctrl d, alt 4, tab, tab, spell Nebraska backwards
and press enter, enter your gender, compose a 500-word essay.... ",
"Entering \"Department of Justice\" into Organization Field during set-up
will crash system" ,
"New app monitors Bill Gates's wealth for you to see" ,
"Mix-up in shipping department sends a dog named \"Bowser\" with every copy" ,
"A \$1 off coupon for Mrs Smith's cream pie with Bill's home address on back" ,
"Includes sample bugs from upcoming Windows 2000 Advanced Server!" ,
"Surgeon General has put a warning on Solitaire that repeated use may be
habit forming.");
my $no=10;
while ($no > 0) {
print "Surprise $no : $surprises[$no-1]\n";
$no = $no - 1;
}