Programming Basics

  Home arrow Programming Basics arrow 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

    Control Flow Constructs: the For and Foreach Loops


    (Page 1 of 2 )

    In this seventh part of a  nine-part article series on the variety of control structures Perl programmers can use to set up the way a program gets to where it's going, we'll continue our study of loops with the for loop and the foreach loop. This article is excerpted from chapter three of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

    The for Loop

    Perl has a forloop, similar to the one found in C/C++/Java. Its syntax is

    for (init_expr; test_expr; step_expr) {
    action
    }

    The init_expr is done first and once. Then the test_expr is tested to be true or false. If true, the action is executed, then the step_expr is executed. Then the test_expr is tested to be true or false, etc.

    The most common use of aforloop is as an alternative way of writing awhileloop that might resemble this one:

    $i = 1;
    while ($i <= 5) {
    # do something important
    $i++;
    }

    This can be written in aforloop as

    for ($i = 1; $i <= 5; $i++) {
    # do something important
    }

    More Programming Basics Articles
    More By Apress Publishing

    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-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap