Home arrow Perl Programming arrow Page 3 - Perl Conditionals

The Unless Statement - 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 Unless Statement is the opposite of the If statement in that it asks if a condition is false, and executes the code unless the condition is true.


#!/usr/bin/perl


$my_iq = 1000;

$your_iq = 90;


unless ($your_iq > $my_iq)

{

print "My brain is enormous and far superior to your own!";

}

In the above code, the program will print out the sentence: "My brain is enormous and far superior to your own!" every time, unless $you_iq is greater than $my_iq. 

Getting Loopy

Near the end of the day I always get a little loopy; I can't think straight and words just come out wrong. But I'm getting off topic here. Loops in code are something completely different.

The purpose of a loop is to loop a piece of code so that you don't have to type it over a billion times. Let's take a look at the For Loop:


#!/usr/bin/perl


for ($count =1; $count<10; $count++)

{

print "I R THE GREATEST!n";

}

The above code created a variable name $count and assigned an initial value of 1. It also has a part that tells it to increment the value of $count by +1 ($greatness++) and to do so until the value of $count is 10 or greater ($count<10). So long as the value of $count is less than 10, the following will print to the screen:

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

  I R THE GREATEST!

If we were working with numbers we could have also just assigned a numeric value to $count and have it do this:


#!/usr/bin/perl


for ($count =1; $count<10; $count++)

{

print "$countn";

}

That would print out the following to your screen:

  1

  2

  3

  4

  5

  6

  7

  8

  9

The For Loop loops for the number of times you tell it to.



 
 
>>> 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: