Arithmetic¶
Arithmetic operators¶
Arithmetic operators perform basic mathematical operations such as addition, subtraction, multiplication, and division on numeric values (constants and variables).
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulo (Remainder) |
| ^ | Power (Exponential) |
Examples:
A common arithmetic operation would be with two numbers.
With two literal numbers:
x = 100 + 50or also with variables:
x = a + bor also with expressions:
x = (100 + 50) * aSubstraction¶
The substaction operator (-):
x = 5
y = 2
z = x - y
escribir(z) //The result would be 3Multiplication¶
The multiplication operator (*):
x = 5
y = 2
z = x * y
escribir(z) //The result would be 10Modulo (Remainder)¶
The remainder operator (%):
x = 5
y = 2
z = x % y
escribir(z) //The result would be 1Power¶
The exponential operator (^):
x = 5
y = 2
z = x ^ y
escribir(z) //The result would be 25Note
For power calculations, you can achieve the same result using the math library mate.pot(x,y)
x = 5
y = 2
escribir(mate.pot(x,y)) //The result would be 25Increment and decrement operators¶
Increment¶
The increment operator is represented by a double addition (+ +).
x = 5 //Declare a variable with value 5
x++ //The value of the variable X increments by 1
escribir(x) //The result would be 6Decrement¶
The decrement operator is represented by a double substraction (- -).
x = 5 //Declare a variable with value 5
x-- //The value of the variable X decrease by 1
escribir(x) //The result would be 4Precedence in Operators¶
In arithmetic, all operators (arithmetic, logical, and relational) have precedence rules that apply when several operators act together, and Latino makes use of those rules.
Arithmetic operators, such as multiplication and division, are performed before addition or subtraction.
To override these precedence rules, parentheses () can be used .
x = 100 + 50 * 3 //Returns 250
y = (100 + 50) * 3 //Returns 450
escribir ("Value of X: " .. x .. ", Value of Y: ".. y)