Broad Network


Perl Comparison and Arithmetic Operators

Perl Basics – Part 7

Perl Course

Foreword: In this part of the series, we talk about some common Perl Operators.

By: Chrysanthus Date Published: 29 Mar 2015

Introduction

This is part 7 of my series, Perl Basics. In this part of the series, we talk about some common Perl Operators. We have seen the logical operators. We have also seen the assignment and equal operators. In this part we look at comparison and arithmetic operators. You should have read the previous parts of the series before reaching here, as this is a continuation.

Operand
An Operand is a variable or a literal (value) associated with an operator. Consider,

    $myVar = 30;

$myVar is a left operand and 30 is a right operand. = is the assignment operator, not the equal operator. The equal operator is, == when dealing with numbers and eq when dealing with strings; and is used only in conditions.

Consider:

     $myVar && $hisVar && $herVar

There are three operands in the above expression. So, you can talk of the first, second and third operand.

Comparison Operators
A comparison operator compares the operands on its sides and returns a logical value (true or false) depending on whether the comparison is correct or wrong. If the comparison is correct a logical value of true is returned. If it is wrong, a logical value of false is returned. Another name for Boolean Value is Logical Value, which is either true or false.

The Equal Operator for Numbers
It is ==, typed as a double assignment operator. The equal operator returns true if operands (numbers) are equal, otherwise it returns false.

The Not Equal Operator for Numbers
The Not Equal operator is the opposite of the Equal Operator. The Not Equal operator is, != . It returns true if the operands are not equal, otherwise it returns false. Let us look at some examples:

Read and try the following code:

use strict;

my $myVar = 25;
my $hisVar = 30;
if ($myVar != $hisVar)
    {
        print 'The values of the two variables are not equal.';
    }

$myVar is 25, $hisVar is 30. The condition is read like this: If $myVar is not equal to $hisVar, then the if-block will be executed. Since the values of the variables are not equal,  ($myVar != $hisVar) returns true.

In the following code, the values of the two variables are equal, so the condition returns false and the if-block is not executed.

use strict;

my $myVar = 50;
my $hisVar = 50;
if ($myVar != $hisVar)
    {
        print 'The values of the two variables are not equal.';
    }

Note: The letter O and the digit zero are not the same things. If you type the letter O in place of zero (0) you will not have the right results. The digit zero is found in the number keypad of your keyboard. The letter O is found in the main keyboard area.

The Equal Operator for Strings
It is eq, meaning, equal. The equal operator returns true if operands (strings) are equal, otherwise it returns false. We have seen examples of this.

The Not Equal Operator for Strings
The Not Equal operator is the opposite of the Equal Operator. The Not Equal operator for strings is, ne . It returns true if the operands are not equal, otherwise it returns false. Read and try the following code to illustrate this:

use strict;

if ("one" ne "two")
    {
        print "The strings are not equal.";
    }

The Greater Than Operator
The Greater Than operator is, > . It returns true if the left operand is greater than the right operand. In the following example, the left operand is greater than the right operand. So the if-block is executed:

use strict;

my $variab1 = 60;
my $variab2 = 70;
if ($variab2 > $variab1)
    {
        print 'The value of variab2 is greater than the value of variab1.';
    }

Try the above code.

Greater Than Or Equal - Operator
The Greater Than or Equal operator is, >= (it is the math greater than sign followed by the math equal sign). It returns true if the left operand is greater than or equal to the right operand.

The Less Than Operator
The Less Than Operator is < .It returns true if the left operand is less than the right operand.   

The Less Than or Equal - Operator
The Less than or Equal operator is, <= . It returns true if the left operand is less than or equal to the right operand.

Arithmetic Operators
An Arithmetic operator takes one or two numbers as operands (either literals or variables) and returns the answer, similar to what happens in arithmetic.

The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). To save time explaining these four operators, just read and try the following examples:

Addition Operator
Code example:

use strict;

my $var1 = 20;
my $var2 = 30;
    
my $var3 = $var2 + $var1;
print $var3;

Subtraction Operator
Code example:

use strict;

my $var1 = 20;
my $var2 = 30;
    
my $var3 = $var2 - $var1;
print $var3;

Multiplication Operator
Code example:

use strict;

my $var1 = 20;
my $var2 = 30;
    
my $var3 = $var2 * $var1;
print $var3;

Note that the multiplication operator is * and not X.

Division Operator
Code example:

use strict;

my $var1 = 20;
my $var2 = 30;

my $var3 = $var2 / $var1;
print $var3;

Note that the division operator is, / .

Other operators are the Modulus (%), Increment (++), Decrement (--), and the Negation operators. You have to learn the particular way in which each of these operators behaves (see below).

Modulus Operator
The modulus operator divides the first operand by the second operand and returns the remainder. Read and try the following code:

use strict;

my $var1 = 17;
my $var2 = 12;
    
my $var3 = $var1 % $var2;
print $var3;

The Modulus operator is the percentage sign.

Increment Operator
The Increment Operator is, ++. It works with one operand, not two as the others. The operand has to be a number. When it is placed in front (prefix) of the operand, it behaves in one way. When it is placed after (postfix) the operand it behaves in another way.

Prefix: When it is prefix, it adds 1 to the operand and returns the incremented operand. Read and try the following code:

use strict;

my $var1 = 10.5;
    
my $var2 = ++$var1;
print $var2;

In the code, initially, 10.5 is assigned to var1. Then we have a statement. In the statement you have a new variable, $var2, the assignment operator and then “++$var1”. What interest us here is “++$var1”, where the increment operator is in front of the variable. The value the increment operator returns is assigned to $var2. If you have tried the code, you would have noticed that the value of $var2 is 11.5. This means, if used prefix, it increments the operand and then returns the incremented operand. Note: in the above code, the final value for $var1 is 11.5 and not 10.5.

Postfix: When it is postfix, it returns the operand before adding 1 to it. The returned value is the original value of the operand. The increased value is the new value of the operand, which is not returned. Read and try the following code.

use strict;

my $var1 = 10.5;
    
my $var2 = $var1++;
print $var2; print "\n";
print $var1;

If you have tried the above code, you would have noticed that the value for $var2 is 10.5 and the final value for $var1 is 11.5, confirming that the incrementing took place after the value was returned. The “print "\n"” sends a line break to the console so that the next result should be displayed one line below the previous one. In Perl, you can have more than one statement in one line, such as in, “print $var2; print "\n";”.

Decrement Operator
The Decrement operator, -- , behaves like the increment operator with the difference that it subtracts 1 instead of adding.

Negation Operator
This operator is the negative sign, - . It works with one operand (on its right); it negates the operand just like in math. Read and try the following:

use strict;

my $var1 = 25;
    
my $var2 = -$var1;
print $var2;

We have come to the end of this part of the series. Do not forget that the all what we learn in this series is applicable to traditional Perl. You can try the code with traditional Perl (in their operating systems). If you want to try a code sample with traditional Perl, just precede it with something like, #!/usr/bin/perl (see the corresponding Perl documentation).

The undef Value in a Condition
When a variable is declared without a value assigned to it, Perl assigns the constant value, undef to the variable, without you knowing. When dealing with numbers, undef is equivalent to zero (false). When dealing with strings, undef is equivalent to the empty string, "" (false). Try the following code:

use strict;

my $var;

if (!$var)
    {
        print 'undef means false'. "\n";
    }
if ($var == undef)
    {
        print 'undef can mean zero, which is false'. "\n";
    }
if ($var eq undef)
    {
        print 'undef can mean "", which is false'. "\n";
    }

The output is:

    undef means false
    undef can mean zero, which is false
    undef can mean "", which is false

undef means undefined.

Let us stop here. Rendezvous 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

Comments

Become the Writer's Fan
Send the Writer a Message