PageResource.com - Web Development Tutorials

Perl Conditionals

How to use if, else, elsif and unless
We touched on the if condition in the last section, and here we are going to see how it works— along with adding else, elsif, and using the unless condition.

The if Condition

The if condition lets you check to see if a statement is true using a comparison operator. If the statement is true, it executes a block of code between two curly braces { }. Otherwise, it will go on to the next command after the braces. Here is a sample if statement:

$gas_money=5;

if ($gas_money < 10)
{
 print "You will not get much gas for less than 10 bucks!";
}

print "Gas is expensive.";

This checks to see if the variable $gas_money is less than 10. If this is true, it executes the statement inside the braces, and then goes on. If not, it will go straight to the print statement after the braces. So, with $gas_money being 5, it prints this result:

You will not get much gas for less than 10 bucks!
Gas is expensive.

If the $gas_money variable were equal to 20, it would skip the code inside the braces and just print the last line:

Gas is expensive.

Using else and elsif

You can also make the script do something if the "if" condition is not true. If you have just one other option, simply use an else statement after the if statement, like this:

if ($gas_money < 10)
{
 print "You will not get much gas for less than 10 bucks!";
}
else
{
 print "You have enough money for now.";
}

print "Gas is expensive.";

Now, if $gas_money is less than 10, the program gives you:

You will not get much gas for less than 10 bucks!
Gas is expensive.

However, if $gas_money is anything greater than 10, it will bypass the statement in the first set of braces and execute the code within the else statement braces. It would print this:

You have enough money for now.
Gas is expensive.

If you want to check for a couple of conditions, you can use elsif to do another check and leave the else code as a last resort. You can have as many elsif statements as you want, but keep them between the if and the else statements. Here is a sample:

if (($gas_money < 10) && ($gas_money > 0))
{
 print "You will not get much gas for less than 10 bucks!";
}
elsif ($gas_money <= 0)
{
 print "You need to get some money!";
}
else
{
 print "You have enough money for now.";
}

print "Gas is expensive.";

You'll notice the && operator here, it checks to see that two conditions are both true before it will return true. So, for the first bit of code to execute, $gas_money has to be between 1 and 9. Also, note the extra parentheses, they separate the condition from the comparisons within it.

Now, if $gas_money is between 1 and 9, you get:

You will not get much gas for less than 10 bucks!
Gas is expensive.

However, if $gas_money is zero or less, you get:

You need to get some money!
Gas is expensive.

Finally, if $gas_money is 10 or more you get:

You have enough money for now.
Gas is expensive.

You could check for several ranges here. Just keep using the elsif statement to check for more ranges and use the else statement last to catch anything leftover.

Using unless

The unless condition checks for a certain condition and executes it every time unless the condition is true. Sort of like the opposite of the if statement:

unless ($gas_money == 10)
{
 print "You need exact change. 10 bucks please.";
}

This shows the message every time unless $gas_money is actually equal to 10. It's a handy shortcut for a longer if/else statement under the right conditions.

Well, that's it for now. Let's go on to: Using Loops.


Copyright © The Web Design Resource. All rights reserved. | Contact Us | Privacy Policy