You are on page 1of 21

Conditional statements may involve logical operators and usually test equality or

compare one value to another. Here's a look at some common conditional statements.
Remember that the code to be executed will only execute if the condition in parentheses
is met.
ifs.pl:

#!/usr/bin/perl

# HTTP HEADER
print "content-type: text/html \n\n";

# SOME VARIABLES
$x = 7;
$y = 7;

# TESTING...ONE, TWO...TESTING
if ($x == 7) {
print '$x is equal to 7!';
print "<br />";
}
if (($x == 7) || ($y == 7)) {
print '$x or $y is equal to 7!';
print "<br />";
}
if (($x == 7) && ($y == 7)) {
print '$x and $y are equal to 7!';
print "<br />";
}

if.pl:
$x is equal to 7!
$x or $y is equal to 7!
$x and $y are equal to 7!

These operators are discussed thoroughly in our PERL Operators lesson.


PERL - If Else

The else statement is a perfect compliment to an if statement. The idea behind them is
that if the conditional statement is not met then do this. In hypothetical, plain english, "If
this is true do this, if not do this."
ifelse.pl:
#!/usr/bin/perl

# HTTP HEADER
print "content-type: text/html \n\n";
# SOME VARIABLES
$name = "Sarah";

$x = 5;

all television
stations
broadcasting
workers
arriving in
Indonesia
following the
movements
involved Teru
adi want to see
hear the
television to
hear music
everywhere
want out of the
room to the
bedroom to
sleep wants to
take a shower
go wherever
our leaders
commander
television
broadcasters are
eager to
participate
pemimpinnnya
robert adhi
Kusuma's son
# IF/ELSE STATEMENTS
if ($x > 10) {
print "$x is greater than 10!";
} else {
print "$x is not greater than 10!";
}
print "<br />";

# STRINGS ARE A LITTLE DIFFERENT


if ($name eq "Sarah") {
print "Hello, $name!";
} else {
print "You are not $name!";
}

PERL - Elsif Statements

Elsif statements test another conditional statement before executing code. This way
multiple conditions can be tested before the script continues.
elsif.pl:

#!/usr/bin/perl

# HTTP HEADER
print "content-type: text/html \n\n";

# SOME VARIABLES

$x = 5;

# PLAY THE GUESSING GAME


if ($x == 6) {
print "X must be 6.";
}
elsif ($x == 4) {
print "X must be 4.";
}
elsif ($x == 5) {
print "X must be 5!";
}

We could take this example a step further, adding an else statement at the bottom. This
would serve as a catch all if none of the conditions were met.

Bookmark and Share

* Go Back
* Continue

Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving
with time!

Web Reference
HTML Reference CSS Reference CSS Examples PHP Examples Help Tizag Grow
Link to Tizag
New - Tizag.com Forums!
Recent Forum Topics:

- table border colors


- Wanna Make Money, Its Very Simple... Here's A Way!!
- Getting this code to work
- What do you think/and little problem.

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials While loops continually iterate as long as the
conditional statement remains true. It is very easy to write a conditional statement that
will run forever especially at the beginner level of coding. On a more positive note, while
loops are probably the easiest to understand. The syntax is while (coditional statement)
{ execute code; }.
Advertise on Tizag.com
whilecounter.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n";


# SET A VARIABLE
$count = 0;

# RUN A WHILE LOOP


while ($count <= 7) {
# PRINT THE VARIABLE AND AN HTML LINE BREAK
print "$count<br />";
# INCREMENT THE VARIABLE EACH TIME
$count ++;
}

print "Finished Counting!";

whilecounter.pl:
0
1all television
stations
broadcasting
workers
arriving in
Indonesia
following the
movements
involved Teru
adi want to see
hear the
television to
hear music
everywhere
want out of the
room to the
bedroom to
sleep wants to
take a shower
go wherever
our leaders
commander
television
broadcasters are
eager to
participate
pemimpinnnya
robert adhi
Kusuma's son
2
3
4
5
6
7
Finished Counting!
PERL - Next, Last, and Redo

Outlined below are several interrupts that can be used to redo or even skip iterations of
code. These functions allow you to control the flow of your while loops.

Next
Place it inside your loop and it will stop the current iteration and go on to the next one.
Continue
Executed after each loop iteration and before the conditional statement is evaluated. A
good place to increment counters.
Last
Last stops the looping immediately (like break)
Redo
Redo will execute the same iteration over again.

flowcontrol.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n";

# SET A VARIABLE
$count = 0;
while ($count <= 7) {

# SET A CONDITIONAL STATEMENT TO INTERRUPT @ 4


if ($count == 4) {
print "Skip Four!<br />";
next;
}
# PRINT THE COUNTER

print $count."<br />";

}
continue {
$count++;
};
print "Loop Finished!";

flowcontrol.pl:
0
1
2
3
Skip Four!
5
6
7
Finished Counting!
Above, we skip the fourth iteration by incrementing the variable again. In the example we
also print a line, "Skip Four!" just to make things easier to follow.
PERL - While Array Loop

Here we are just showing a method of looping through an array using a while loop. We
use three variables to do this including: the array, a counter, and an index number so that
each time the while loop iterates we also loop through each index of the array.
whilearrayloop.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n";

# SET UP AN HTML TABLE


print "<table border='1'>";

# DEFINE AN ARRAY
@names = qw(Steve Bill Connor Bradley);

# COUNTER - COUNTS EACH ROW


$count = 1;

# COUNTS EACH ELEMENT OF THE ARRAY


$n = 0;

# USE THE SCALAR FORM OF ARRAY


while ($names[$n]) {
print "<tr><td>$count</td><td>$names[$n]</td></tr>";
$n++;
$count++;
}
print "</table>";

while.pl:
1 Steve
2 Bill
3 Connor
4 Bradley

Bookmark and Share

* Go Back
* Continue
Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving
with time!

Web Reference
HTML Reference CSS Reference CSS Examples PHP Examples Help Tizag Grow
Link to Tizag
New - Tizag.com Forums!
Recent Forum Topics:
- table border colors
- Wanna Make Money, Its Very Simple... Here's A Way!!
- Getting this code to work
- What do you think/and little problem.

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials

2003-2008 Erack Network | Copyright | Privacy Policy | Advertising Information


Site design by Seattle Web Design

You might also like