Perl Condition Statement With An Example

10:55
Every programming language must have decision statement that’s the part that makes the scripting or programming language more robust and intelligent. When we talk about decision statement that first thing comes to our mind is If…else statement. We also take daily decision based in if logic only. So let’s see Perl condition statement and their types.
Perl Provide the following Condition statements:
  1. IF Statement
  2. IF…ELSE Statement
  3. IF…ELSIF...ELSE Statement
  4. UNLESS Statement
  5. UNLESS….ELSE Statement
  6.  UNLESS…ELSIF….ELSE Statement
  7. SWITCH Statement
  8. Conditional Operator ? :

IF Statement

IF Statement is simple statement that is used where some condition need to be tested if that condition is true then perform certain operation and if not then continue executing code followed by IF statement.

Following is the syntax of IF Statement:

If(Boolean expression )
{
//Statement to be executed if condition is true
}

Here is an example of IF Statement:
$x = 100;
if( $x < 200 )
{  
    printf "This will execute means condition it true\n";
}

$x = 0;
if( $x )
{
    printf "X has a true value\n";
}
print "value of a is : $a\n";
Output if the above program is:

This will execute means condition it true
value of a is : 0
If you change the value of x=0 to x=1 then the output will be:

This will execute means condition it true
X has a true value
value of a is : 1

So here what happens is o is considered as false and 1 is considered true for Boolean value. 

IF…ELSE Statement 

Sometime we come across the situation where we need to perform something even if condition false. In that case only IF statement is not sufficient. So there IF..ELSE statement comes into the picture.

Here is the syntax:
If(Boolean Expression)
{
//If condition is true then this code will executed.
}else
{
If condition is false then this code will get executed
}
Here is an example for IF…ELSE Statement:
$x = 100;
if( $x < 200 ){  
    printf "Value of x is less then 200 \n";
}else
{
    printf "Value of $x is greater then 200 \n";
}

$x = 1;
if( $x ){
    printf "X has a true value\n";
}else
{
    printf "X has a false value\n";
}
Output of the program is:

Value of x is less than 200
X has a true value

IF…ELSIF...ELSE Statement

There are some situations where you have multiple options but we must select one at a time. In this kind of situation we can use this IF…ELSIF….ELSE statement.
Here is the syntax of IF…ELSIF….ELSE statement:

If(Boolean Expression)
{
//If condition is true then this code will executed.
}elsif(Boolean Expression)
{
//If condition is true then this code will executed.
}else
{
//If both above conditions are false then this code will executed.

}

Let’s take one example to understand:

Let’s assume you have system only 3 person are allowed to access it or for any other person it will show error
$name= "Dharmesh";
# check the boolean condition using if statement
if( $name  eq  "Sanjay" ){
    # if condition is true then print the following
    printf "Welcome Sanjy\n";
}elsif($name  eq "Tejas" ){
    # if condition is true then print the following
    printf "Welcome Tejas\n";
}elsif($name  eq "Dharmesh" ){
    # if condition is true then print the following
    printf "Welcome Dharmesh\n";
}else{
    # if none of the above conditions is true
    printf "You are not authorized person\n";
}

In above example only one person at time can access the system. It will only allow Sanjay, Tejas and Dharmesh otherwise gives an error.

SWITCH Statement

Instead of using this there is another alternative that Perl provide for IF…ELSIF….ELSE statement is switch statement. Let’s take same above example for switch statement too so it will be easy for you to understand. But for that you need to specify in beginning of script “use switch”.
use switch;

$name=”Tejas”;

Switch($name)
{
case “Sanjay”
{
    printf "Welcome Sanjy\n";
}
case “Tejas”
{
    printf "Welcome Tejas\n";

}
case “Dharmesh”
{
    printf "Welcome Dharmesh\n";
}
else
{
    printf "You are not authorized person\n";
}
}
This code also works same as above. This two are alternative for each other. Let’s see one more example:
use Switch;
$var = 10;
@array = (10, 20, 30);
%hash = ('key1' => 10, 'key2' => 20);

switch($var){
   case 10           { print "number 100\n"; next; }
   case "a"          { print "string a" }
   case [1..10,42]   { print "number in list" }
   case (\@array)    { print "number in list" }
   case (\%hash)     { print "entry in hash" }
   else              { print "previous case not true" }
}
When above code will be executed, following will be the output.
number 100
number in list

UNLESS Statement:

Unless is same as conditional statement as IF statement. But only difference in UNLESS statement is that it execute block of code when condition is false. On other hand in IF Statement block of code execute only when condition is true.
Lets see an example:
$num=100;
unless ($num<200)
{
print ”This will execute when condition is false\n”;
}
 print ”The value of num is $num\n”;
When above code get executed it will produce following output:
Executing the program....
The value of num is 100

Since condtion given in UNLESS Statement was true hence This will execute when condition is false this is not printed.

UNLESS….ELSE Statement

UNLESS…ELSE is also works similar as IF…ELSE statement. It also behave same as UNLESS statement that we have seen above. Just else part is added to it. I will take same above example but we will add else part in it.
$num=100;
unless ($num<200)
{
print ”This will execute when condition is false\n”;
}else
{
 print ”The value of num is $num\n”;
}
Following will be the output of above program:
Executing the program....
The value of num is 100

UNLESS…ELSIF….ELSE Statement

This again same as IF…ELSIF...ELSE Statement but as we have seen earlier here condition must be false in order to execute the code. So let’s move on to quick example.
$num=100;
unless ($num==200)
{
print ”The value of num is equal $num\n”;
}elsif($num==400)
{
print ”The value of num is equal $num\n”;
}else
{
print ”The Actual value is $num\n”;
}
After execution we have got following output:

Executing the program....
The value of num is equal 200


But if you try this code then only else part will be executed:
$num=200;
unless ($num==200)
{
print "The value of num is equal 200\n";
$num=400;
}elsif($num==400)
{
print "The value of num is equal 400\n";
}else
{
print "The actual value of $num\n";
}

The ? : Operator

The last many called this a ?: operator or ternary operator. This also works as IF…ELSE statement. It first evaluates Boolean expression and if it’s true then it will take first parameter and if false then it will take second parameter.

Syntax:
Exp1 ? Exp2 : Exp3;

Let’s see simple example:

$name=”Dharmesh”;
$age=25;
$status=($age==25)?”Young”: ”Senior Citizen”;
print”$name is $status”;
Following will be the output of above program:
Executing the program....
Dharmesh is Young

So we have seen all decision making statement that are provided by Perl Scripting. We have seen all IF, IF…ELSE , IF…..ELSIF….ELSE , ? : operator and Unless operator. If you have any queries or suggestion please comment below. Thanks for Reading.

Share this

CEO and Admin of TechnoDiscover And Nothing Like Everything
Bcom From Mumbai University
Pursuing MCA From Mumbai University

Related Posts

Previous
Next Post »