Arithmetic Operators


The following table shows the arithmetic operators defined by generic.

Operator Meaning
+ addition
- subtraction and unary minus
* multiplication
/ division
% modulo
++ increment
-- decrement

The operators +, -, * and / act in a standard way that conforms to intuition. These operators also act in conformance to the operators of standard algebra. These operators can be applied to any of the builtin numeric data types.

Please consider the next example program - shown below.

// arithmetic_operators.txt - some simple arithmetic operators

generic arithmetic_operators
{
    arithmetic_operators()
    {
        i = 2
        l = 3
        n = i * l
        s = i.to_string() + " * " + l.to_string() + " == " + n.to_string()
        s.println()

        d1 = 10.0
        d2 = 3.0
        d3 = d1 / d2
        s = d1.to_string() + " / " + d2.to_string() + " == " + d3.to_string()
        s.println()
    }
}

The output of this program is shown below.

2 * 3 == 6
10 / 3 == 3.333333333333333481363069950020872056484222412109375

The first computation is to multiply integer 2 and 3 and it gives 6 (no surprises). The next multiplication and division work as expected.

Increment and Decrement Operators

The expression:

x = x + 1

is the same as the expression:

x++

and the expression

x = x - 1

is the same as the expression:

x--

The increment and decrement operators can be used only in the postfix form. Consider the next program.

//increment_operators.txt-- postfix version of operators

generic increment_operators
{
    increment_operators()
    {
        i = 1
        g = i++
        s = "i == " + i.to_string() + " g == " + g.to_string()
        s.println()
    }
}

The output of this program is as follows.

i == 2 g == 2

Examining the output it becomes apparent that i++ delivers 2.