Perl if-else Statement

The if statement in Perl language is used to perform operation on the basis of condition. By using if-else statement, you can perform operation either condition is true or false. Perl supports various types of if statements:

  • If
  • If-else
  • If else-if

If

The Perl single if statement is used to execute the code if condition is true. The syntax of if statement is given below:

Syntax
if(expression){  
//code to be executed  
}

Flowchart of if statement in Perl

if statement in perl
Example

Let's see a simple example of Perl language if statement.

snippet
$a = 10;
if( $a %2==0 ){
    printf "Even Number\n";
}
}
Output
Even Number

Here, output is even number as we have given input as 10.

If-else

The Perl if-else statement is used to execute a code if condition is true or false. The syntax of if-else statement is given below:

Syntax
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}

Flowchart of if-else statement in Perl

if-else statement in Perl
Example #1

Let's see the simple example of even and odd number using if-else statement in Perl language.

snippet
$a = 10;
if( $a %2==0 ){
    printf "Even Number\n";
}else{ 
    printf "Odd Number\n";
}
Output
Even Number

Here, input is an even number and hence output is even.

Example #2

Perl If-else Example with Input from user

In this example, we'll take input from user by using standard input (<STDIN>/<>).

snippet
print "Enter a Number?\n";
$num = <>;
if( $num %2==0 ){
    printf "Even Number\n";
}else{ 
    printf "Odd Number\n";
}
Output
Enter a Number? 5 Odd Number
Output
Enter a Number? 4 Even Number

In the first output, user has entered number 5 which is odd. Hence the output is odd.

In the second output, user has entered number 4 which is even. Hence the output is even.

If else-if

The Perl if else-if statement executes one code from multiple conditions. The syntax of if else-if statement is given below:

Syntax
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}

Flowchart of if else-if statement in Perl

if-else-if statement in Perl
Example

The example of if else-if statement in Perl language is given below.

snippet
print "Enter a Number to check grade\n";
$num = <>;
if( $num < 0 || $num > 100){
    printf "Wrong Number\n";
}elsif($num >= 0 && $num < 50){ 
    printf "Fail\n";
}elsif($num >= 0 && $num < 60){ 
    printf "D Grade\n";
}elsif($num >= 60 && $num < 70){ 
    printf "C Grade\n";
}elsif($num >= 70 && $num < 80){ 
    printf "B Grade\n";
}elsif($num >= 80 && $num < 90){ 
    printf "A Grade\n";
}elsif($num >= 90 && $num <= 100){ 
    printf "A+ Grade\n";
}
Output
Enter a Number to check grade 66 C Grade
Output
Enter a Number to check grade 2 Wrong Number
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +