SunQuest
 
       Perl
  Home arrow Perl arrow Page 4 - 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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
PERL

Perl 101 (Part 3) - Looping The Loop
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Perl 101 (Part 3) - Looping The Loop - Dos And Don'ts


    (Page 4 of 9 )

    The two control structures explained above test the conditional expressionfirst, and only proceed to execute the statements within the loop if theexpression evaluates to true. However, there are often occasions when youneed to execute a particular set of statements at least once before youcheck for a valid conditional expression.

    Perl has a solution to this problem too - it offers the "do-while" and"do-until" constructs, which allow you to execute a series of statements atleast once before checking the conditional expression. Take a look:

    do { do this! } while (condition)
    or

    do { do this! } until (condition)
    In this case, Perl will only test for the condition *after* executing theloop once.

    #!/usr/bin/perl # weighted employee evaluation program version 2.0 # call me WEEP II! # use DO to ensure that the statements # within the loop are executed at least once do { print ("Can we put you in a cubicle, cancel all your benefits, and pay you less than minimum wage? [y/n] "); $reply = <STDIN>; chomp($reply); } while($reply ne 'y'); print ("Heh heh! This company couldn't do without employees like you!\n");
    As you can see, the "do" construct can help to make your code more compact- compare WEEP version 2.0 with the original above.

    This article copyright Melonfire 2000. All rights reserved.

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


     

       

    PERL ARTICLES

    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway