Mathematical operators

Jump to navigationJump to search

The mathematical operators perform basic math operations, such as addition and subtraction. The values that the operators act on are called operands.

Syntax and Operation

Addition

The addition operator + (plus sign) returns the sum of two values (i.e., it adds two values). In the following example, the value 12 would be stored in the variable x.

```x = 10 + 2;
```

Note that the order of the operands is not important (that is, a + b is the same as b + a).

Subtraction

The subraction operator - (minus sign) returns the difference of two values (i.e., it subracts the right-hand value from the left-hand value). In the following example, the value 8 would be stored in the variable x.

```x = 10 - 2;
```

Note that the order of the operands is important (that is, a - b is not the same as b - a).

Multiplication

The multiplication operator * (asterisk) returns the product of two values (i.e., it multiplies one value by the other). In the following example, the value 20 would be stored in the variable x.

```x = 10 * 2;
```

Note that the order of the operands is not important (that is, a * b is the same as b * a).

Division

The division operator / (forward slash) returns the quotient of two values (i.e., it divides the left-hand value by the right-hand value). In the following example, the value 5 would be stored in the variable x.

```x = 10 / 2;
```

Note that the order of the operands is important (that is, a / b is not the same as b / a).

Modulus

The modulus operator % (percent sign) returns the remainder of a division operation of two values. In the following example, the value 2 would be stored in the variable x, because 4 goes into 10 twice (4 * 2 = 8), with 2 left over.

```x = 10 % 4;
```

Note that the order of the operands is important (that is, a % b is not the same as b % a).