Programming Basics

  Home arrow Programming Basics arrow Page 2 - Control Flow Constructs: the For and F...
PROGRAMMING BASICS

Control Flow Constructs: the For and Foreach Loops
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2012-03-21

    Table of Contents:
  • Control Flow Constructs: the For and Foreach Loops
  • The foreach Loop

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Control Flow Constructs: the For and Foreach Loops - The foreach Loop


    (Page 2 of 2 )

    Perl has another loop called the foreachloop. It is used to loop through lists and arrays. We will talk about arrays in the next chapter, but since we have seen examples of a list, we can look at theforeachloop processing a list of numbers:

    #!/usr/bin/perl -w
    # foreach.pl

    use strict;

    my $number;

    foreach $number (1 .. 10) {
    print "the number is: $number\n";
    }

    Theforeachloop executes the body of the loop (theprint()function in this example) for each number in the list.$numberis called the loop control variable, and it takes on the values in the list, one at a time. Recall that(1 .. 10)is shorthand for(1, 2, 3, 4, 5, 6, 7, 8, 9, 10). This code produces this result:

    $ perl foreach.pl
    the number is: 1
    the number is: 2
    the number is: 3
    the number is: 4
    the number is: 5
    the number is: 6
    the number is: 7
    the number is: 8
    the number is: 9
    the number is: 10
    $

    A note about the keywordsforandforeach: they are synonyms for each other. In other words, we can say

    foreach ($i = 1; $i <= 10; $i++)_ { .. }

    and

    for $number (1..10) { .. }

    foreach is rarely used in place offor, butforis often used instead offoreach. In the spirit of minimal confusion, we will spell outforeachwhen we have aforeachloop.

    We will talk more aboutforeachin the next chapter when we discuss the array data type.

    do .. while and do .. until

    When we were categorizing our lists, we divided indefinite loops into those that execute at least once and those that may execute zero times. The whileloop we’ve seen so far tests the condition first, and so if the condition isn’t true the first time around, the “body” of the loop never gets executed. There’s two other ways to write our loop to ensure that the body is always executed at least once:

    do { action } while ( condition );
    do { action } until ( condition );

    Now we do the test after the block. This is equivalent to moving the diamond in our flow chart from the top to the bottom.

    Here is an example:

    #!/usr/bin/perl -w
    # dowhiledountil.pl

    use strict;

    my $i = 1;

    print "starting do...while:\n";
    do {
    print " the value of \$i: $i\n";
    $i++;
    } while ($i < 6);

    $i = 1;

    print "starting do...until\n";
    do {
    print " the value of \$i: $i\n";
    $i++;
    } until ($i >= 6);

    Executing this program produces the following:

    $ perl dowhiledountil.pl
    starting do...while:
    the value of $i: 1
    the value of $i: 2
    the value of $i: 3
    the value of $i: 4
    the value of $i: 5
    starting do...until
    the value of $i: 1
    the value of $i: 2
    the value of $i: 3
    the value of $i: 4
    the value of $i: 5
    $

    The importance of thedo..whileanddo..untilloop is that the body of the loop is always executed at least once.

    Please check back next week for the continuation of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP

    Developer Shed Affiliates

     



    © 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap