Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

perl
communitycreator
logicaloperator

What are the logical operators and, or, & not in Perl?

harris910

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:

  • and
  • or
  • not

Description

The table below explains each of the logical operators in Perl.

Operator Symbol

Operator Name

Example

Description

  • &&
  • and

Logical And

Operand-A && Operand-B

Returns True when both operand-A and operand-B are True; otherwise, it returns False.

  • ||
  • or

Logical Or

Operand-A || Operand-B

Returns True when either operand-A or operand-B is True; otherwise, it returns False.

not

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.

Visual representation of Logical Operators in Perl

Note:

  • True equates to 1.
  • False equates to nothing.

Code

The following code demonstrates how to use these logical operators in Perl.


$A=10, $B=20, $C=30;

#And Operator
#Both True
print "The value of (A<B && B<C): ".($A<$B && $B<$C). "\n";
#First operand: True		Second Operand: False
print "The value of (A<B and B>C): ".($A<$B and $B>$C). "\n";
#Both False
print "The value of (A>B && B>C): ".($A>$B && $B>$C). "\n";

#Or Operator
#Both True
print "The value of (A<B || B<C): ".($A<$B || $B<$C). "\n";
#First operand: True		Second Operand: False
print "The value of (A<B or B>C): ".($A<$B or $B>$C). "\n";
#Both False
print "The value of (A>B || B>C): ".($A>$B || $B>$C). "\n";

#Not operator
$D=1;
#reverse of true logical state
print "The value of not(D): ".not($D). "\n";

RELATED TAGS

perl
communitycreator
logicaloperator
RELATED COURSES

View all Courses

Keep Exploring