Perl
  Home arrow Perl arrow Page 5 - Perl 101 (Part 3) - Looping The Loop
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Perl 101 (Part 3) - Looping The Loop
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2000-06-15


    Table of Contents:
  • Perl 101 (Part 3) - Looping The Loop
  • While You Were Sleeping...
  • ...Or Until You Wake Up
  • Dos And Don'ts
  • For Pete's Sake!
  • Every Comedian Needs An Exit
  • Grade School
  • Playing With Friends
  • So Many Choices...

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Perl 101 (Part 3) - Looping The Loop - For Pete's Sake!
    ( Page 5 of 9 )

    Both "while" and "until" are typically used when you don't know for certain how many times the program should loop - in the examples above, for example, the program continues to loop until the user enters the right answer. But Perl also comes with a mechanism for executing a set of statements a specific number of times - and it's called the "for" loop:

    for (initial value of counter; condition; update counter) { do this! }
    Doesn't make any sense? Well, the "counter" here refers to a scalar variable that is initialized to a specific numeric value [usually 0 or 1]; this counter is used to keep track of the number of times the loop has been executed.

    Each time the loop is executed, the "condition" is tested for validity. If it's found to be valid, the loop continues to execute and the value of the counter is updated appropriately; if not, the loop is terminated and the statements following it are executed.

    Take a look at this simple example of how the "for" loop can be used:

    #!/usr/bin/perl for ($a=5;$a<12;$a++) { print("It's now $a PM. Too early!\n"); } print("Let's party!\n");
    Here's what the output looks like:

    It's now 5 PM. Too early! It's now 6 PM. Too early! It's now 7 PM. Too early! It's now 8 PM. Too early! It's now 9 PM. Too early! It's now 10 PM. Too early! It's now 11 PM. Too early! Let's party!
    How does this work? We've begun by initializing the variable $a to 5. Each time the loop is executed, it checks whether or not $a is less than 12; if it is, a line of output is printed and the value of $a is increased by 1 - that's where the $a++ comes in. Once the value of $a reaches 12, the loop is terminated and the line following it is executed.

    And, for something slightly more complex, take a look at our re-write of the factorial calculator above:

    #!/usr/bin/perl # factorials version 2.0 # ask for a number. print ("Gimme a number!\n"); # process it $number = <STDIN>; chomp($number); # use the FOR loop to calculate the factorial # note how we've initialized variables within the # loop itself - you can do this too! for ($factorial=1,$counter = $number; $counter > 1; $counter--) { $factorial = $factorial * $counter; } print ("The factorial of $number is $factorial.\n");


    This article copyright Melonfire 2000. All rights reserved.

     
     
    >>> More Perl Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek