Syntax
Different syntaxes of “if” statements are given below.
A. if statement
If (condition) {
statement(s)
}
The above syntax is used to execute the statements if the condition defined in the “if” statement returns true.
B. if-else statement
If (condition) {
statement(s)
} else{
statement(s)
}
The above syntax is used to execute the statements of the “if” block if the condition returns true otherwise, execute the statements of the “else” block.
C. if-else-if statement
if (condition) {
statement(s)
} else if (condition) {
statement(s)
}
else if (condition){
statement(s)
}
…
else {
statement(s)
}
The above syntax is used to execute the statements of the first “if” block if the condition returns true otherwise, execute the statements of the next “else if” block if the condition returns true. If the condition of all “if” block returns false, then the statements of the “else” block will be executed.
The uses of different types of “if” statements have been shown in the next part of this tutorial.
Example-1: Use of “if” Statement
Create a PERL file with the following code that will print the current date and time of the system if the input number taken from the user is greater than or equal to 10; otherwise, nothing will be printed. The localtime() function has been used in the code to read the current date and time of the system.
print "Enter a number:";
$value = <>;
# Check if the input value is greater than or equal to 10 or not
if($value >= 10){
# Read the current date and time
$datetime = localtime();
# Print the current date and time value
print("Today is $datetime\n");
}
Output
The following output will appear after executing the above code two times with the input values 10 and 5.
Example-2: Use of “if-else” Statement
Create a PERL file with the following code that compares two number values taken from the user and prints a message based on the values. The chomp() function has been used to remove the newlines from the input values. The int() function has been used to convert the input value into the number before comparing the values. The “if-else” statement has been used here to check whether the input numbers are equal or not.
print "Enter the first number:";
$value1 = <>;
print "Enter the second number:";
$value2 = <>;
# Remove the newline from each input
chomp($value1);
chomp($value2);
# Check the input values are equal or not
if(int($value1) == int($value2)){
print "$value1 and $value2 are equal.\n";
}
else
{
print "$value1 and $value2 are not equal.\n";
}
Output
The following output will appear after executing the above code with the values 7 and 5.
The following output will appear after executing the above code with the values 4 and 4.
Example-3: Use of “if-elsif-else” Statement
Create a PERL file with the following code that will check whether two number values taken from the user are equal or the first number is greater than the second number, or the first number is less than the second number. The chomp() function has been used to remove the newlines from the input values. The int() function has been used to convert the input value into the number before comparing the values. The “if-elsif-else” statement has been used here to do the task mentioned above and print the appropriate message,
print "Enter the first number:";
$value1 = <>;
print "Enter the second number:";
$value2 = <>;
# Remove the newline from each input
chomp($value1);
chomp($value2);
# Check the input values are equal or not
if(int($value1) == int($value2)){
print "$value1 and $value2 are equal.\n";
}
# Check the first value is less than the second value
elsif(int($value1) < int($value2))
{
print "$value1 is less than $value2.\n";
}
else
{
print "$value1 is greater than $value2.\n";
}
Output
The following output will appear after executing the above code with the values 5 and 5.
The following output will appear after executing the above code with the values 9 and 5.
The following output will appear after executing the above code with the values 3 and 6.
Example-4: Use of nested “if” Statement
Create a PERL file with the following code that uses nested “if” conditions to print a message based on the input value taken from the user. According to the code, if the MCQ mark taken from the user is greater than or equal to 70, then the descriptive marks will be taken from the user, and the second “if” condition will be checked. If the descriptive mark is greater than or equal to 50, then “You have passed the exam.” Any other messages will be printed if any of the “if” conditions return false.
print "Enter the MCQ marks: ";
$mcq = <STDIN>;
# Check the first condition
if ($mcq >= 70) {
# Take the descriptive marks from the user
print "Enter the descriptive marks: ";
$descriptive = <STDIN>;
# Check the second condition
if ($descriptive >= 50) {
# Print message when both “if” conditions return true
print "You have passed the exam.\n";
}
else
{
# Print message if the second “if” condition returns false
print "You have failed the exam.\n";
}
} else {
# Print message if the first “if” condition returns false
print "You have failed the exam.\n";
}
Output
The following output will appear after executing the above code with the values 75 and 46.
The following output will appear after executing the above code with the values 77 and 58.
The following output will appear after executing the above code with the value 65.
Conclusion
The way of using simple “if,” “if-else,” “if-elsif-else,” and nested “if” statements have been shown in this tutorial by using multiple examples.