 # PERL - Arithmetic Operators

Arithmetic operators are symbols used to execute general arithmetic procedures including: addition (+), subtraction (-), multiplication (*), and division (/).

## Arithmetic Operators:

 Operator Example Result Definition + 7 + 7 = 14 Addition - 7 - 7 = 0 Subtraction * 7 * 7 = 49 Multiplication / 7 / 7 = 1 Division ** 7 ** 7 = 823543 Exponents % 7 % 7 = 0 Modulus

With these operators we can take a number and perform some simple math operations.

## PERL Arithmetic:

```#!/usr/bin/perl

print "content-type: text/html \n\n";	#HTTP Header

#PICK A NUMBER
\$x = 81;
\$sub = \$x - 9;
\$mul = \$x * 10;
\$div = \$x / 9;
\$exp = \$x ** 5;
\$mod = \$x % 79;
print "\$x plus 9 is \$add<br />";
print "\$x minus 9 is \$sub<br />";
print "\$x times 10 is \$mul<br />";
print "\$x divided by 9 is \$div<br />";
print "\$x to the 5th is \$exp<br />";
print "\$x modulus 79 is \$mod<br />";
```

## arithmetic.pl:

81 plus 9 is 90
81 minus 9 is 72
81 times 10 is 810
81 divided by 9 is 9
81 to the 5th is 3486784401
81 modulus 79 is 2

# PERL - Assignment Operators

Assignment operators perform an arithmetic operation and then assign the value to the existing variable. In this example, we set a variable (\$x) equal to 5. Using assignment operators we will replace that value with a new number after performing some type of mathematical operation.

## Assignment Operators:

 Operator Definition Example += Addition (\$x += 10) -= Subtraction (\$x -= 10) *= Multiplication (\$x *= 10) /= Division (\$x /= 10) %= Modulus (\$x %= 10) **= Exponent (\$x **= 10)

## PERL Assignment:

```#!/usr/bin/perl

print "content-type: text/html \n\n";	#HTTP HEADER

\$x = 5;
print '\$x plus 10 is '.(\$x += 10);
print "<br />x is now ".\$x;       #ADD 10
print '<br />\$x minus 3 is '.(\$x -= 3);
print "<br />x is now ".\$x;       #SUBTRACT 3
print '<br />\$x times 10 is '.(\$x *= 10);
print "<br />x is now ".\$x.       #MULTIPLY BY 10
print '<br />\$x divided by 10 is '.(\$x /= 10);
print "<br />x is now ".\$x;       #DIVIDE BY 10
print '<br />Modulus of \$x mod 10 is '.(\$x %= 10);
print "<br />x is now ".\$x;       #MODULUS
print '<br />\$x to the tenth power is '.(\$x **= 10);
print "<br />x is now ".\$x;       #2 to the 10th
```

## Display:

\$x plus 10 is 15
x is now 15
\$x minus 3 is 12
x is now 12
\$x times 10 is 120
\$x is now 120
\$x divided by 10 is 12
x is now 12
Modulus of \$x mod 10 is 2
x is now 2
\$x to the tenth power is 1024
x is now 1024

Each time an operation is performed our variable (\$x) is permanently changed to a new value of \$x.

# PERL - Logical & Relational Operators

Relationship operators compare one variable to another. (5 < 12) They are used to compare equality or inequality of two or more variables, be it a string or numeric data.

Logical operators state and/or relationships. Meaning, you can take two variables and test an either or conditional. Logical operators are used later on in conditionals and loops. For now, just be able to recognize them in the upcoming examples.

## Relational

 Operator Example Defined Result ==,eq 5 == 55 eq 5 Test: Is 5 equal to 5? True !=,ne 7 != 27 ne 2 Test: Is 7 not equal to 2? True <,lt 7 < 47 lt 4 Test: Is 7 less than 4? False >,gt 7 > 47 gt 4 Test: Is 7 greater than 4? True <=,le 7 <= 117 le 11 Test: Is 7 less than or equal to 11? True >=,ge 7 >= 117 ge 11 Test: Is 7 greater than or equal to 11? False

## Logical

 Operator Defined Example &&,and Associates two variables using AND if ((\$x && \$y) == 5)... ||,or Associates two variables using OR if ((\$x || \$y) == 5)...

Please note that you must use each different operator depending of whether or not you are comparing strings or numbers. In the table above, the black operators are for numbers and the red ones are for strings.

# PERL - Variables + Operators

Variables can be used with mathematical formulas using PERL Operators discussed in a previous lesson. Also, note that variables are case sensitive. "\$myvariable," "\$MYvariable," and "\$Myvariable" can all be assigned different values due to case sensitivity. Numbers of course can be added, subtracted, or multiplied using operators. Strings as shown in the example below can also be used with operators.

## PERL Code:

```#!/usr/bin/perl

print "Content-type: text/html \n\n"; #HTTP HEADER

\$myvariable = "Hello,";
\$Myvariable = " World";

\$string3 = "\$myvariable \$Myvariable";

print \$string3;
```

Hello, World

# Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

 Web Reference HTML Reference CSS Reference CSS Examples PHP Examples Help Tizag Grow Link to Tizag Tizag.com Store
 Advertise Here More Tutorials! Microsoft Office Tutorials Artist Tutorials