Tinkering with Perl

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  37  38  39  40  41  42  43  44  45  46  Next  Skip Forward
Printer-Friendly Version

While loops

A while loop is a loop used to run a block of code while a condition is true. For example:

$ShouldContinue = "y";
$NumberOfTimesThroughLoop = 0;
while (not($ShouldContinue == "n"))
    {
    ++$NumberOfTimesThroughLoop;
    if ($NumberOfTimesThroughLoop == 1)
	{
	print "This loop has been executed $NumberOfTimesThroughLoop time.\n";
	}
    else
	{
	print "This loop has been executed $NumberOfTimesThroughLoop times.\n";
	}
    print "Go through the loop again(y/n)? ";
    $ShouldContinue = <>;
    chomp $ShouldContinue;
    }

Now, let's look at what this code does.

$ShouldContinue tells if the loop should continue. "n", for no, means to stop; anything else means to continue. So the loop says, "While we should continue".

The variable $NumberOfTimesThroughLoop is value of $NumberOfTimesThroughLoop by one. In general, the expression:

++$variable name;

(where variable name is the name of a scalar variable) means "Increase the value of variable name by one."

Then we have a conditional. This conditional sees if it's the first time through the loop. Why do we do that?

The statements inside the if clause (the block immediately following the if) and the else clause are almost identical -- but there is an 's' after "time" in the else clause. This is so that, on the first time through, the program will print out:

This loop has been executed 1 time.

but on the second (third, fourth, etc.) time through, it will say something like:

This loop has been executed 2 times.

Then, after that, it asks the user if he wants to run through the loop again. After doing that, it reads a line of input into the variable $ShouldContinue -- if the user types "y", $ShouldContinue will contain a "y", and if the user types "n", $ShouldContinue will contain a "n".

Then, after that, it checks if it should continue (the conditional clause right after the word "while"), and if it should, it executes the loop again.

See also:

Statements - Flow control - Blocks - Conditional clauses - If-then-else - Loops - For loops

Tinkering with Perl is a free book that provides an introduction to programming in Perl, as well as a basic reference for things like foreach in Perl, if-then, and if-then-else, in addition to providing a glossary where you can find definitions for concatenate and other terms.

Tinkering with Perl may be one of the most popular offerings on this site, but it's not the only attraction. You can read a tongue-in-cheek Game Review: Meatspace, read an even more offbeat customer service survey (whether or not you actually fill it out), and spend a few minutes wishing your boss would read, The Administrator Who Cried, "Important!" (Not to mention that there are other things you can read here besides tech stuff, from Janra Ball: The Headache to The Spectacles.)

Read more...

Top

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  37  38  39  40  41  42  43  44  45  46  Next  Skip Forward
Printer-Friendly Version