Previous
Operators 
Next
For and Foreach 
Beginner's Tutorial for CGI using Perl Language
If

The Flow of Control

Every windows user knows the flow of control. It is usually Control-Alt-Delete. But in programming we have the control. Let us get acquainted with some loops and conditions.

If condition

I don't know what would have become of the computer industry if the 'if' condition was not there. I don't think that there is one proper program without the if condition - except for those tiny 'Hello World' programs.

Syntax:
if ( condition ) {
statement
}
elsif ( condition ) {
statement
}
else {
statement
}

The operators that we discussed in the previous chapter is of much use in if conditions.
if ( $i == 5) { statement; }
will execute the statement if the variable '$i' has the value 5.

The 'If' condition checks the condition and if it is true, then it will execute the statement. If not it will check the next 'elsif' condition. If no given conditions are true, it will execute the statement in else(if 'else' is present).

On to our first example....

#!/usr/local/bin/perl

print "Here's some descriptions of airplanes run by various OS...\n";
$OS = "DOS"; #Change this to different OS Names

print "Our OS is $OS. So our plane will be like this...\n";
if ( $OS eq "DOS" ) { 
print "Everybody pushes it till it glides, then jumps on and lets it
coast till it skids, then jumps off, pushes, jumps back on, etc.";
}
elsif ( $OS eq "Macintosh") {
print "All the flight attendants, captains and baggage handlers
look the same, act the same and talk the same. Every time you ask a
question, you are told you don't need to know, don't want to know and
everything will be done for you without your knowing, so just shut up.";
}
elsif ( $OS eq "Windows") {
print "Colorful airport terminal, friendly flight attendants, easy
access to a plane, uneventful takeoff. Then: BOOM! You blow up without
any warning whatsoever.";
}
elsif ( $OS eq "Unix") {
print "Everyone brings one piece of the plane. Then they go on the
runway and piece it together, all the while arguing about what kind of
plane they're building.";
}
else {
print "General Error: No Plane.";
}

You would have noticed that the operator used is 'eq' and not '=='($OS eq "Unix" and not $OS == "Unix"). This is because we are comparing stings - not numbers. For numbers, '==' operator must be used.

Previous
Operators 
Next
For and Foreach 

Comments

Anonymous at 29 Oct, 2007 03:42
How do i extract data between two html tags within a file to different file? i
i.e can i use if ($line = (/\\<html>\../\s\<html>\){
print $line.
}
i need help??
Reply to this.
Anonymous at 14 Nov, 2007 03:54
Hi there,

I am completely new to perl. I was browsing on the web looking for good tutorial and yours is exactly what I was looking for. Well Done! Thank you very much.

The explanation of the if conditions with the plains and the OS is simply hilarious.

Thanks again,
Alex
Reply to this.
Anonymous at 18 May, 2008 08:15
ok so lets say i wanted to do something that counts the number of checkboxes that are checked on a page and then when you hit submit it shows the number that were checked. does this use the if line?
Reply to this.
Comment


Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed