Javatpoint Logo
Javatpoint Logo

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

Perl If Example

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

Flowchart of if statement in Perl

if statement in perl

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

Output:

Even Number

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


Perl If-else Example

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:

Flowchart of if-else statement in Perl

if-else statement in Perl

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

Output:

Even Number

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


Perl If-else Example with Input from user

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

Output:

Enter a Number?
5
Odd Number
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.


Perl If else-if Example

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

Flowchart of if else-if statement in Perl

if-else-if statement in Perl

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

Output:

Enter a Number to check grade
66
C Grade
Enter a Number to check grade
2
Wrong Number
Next TopicPerl Switch




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA