logo codesdope

if and else in Perl


From here your real programming learning will start. In this chapter, you will learn about decision making. This means that if a user gives you some input, then you will learn to judge that input and then perform according to it.

In real life, we also make conditional decisions e.g.- If it is raining, then you will take an umbrella, else not; if it is your birthday, then it is a special day, else a normal day; if you want to allow some features in a website to the users with an account only, then you can think of - if user has an account, then this else not.

if and else in Perl

Now let's implement this in Perl.

$a = <>;
chomp($a);
if ($a > 10){
  print "Your number is greater than 10\n";
}
else{
  print "Your number is not greater than 10\n";
}
Output
12
Your number is greater than 10

In the first line, we are taking input.
if ($a>10) - Here we used 'if'.
First see $a>10 - The condition is comparing a with 10 and if a is greater than 10, then it is true and if smaller, then false.
So, the whole story is that if a>10 ( if a is greater than 10 ), then print "your number is greater than 10\n"; will be executed, otherwise it will execute print "your number not greater than 10\n";
{} represents the bodies of 'if' and 'else'. This means that the statements written inside the curly braces '{}' after 'if' constitute the body of that 'if' and the statements inside the curly braces '{}' constitute the body of that 'else'. It will be clearer from examples. So, let's proceed for now.

See the flow diagram below.

if (condition){
  statement
  statement
  ...
}
else{
  statement
  statement
  ...
}

We first write if (condition) ( where the condition is what has to be judged.. In the first example, the condition was 'a>10' ). The statements in the body of if will be executed only when this condition is true, otherwise the statements in the body of else will be executed. ({} represents the bodies of 'if' and 'else').

Odd and even example

$a = <>;
chomp($a);
if ($a%2 == 0){
  print "EVEN\n";
}
else{
  print "ODD\n";
}
Output
10
EVEN

if ($a%2 == 0) : This is very simple to understand. $a%2 will give the remainder of the division of 'a' by 2. We know that if a number is divisible by 2, then it is even. So, if 'a' is divisible by 2, then 'a%2' will be 0.
So, the whole story is that if a is divisible by 2 (a%2==0), then print "EVEN\n"; will be executed, otherwise print "ODD\n"; will be executed.

if under if


Yes, we can also use one if inside another. Let's see how to do this.

print "Enter salary\n";
$a = <>;
chomp($a);
print "Enter sex M/F\n";
$b = <>;
chomp($b);
if ($a>10000){
  if ($b eq "M"){
    print "Your salary is good and you are a male\n";
  }
  else{
    print "Your salary is good and you are a female\n";
  }
}
else{
  if ($b eq "M"){
    print "Your salary is not so good and you are a male\n";
  }
  else{
    print "Your salary is not so good and you are a female\n";
  }
}
Output
Enter salary
12000
Enter sex M/F
F
Your salary is good and you are a female

This is also called nesting. It is very simple. After giving input, firstly, the condition of the top-most 'if' will be checked i.e. ($a > 10000). If it is true, then the statements inside this 'if' will be executed. The first statement inside this 'if' is if ($b eq "M"). So, this will be executed and if the condition of this if is also true, then the statements inside it will be executed, otherwise the statements inside the corresponding else will be executed.

Therefore, after giving the inputs of '12000' and 'F', the 'if' with condition 'a > 10000' became true. So, the statement in its body got executed. Now, the first statement inside it is another if with the condition b eq "M" which became false. So, the statement inside the corresponding 'else' got executed i.e. print "Your salary is good and you are a female\n";.

Notice '{}' after every 'if' and 'else' representing their bodies.

Using elsif


elsif is used for else if. Imagine that there are 10 pockets of candies and each of them costs 1 coin. Now, if you have more than 10 coins, then you will buy 10. Else if you have 5 coins, then you will buy 5 pockets. Else you will buy less than 5 pockets. We do the same thing in Perl. Look at the following example to understand this.

print "Enter salary\n";
$a = <>;
chomp($a);
print "Enter sex M/F\n";
$b = <>;
chomp($b);
if ($a>10000 && $b eq "M"){
  print "Your salary is good and you are a male\n";
}
elsif ($a>10000 && $b eq "F"){
  print "Your salary is good and you are a female\n";
}
elsif ($a<=10000 && $b eq "M"){
  print "Your salary is not so good and you are a male\n";
}
elsif ($a<=10000 && $b eq "F"){
  print "Your salary is not so good and you are a female\n";
}
else{
  print "Enter valid input\n";
}
Output
Enter salary
8000
Enter sex M/F
M
Your salary is not so good and you are a male

I hope that you have already got this by just an example. Next lines will make it even clearer.
The first condition in 'if' will be checked.
If this condition is true, then only 'if' will be executed (i.e. its corresponding statements will be executed) and if it is false, then the compiler will check below.
If the condition of 'if' is false, then the condition of 'elsif' will be checked. In the same way, if this condition is true, then its statements will be executed, otherwise the compiler will come below.
If the conditions of 'if' and all 'elsif' are false, then only the statement corresponding to else(i.e. print "Enter valid input\n") will be executed.

if (condition){
  statement
  statement
  ...
}
elsif{
  statement
  statement
  ...
}
elsif{
  statement
  statement
  ...
}
elsif{
  statement
  statement
  ...
}
elsif{
  statement
  statement
  ...
}
else{
  statement
  statement
  ...
}

We can have any number of 'elsif' after 'if'.

In the above example, we have given inputs of '8000' and 'M'. So, the condition of 'if' ( $a>10000 && b eq "M" ) is false since a is less than 10000. Now coming to the next 'elsif' having the condition ($a>10000 && b eq "F"), its condition is also false. The condition ( $a<=10000 && b eq "M" ) of the next 'elsif' is true because 8000 < 10000 and also 'b' is 'M'. So, the statement inside this 'elsif' got executed and 'Your salary is not so good and you are a male' got printed on the screen.

Unless


Perl provides another tool for conditional flow and it is unless. It is exactly the opposite of 'if'. The statements inside 'if' are executed if the condition is true but the statements inside 'unless' are executed if the condition is false. Let's see an example to understand this.

print "Enter salary\n";
$a = <>;
unless ($a>10000){
  print "Your salary is less than 10000\n";
}
else{
  print "Yup! good salary\n";
}
Output
Enter salary
100
Your salary is less than 10000

We have entered 100 and it is less than 10000. So, the condition $>10000 is false and that's why the statement inside it got executed.

Another form of if else


We can also judge the condition using the ternary operator. Ternary operator checks whether a given condition is true and then evaluates the expressions accordingly. It works as follows.

condition ? expression1 : expression2;

If the condition is true, then expression1 gets evaluated, otherwise expression2 gets evaluated.

print("Enter age\n");
$age = <>;
chomp($age);
($age > 18) ? print("eligible to vote\n") : print("not eligible to vote\n");
Output
Enter age
10
not eligible to vote

Here, if the condition ($age > 18) is true, then expression1 i.e. print("eligible to vote\n") will get evaluated, otherwise expression2 i.e. print("not eligible to vote\n") will get evaluated. Since the value of age that we entered (10) is less than 18, therefore the expression2 got evaluated and "not eligible to vote" got printed.

Let's see another example in which we want to find the greater among two numbers.

$num1 = 4;
$num2 = 5;
$num = ($num2 > $num1) ? $num2 : $num1;
print("The greater number is $num\n");
Output
The greater number is 5

In this example, if the condition ($num2 > $num1) is true, then num2 i.e. 5 will get assigned to 'num', otherwise 'num1' i.e. 4 will get assigned to 'num'. In our case, the values of 'num1' and 'num2' are 4 and 5 respectively. Thus, the condition '$num2 > $num1' became and the value of num2 i.e. 5 got assigned to num and so 5 got printed.

my - Local variable


Suppose we have a variable having some value but we want to use a different value of the same variable only in one block of code. This can be easily done using a keyword my in Perl. Let's see an example to understand how it works.

$a = 5;
{
  my($a);
  $a = 6;
  print "Value of a is $a\n";
}
print "Value of a outside the block is $a\n";
Output
Value of a is 6
Value of a outside the block is 5

Let's see one more example of using 'my' in 'if' block.

$a = 5;

{

my($a);
$a = 7;

if ($a == 7){
  my($a);
  $a = 6;
  print "Value of a is $a\n";
}
print "value of a in this block is $a\n";
}
print "Value of a outside every block is $a\n";
Output
Value of a is 6
Value of a in this block is 7
Value of a outside every block is 5
Prev Next

Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.