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:
- Conditional key word
(if, for, foreach, do, ...)
- Conditional expression (Condition)
A conditional expression has a value that can be either
true or false.
- A block of one or more statements
The block of statements is executed depending on the true or
false value of the conditional expression.
There are two types of conditional expressions:
- Simple conditional expression (Relational expression, simple condition)
A simple condition consists of a relational operator
that compares two operands.
Examples: ($sum == 100), ($operator eq "add")
- Complex conditional expression (Logical expression, complex condition)
A complex condition can be created by using a logical operator to
connect two simple conditions
Examples: ($rate !=0 && $employee ne "")
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:
- Only one if keyword is allowed with an if conditional
statement.
- An if clause may have zero or one else clause.
- An if clause may have zero, one, or many elsif clauses.
- The else block of statements may have zero, one, or many lines
of code.
Note:
- The elsif clause must come before the else clause.
The elsif and else clauses are optional.
- If a conditional expression is true, the branching statement
takes
the true path, executing the true block of statements. When the
conditional expression is false, the branch statement takes the false
path, executing the first statement following the true block of
statements.
- While comparison operators are the simplest and most common way
to construct a condition, they are not the only way. You can
use any expression as a condition. If the expression is an empty
string (""), null, or evaluated as 0, it is considered to
be false. In all other cases, it is true.
An undefined variable in a conditional expression is treated
as 0 for numeric tests and an empty or null string for string tests.
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