Perl Logical Operators and their Categories

Perl Operators  Part 4

Perl Course

Foreword: Logical operators are the operators for the Boolean AND, Boolean OR and Boolean NOT. In this part of the series, I talk about logical operators in Perl.

By: Chrysanthus Date Published: 12 Oct 2015

Introduction

This is part 4 of my series, Perl Operators. Logical operators are the operators for the Boolean AND, Boolean OR and Boolean NOT. In this part of the series, I talk about logical operators in Perl. You should have read the previous parts of the series before reaching here, as this is a continuation. I use the if-condition to explain logical operators. In the following code samples, let 10dm mean a short man and 20dm mean a tall man.

Single Condition Expression Example
Consider the following code:

use strict;

#tall means 20
my \$me = 20;

if (\$me == 20)
{
print "I am tall";
}

Read and try the code. In the condition, (parentheses of if) there is only one expression, which is, (\$me == 20). If this expression results in true, the if-block will be executed. In Perl, the number 1 means true in a condition and the number 0 means false. In other words, 1 is the Boolean value for true and 0 is the Boolean value for false. The above if-construct is equivalent to

if (1)
{
print "I am tall";
}

Here 1 is true. For this second if-construct to be executed, you do not need the creation of the variable and its assignment. Read and try the following code:

use strict;

if (1)
{
print "I am tall";
}

Let us look at a case where the condition results in false. Consider the following code:

use strict;

my \$me = 10;

if (\$me == 20)
{
print "I am tall";
}

The if-block (curly braces) in the code will not be executed, because the condition results in false, since the value of the variable, \$me, is 10 for short and not 20 for tall. The above if-construct is equivalent to:

if (0)
{
print "I am tall";
}

An if-block can only be executed if the condition is true (1). In this last case it is not executed, since zero means false.
More than One Expression in Condition
You can have more than one expression in a condition. In this part of the series, I consider a maximum of two expressions in a condition. Each of the expressions results in true or false. The expressions are combined with the and, or Or, or not operator. The and operator is typed as, and or &&. The or operator is typed as, or or || . The not Operator is typed as not or ! . These three operators are called logical operators.

Note: and or && mean the same thing, but are of different categories  see below.
Note: or or || mean the same thing, but are of different categories  see below.
Note: not or ! mean the same thing, but are of different categories  see below.

With logical operators, the rules (truth tables) for and, or and not can be written as:

and
(false) && (false) = false
(false) && (true) = false
(true) && (false) = false
(true) && (true) = true

or
(false) || (false) = false
(false) || (true) = true
(true) || (false) = true
(true) || (true) = true

not
!(false) = true
!(true) = false

Each of the above three patterns is called a truth table.

Double-Expression Examples
The if-block will not be executed in the following code:

use strict;

if ((0)and(1))
{
print "We are tall";
}

A practical example for the above code is:

use strict;

my \$you = 20;
my \$me = 20;

if ((\$you == 10)and(\$me == 20))
{
print "We are tall";
}

20 is assigned to the variable, \$you, and also to the variable, \$me. The first expression in the condition results in false and the second one results in true. (false)&&(true) gives false as the effective Boolean value for the condition. So the block is not executed.

The if-block will be executed in the following code:

use strict;

if ((0)or(1))
{
print "We are tall";
}

A practical example for the above code is:

use strict;

#Tall means 20 and short means 10
my \$you = 20;
my \$me = 20;

if ((\$you == 10)or(\$me == 20))
{
print "We are tall";
}

Read the above code. Try it. The first expression results in false; the second one results in true. The effective condition is true, since (false)||(true) gives true.
not Examples
The if-block will be executed in the following code:

use strict;

if (not(0))
{
print "I am tall";
}

The if-block is executed, if the condition is true. not(false) gives true.

A practical example for the above code is:

use strict;

#Let tall mean 20 and short mean 10
my \$me = 20;

if (not(\$me == 10))
{
print "I am tall";
}

Exclusive OR
I gave three truth tables above: one for and, one for or, and one for not. You can have another one, which is for what is known as Exclusive OR, written as xor. It is:

Xor
(false) || (false) = false
(false) || (true) = true
(true) || (false) = true
(true) || (true) = false

It differs from the or table in the last line, where (true) || (true) = false instead of (true) || (true) = true.

In the following program, the if-block is not executed because 1 xor 1 is false (0):

use strict;

if ((1)xor(1))
{
print "We are tall";
}

A practical example for the above code is:

use strict;

#Let tall mean 20 and short mean 10
my \$me = 20;
my \$you = 20;

if ((\$me==20)xor(\$you==20))
{
print "We are tall";
}
Categories of Logical Operators
&&
In Perl && is known as the C-style And. It is alone in its category. It has left associativity. This means that, when the same sequence of this operator is used, one after another, the one on the left is operated first.

The && operator is of a higher precedence than the and operator  see below.

||
In Perl || is known as the C-style Or. It is alone in its category. It has left associativity. This means that, when the same sequence of this operator is used, one after another, the one on the left is operated first.

The || operator is of a higher precedence than the or operator  see below.

not
In Perl not is an operator. It is alone in its category. It has right associativity. This means that, when the same sequence of this operator is used, one after another, the one on the right is operated first.

The not operator does not have a c-style corresponding operator. However, it has a Perl corresponding operator, which is !  see later. The not operator is of a lower precedence than the ! operator  see below.

and
In Perl and is an operator. It is alone in its category. It has left associativity. This means that, when the same sequence of this operator is used, one after another, the one on the left is operated first.

The and operator is of a lower precedence than the && operator  see below.

or/xor
These two operators are of the same category. They have left associativity. This means that, when the same sequence of any of these operators is used, one after another, the one on the left is operated first.

The or operator is of a lower precedence than the || operator  see below.

Precedence
Operators in Perl exist in categories. Different categories are said to have different precedence. Operators in the same category have the same precedence and same associativity. Associativity is left or right. Precedence is a question of level.

Now, when a sequence of operators in the same category are used one after another, the one on the left or right is evaluated first, depending on the said associativity (left or right) for the category. However, when the sequence of operators are of different categories, the one with the said highest precedence is evaluated first; it does not matter which one is on the left or on the right.

As you can see, when there is a sequence of operators, one has to be evaluated first, before another one, before another, and so on. Which one is evaluated first? If the operators are of the same category, the one that is evaluated first depends on whether the category is left or right associative. If two consecutive operators are of different categories, the one with the higher precedence is evaluated first; it does not matter whether it is on the left or on the right.

I will come back to associativity and precedence in a later part of the series.

Time to take a break. We continue in the next part of the series.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT