Databases Reference
In-Depth Information
# String comparison
my $username="Ali";
if($username lt "N")
{
print "The username appears in the first half of the alphabet.\n";
}
With the
if...else
construct, we can have some code that is executed when the con-
dition is true, and other code that's executed when the condition is false:
if($username eq "Ali")
{
print "Hi Dad!\n";
}
else
{
print "Hello!\n";
}
If the
$username
variable has the value
"Ali"
, the
"Hi Dad!
" message will be displayed;
otherwise, the message “Hello!” is displayed instead.
To handle other possible conditions, we can use the
if...elsif...else
construct. For
example:
if($username eq "Ali")
{
print "Hi Dad!\n";
}
elsif($username eq "Sadri")
{
print "Hi Mom!\n";
}
else
{
print "Hello!\n";
}
If the
$username
variable has the value
"Ali"
, the
"Hi Dad!
message will be displayed
(and the later checks will not be performed); if the
$username
variable has the value
"Sadri"
, the
"Hi Mom!"
message will be displayed, and if neither condition is satisfied,
the
"Hello!"
message will be displayed.
We can combine conditions using the Boolean operators AND (
&&
), OR ||(
|| OR
), and
NOT (
!
). For example, we can print a message if two conditions are met:
# Combining conditions
my $temperature=19;
# Boolean AND
if( ($temperature > 18) && ($temperature < 35) )
{