What are the logical operators and, or, & not in Perl?
A logical operator is a symbol or word that connects two or more expressions so that the value of the created expression is solely determined by the value of the original expressions and the operator’s meaning.
Below are the logical operators available in Perl:
andornot
Description
The table below explains each of the logical operators in Perl.
Operator Symbol | Operator Name | Example | Description |
| Logical And | Operand-A && Operand-B | Returns True when both operand-A and operand-B are True; otherwise, it returns False. |
| Logical Or | Operand-A || Operand-B | Returns True when either operand-A or operand-B is True; otherwise, it returns False. |
| Logical Not | not Operand-A | Returns the operand's logical state in reverse. |
The figure below is the visual representation of the logical operators in Perl.
Note:
Trueequates to1.Falseequates to0.
Code
The following code demonstrates how to use these logical operators in Perl.
$A=10, $B=20, $C=30;#And Operator#Both Trueif ($A<$B && $B<$C){print "The value of (A<B && B<C): true \n";} else {print "The value of (A<B && B<C): false \n";}#First operand: True Second Operand: Falseif ($A<$B and $B>$C){print "The value of (A<B and B>C): true \n";} else {print "The value of (A<B and B>C): false \n";}#Both Falseif ($A>$B && $B>$C){print "The value of (A>B && B>C): true \n";} else {print "The value of (A>B && B>C): false \n";}#Or Operator#Both Trueif ($A<$B || $B<$C){print "The value of (A<B || B<C): true \n";} else {print "The value of (A<B || B<C): false \n";}#First operand: True Second Operand: Falseif ($A<$B or $B>$C){print "The value of (A<B or B>C): true \n";} else {print "The value of (A<B or B>C): false \n";}#Both Falseif ($A>$B || $B>$C) {print "The value of (A>B || B>C): true \n";} else {print "The value of (A>B || B>C): false \n";}#Not operatorif (not($A<$B)){print "The value of not(A<B): true \n";} else {print "The value of not(A<B): false \n";}
Free Resources