Tuesday, 16 July 2013

Binary arithmetic operators in perl programming

The Binary Arithmetic Operators

There are six binary arithmetic operators: addition, subtraction, multiplication, exponentiation (**), division, and modulus (%).
For example, op1.pl:
$x = 3 + 1;
$y = 6 - $x;

$z = $x * $y;

$w = 2**3;  # 2 to the power of 3 = 8
The modulus operator is useful when a program needs to run down a list and do something every few items. This example shows you how to do something every 10 items, mod.pl:
for ($index = 0; $index <= 100; $index++) {

    if ($index % 10 == 0) {

        print("$index ");

    }

}
When this program is run, the output should look like the following:
0 10 20 30 40 50 60 70 80 90 100
Notice that every tenth item is printed. By changing the value on the right side of the modulus operator, you can affect how many items are processed before the message is printed. Changing the value to 15 means that a message will be printed every 15 items. Chapter describes the if and for statement in more detail.

0 comments:

Post a Comment