Professional Documents
Culture Documents
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!
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 />";
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;
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.
* Go Back
* Continue
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:
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
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
# SET A VARIABLE
$count = 0;
while ($count <= 7) {
}
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
# DEFINE AN ARRAY
@names = qw(Steve Bill Connor Bradley);
while.pl:
1 Steve
2 Bill
3 Connor
4 Bradley
* 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