Programming Basics

  Home arrow Programming Basics arrow Loops and Control Flow Constructs

Loops and Control Flow Constructs
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0

    Table of Contents:
  • Loops and Control Flow Constructs
  • Looping Until



    Loops and Control Flow Constructs

    (Page 1 of 2 )

    In this sixth part of a nine-part series on Perl control flow structures, we continue our study of loops. This article is excerpted from chapter three of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

    Infinite Loops

    The important but obvious point is that what we’re testing gets changed inside the loop. If our condition is always going to give a true result, we have ourselves an infinite loop. Let’s just remove the second of those two statements:

    #!/usr/bin/perl -w

    use strict;

    my $countdown = 5;

    while ($countdown > 0) {
    print "Counting down: $countdown\n";

    $countdownnever changes. It’s always going to be 5, and 5 is, we hope, always going to be more than 0. So this program will keep printing its message until you interrupt it by holding down Ctrl and C. Hopefully, you can see why you need to ensure that what you do in your loop affects your condition.

    Should we actually want an infinite loop, there’s a fairly standard way to do it. Just put a true value—typically
    1—as the condition:

    while (1) {
    print "Bored yet?\n";

    The converse, of course, is to saywhile (0)in the loop’s declaration, but nothing will ever happen because this condition is tested before any of the commands in the loop are executed. A bit silly really.

    More Programming Basics Articles
    More By Apress Publishing

    blog comments powered by Disqus


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