Home arrow Perl Programming arrow Page 4 - Perl Conditionals

The While Loop - Perl

While Perl truly is the programming language of the slacker, the bored, and the (okay I'll say it with great lamentation) creative, it is also a harsh mistress. But let's face it: as a computer programmer, you are luck to have a mistress at all. So chin up. Perl doesn't just freely give you love. There are conditions, or, more precisely, conditionals, which is the topic of this fourth part in a series.

TABLE OF CONTENTS:
  1. Perl Conditionals
  2. You Better or Else!
  3. The Unless Statement
  4. The While Loop
By: James Payne
Rating: starstarstarstarstar / 6
October 16, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The While Loop loops until a condition you give it is false.


#!/usr/bin/perl


$count= 1


while ($count<10)

{

print "$countn";

$count++;

}

The above code will, like the code before it, print out the numbers 1 through 9, stopping once the value becomes equal to ten and the statement is false.

Foreach There is Another!

Our final Loop is known as the Foreach Loop and is used mostly with arrays.


#!/usr/bin/perl


@breakfast = ("Sausage", "Eggs", "Bacon", "Grits", "Toast" "Hash");


print "My breakfast:nn";


foreach $breakfast (@breakfast)

{

print "$breakfastn";

}

This code access the values in your array and prints them one by one:

  Sausage

  Eggs

  Bacon

  Grits

  Toast

  Hash

And that is it for Loops and Conditionals. In my next tutorial, I will go over a few more statements, and we will learn to work with files -- creating them, reading them, writing them, you name it.

So go forth, young nerdling, and use your Perl wisdom for great deeds. Till then...



 
 
>>> More Perl Programming Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: