The Basics - Control Structures


We have seen that most of the operators that compare and contrast numbers are similar to that of other languages, now lets see them in action in some control structures. Control structures are constructs that are meant to direct the flow of a program in one way or another, usually based on conditions inside of the program. Perl has the C and JAVA style control structures, but it also has a few that may be new to everyone.

The first, and probably most important, is the if statement. It is similar to that of the JAVA and C if statements, and also includes the ability to have elsif (note how that is spelled) and else clauses. Here is a sample of what it does:

      #!/usr/bin/perl -w

      $number1 = 5;
      $number2 = 7;

      if($number1 < $number2) {
        print "Number 1: ", $number1, " is < Number 2: ", $number2, "\n\n";
      }

      $number1 *= 2;

      if($number1 > $number2) {
        print "Number 1: ", $number1, " is > Number 2: ", $number2, "\n\n";
      }
      else {
        print "Number 1: ", $number1, " is < Number 2: ", $number2, "\n\n";
      }

      $number1 = $number2;

      if($number1 < $number2) {
        print "Number 1: ", $number1, " is < Number 2: ", $number2, "\n\n";
      }
      elsif($number1 > $number2) {
        print "Number 1: ", $number1, " is > Number 2: ", $number2, "\n\n";
      }
      else {
        print "Number 1: ", $number1, " is == Number 2: ", $number2, "\n\n";
      } 
      
The output of this little script looks like so:
 
      Number 1: 5 is < Number 2: 7
                                                                                
      Number 1: 10 is > Number 2: 7
                                                                                
      Number 1: 7 is == Number 2: 7          
      

This works exactly as it does in the other major languages, but now comes a twist. There is another statement that is related to the if statement, and it is called the unless statement. An unless statement was created to make the thought process of logic a little bit easier. Sometimes it is hard to think in terms of nots and not-equal-to items. The unless is basically a 'not if' statement. Here it is in action:

      #!/usr/bin/perl -w

      $number1 = 7;

      if($number1 != 5) {
        print "Number 1: ", $number1, " does not equal 5.\n\n";
      }

      unless($number1 == 5) {
        print "Number 1: ", $number1, " does not equal 5.\n\n";
      } 
      
The output of this little script looks like so:
 
      Number 1: 7 does not equal 5.
                                                                                
      Number 1: 7 does not equal 5.          
      

Perl has three basic loop structures, two of which you have seen in major languages, and one that will new to most people. The first loop we will demonstrate is called the while loop, the second is the for loop, and the third is the foreach loop.

A while loop works like a C or JAVA loop, the test condition is on the top of the loop structure, and as long as the condition is true, the loop will operate on everything in the brackets. A for loop operates like a while loop when the condition is true, but also has the ability to define variables and perform operations. The foreach loop was designed to operate over a list of values, namely an array or hash. Here are the loops in practice:

      #!/usr/bin/perl -w
 
      $number1 = 10;

      while($number1 >= 1) {
        print $number1, "...";
        $number1--;
      }
      print "\n\n";

      for($number2 = 10; $number2 >= 1; $number2--) {
        print $number2, "...";
      }
      print "\n\n";

      @array = (10, 9, 8, "you get the idea");

      foreach $element (@array) {
        print $element, "...";
      }
      print "\n\n"; 

      print "Going Beyond The End Of The Array...\n";
      for($number3 = 0; $number3 <= 10; $number3++) {
        print $array[$number3], "...";
      }
      
      print "\n\n"; 
      
The output of this little script looks like so:
 
      10...9...8...7...6...5...4...3...2...1...
 
      10...9...8...7...6...5...4...3...2...1...
 
      10...9...8...you get the idea...
 
      Going Beyond The End Of The Array...
      Use of uninitialized value in print at code/./Control.pl line 77.
      Use of uninitialized value in print at code/./Control.pl line 77.
      Use of uninitialized value in print at code/./Control.pl line 77.
      Use of uninitialized value in print at code/./Control.pl line 77.
      Use of uninitialized value in print at code/./Control.pl line 77.
      Use of uninitialized value in print at code/./Control.pl line 77.
      Use of uninitialized value in print at code/./Control.pl line 77.
      10...9...8...you get the idea........................
         
      

Remember that if we wish to access a specific element in an array or hash, we need to access it by using a $ notion. In the foreach loop we create a temporary scalar variable that we use to do the 'stepping' through the array or hash. If you choose to use a for or while loop to step through the array or hash you run the risk of falling off of the end (as seen above); use the foreach loop whenever possible.

For historical (or masochistic) reasons Perl includes a couple of old friends from the fortran era: goto statements, blocks, and labels. It is possible to construct basic control structures form these building blocks:

      #!/usr/bin/perl -w
 
      $number1 = 10;

      LOOP:	{
	      print $number1, "...";
	      $number1--;
	      if($number1 >= 1) {
	        goto LOOP;
	      }
      }
	
      print "BOOM\n\n"; 
      
The output of this little script looks like so:
 
      10...9...8...7...6...5...4...3...2...1...BOOM          
      

Perl does not feature a switch construct mainly because it doesn't need one. It is easy to construct such a statement by using a combination of if statements, labels, and goto statements:

      #!/usr/bin/perl -w
 
      $number = 1;
 
      SWITCHLOOP:  {

        SWITCH:  {
	
          if($number == 1) {
	    print "Number 1\n"; 
	    last SWITCH;
	  }
        
	  if($number == 2) {
	    print "Number 2\n"; 
	    last SWITCH;
	  }
        
	  if($number == 3) {
	    print "Number 3\n"; 
	    last SWITCH;
	  }
        
	  print "Number Not Found\n";
        }

        $number++;
  
        if($number <= 4) {
          goto SWITCHLOOP;
        }
  
      }

      print "End\n"; 
      
The output of this little script looks like so:
 
      Number 1
      Number 2
      Number 3
      Number Not Found
      End         
      

Now that we are familiar with the basics, lets move on to more pressing issues.


Variables 2    <Control>    Files

Created By: Jason Zurawski
Last Modified: Feb. 29, 2004