CMPS 335 Advanced Web Publishing Perl and CGI Programming


Conditional Statements and Loops


Decision-making statements are used a lot in programs.  Whenever a program has a choice of actions, a conditional statement is used.  Conditional statements are also known as branching statements.   Conditional statements contain conditional operators.

Conditional Operators

There are two major categories of conditional operators:

    1. Relational Operators (Comparison Operators)
    2. Logical Operators (Boolean operators)

A relational operator is used to create a simple condition (conditional expression) for comparing two numbers or two strings.  A logical operator is used to combine two simple conditions to create a more complex condition.
     Category                    Operator     
    ---------------------------------------------------------
     1. Relational Operators    
        a. Comparing numbers     ==, !=, <, >, <=, >=
        b. Comparing strings     eq, ne, lt, gt, le, ge
    
     2. Logical Operators        ||    (logical OR)                   
                                 &&    (logical AND)
                                 !     (logical NOT)  

Conditional Statements and Expressions

A conditional statement generally contains three parts: There are two types of conditional expressions: Conditional statements include: if, if else, if elsif, if elsif else, for, foreach, while, and until.

The if Family

The syntax of the if family of statements is:

    if (conditional expression) {
      block of statments
    }
    elsif (conditional expression) {
      block of statements
    }
    else {
      block of staements
    }  
The if family branching statements follow the zero, one, or many rule:
Note: Examples
  #!/usr/bin/perl

  if ($count) {
  print "Variable is undefined, the condition is true\n";
  }
  else {
  print "Variable is undefined, the condition is false\n";
  }

  if ($count == 0) {
    print "Variable is undefined, the count==0 condition is true\n";
  }
  else {
    print "Variable is undefined, the count==0 condition is false\n";
  }
  $count = 0;
  if ($count < 5) {
    print "Count is 0, the count < 5 condition is true\n";
  }
  else {
    print "Count is 0, the count < 5 condition is false\n";
  }
  $count = 4;
  if ($count == 5) {
    print "Count is 4, the count == 5 condition is true\n";
  }
  elsif ($count+1 == 5) {
    print "Count is 4, the count+1 == 5 condition is true\n";
  }
  else {
    print "The count is 4, neither count==5 nor count+1==5 is true\n";
  }
  $count = 6;
  if ($count == 5) {
    print "Count is 6, the count == 5 condition is true\n";
  }
  elsif ($count+1 == 5) {
    print "Count is 6, the count+1 == 5 condition is true\n";
  }
  else {
    print "Count is 6, neither count==5 nor count+1==5 is true\n";
  }         

  [jhu@cs cgi-bin]$ perl ifExamples.cgi
  Variable is undefined, the condition is false
  Variable is undefined, the count==0 condition is true 
  Vount is 0, the count < 5 condition is true
  Count is 4, the count+1 == 5 condition is true
  Count is 6, neither count==5 nor count+1==5 is true 

The foreach Loop

(a) For array processing
    Syntax:  foreach $arrayElement (@array) {
               block of statements
             }   
where $arrayElement is the name of the variable that will act as a stand-in for each member of the array, and @array is the name of the array.  Examples:
        @colors = ("red","green","blue");
        foreach $item (@colors){
           print "$item\n";
        }
        @digits = (1..10);
        number = 25;
        foreach $number (@digits){
          print $number ";
        }
        print "$number\n";      

(a) For hash processing
    Syntax:  foreach $key (keys %hash) {
               print $hash{$key};
             }   
where $key is the name of the scalar that will hold each individual key as you go through the loop.&nbap; keys is the keys function that creates a list of the keys from the %hash.  Examples:
        foreach $key (keys %ENV) {
          print "$key = $ENV{$key}\n;
        }    

The while and until Loops

The while loop executes while its conditional expression is true.  The until loop executes while its conditional expression is false.  In both cases, the loop test is performed before the loop is entered.
    Syntax:  while/until (conditional expression) {
               block of statements
             }   
Examples:
    
    [jhu@cs cgi-bin]$ cat loopExamples.cgi
    #!/usr/bin/perl
    
    @colors = ("red","green","blue");
    foreach $item (@colors) {
      print "$item\n";
    }
    
    @digits = (1..10);
    $number = 25;
    foreach $number (@digits) {
      print "$number ";
    }
    print "$number \n";

    $n = 5;
    while ($n < 10) {
      print "$n ";
      $n++;
    }# end while
 
    print "\n";
    $n = 5;
    until ($n > 10) {
      print "$n ";
      $n++;
    }# end until 
    
    [jhu@cs cgi-bin]$ perl loopExamples.cgi
    red
    green
    blue
    1 2 3 4 5 6 7 8 9 10 25 
    5 6 7 8 9   
    5 6 7 8 9 10 

Return to CMPS 335 Home Page
Return to Web Site Home Page