# Arithmetic Operations

In this section, we will discuss how to perform arithmetic operations, such as addition, subtraction, multiplication, and division.

### Sponsored Link

## Arithmetic Operators

In programming, you can use arithmetic operators to perform various calculations. To perform arithmetic operations, we use arithmetic operators. An "operator" is a symbol used to perform operations, such as "+" for addition or "-" for subtraction. VCSSL supports the following arithmetic operators:

Symbol | Meaning | Supported Types | Details |
---|---|---|---|

+ | Addition | int, float, complex, varint, varfloat, varcomplex, string |
Performs addition of numbers. For the string type, it concatenates strings. |

- | Subtraction | int, float, complex, varint, varfloat, varcomplex |
Performs subtraction of numbers. |

* | Multiplication | int, float, complex, varint, varfloat, varcomplex |
Performs multiplication of numbers. |

/ | Division | int, float, complex, varint, varfloat, varcomplex |
Performs division of numbers. |

% | Remainder | int, varint |
Calculates the remainder when dividing numbers. |

** | Exponentiation | float, varfloat |
Performs exponentiation. Requires importing the Math library. |

In earlier versions of VCSSL (prior to 2.1), the caret symbol (^) was used for exponentiation. However, this symbol is commonly associated with bitwise XOR in other C-like languages. Therefore, starting from VCSSL 2.1, it is recommended to use the double asterisk (**) symbol instead.

Additionally, at present, exponentiation between integers is performed after converting them to floating-point numbers, resulting in a float-type output. To avoid confusion, integer-to-integer exponentiation is deprecated from VCSSL 3.0 onwards. It is recommended to explicitly cast operands to floating-point numbers before exponentiation.

While integer-to-integer exponentiation is currently compatible, there is a possibility that future specifications may modify it to return integer results.

## Compound Assignment Operators and Increment/Decrement Operators

For frequently used operations that involve both arithmetic operation and assignment, special operators (referred to as "compound assignment operators") are provided as follows. These operators are widely adopted in many C-like languages.

Symbol | Meaning | Example |
---|---|---|

+= | Add the right value to the left value and assign the result to the left value. | a += b ; ( a is assigned the result of a+b ) |

-= | Subtract the right value from the left value and assign the result to the left value. | ( a is assigned the result of a-b ) |

*= | Multiply the left value by the right value and assign the result to the left value. | a *= b ; ( a is assigned the result of a*b ) |

/= | Divide the left value by the right value and assign the result to the left value. | a /= b ; ( a is assigned the result of a/b ) |

++ | Increment the left or right value by 1 and assign the result to that value. | a ++ ; ( a is assigned the result of a+1 ) |

-- | Decrement the left or right value by 1 and assign the result to that value. | a -- ; ( a is assigned the result of a-1 ) |

The last two operators, ++ and --, are used extensively for loop counter control, making their usage very common. They are generally referred to as "increment" and "decrement" operators, respectively.

When assigning the result of an increment to another variable, such as 'a = ++b;' or 'a = b++;', the prefix increment ('++b') assigns the value after the increment, while the postfix increment ('b++') assigns the original value (before applying the increment). This principle applies similarly to decrements.

## Performing Addition

As an example of using arithmetic operators, let's perform addition operations. Try the following code:

- Execution Result -

When you execute this program, "2" will be displayed in the VCSSL console. Now, what happens when you do this:

- Execution Result -

This time, "2.0" will be displayed in the message window.

In the first example, it was "2" and an integer, but in the second example, it became "2.0" and a number with a decimal point. This means that the result of the first operation is an integer value, while the result of the second operation becomes a value with a decimal point.

Thus, the result type of an arithmetic operation varies based on the operand types. As explained earlier, we refer to integer types as "int" and types of numbers with decimal points as "float."

The result of an operation between two int values will be an int, and in cases where two float values are involved or when int and float types are mixed in an operation, the result becomes a float value.

Represented in a table, it looks like this:

**[Result of Operation between A and B]**

A: int | A: float | |

B: int | Result: int | Result: float |

B: float | Result: float | Result: float |

Similarly, operations involving at least one "complex" type value will result in a complex type. In other words, when you add complex numbers to the table above, it becomes like this:

**[Result of Operation between A and B]**

A: int or float | A: complex | |

B: int or float | Result: The table above | Result: complex |

B: complex | Result: complex | Result: complex |

## (Important) Be Cautious with Integer Division

Now, the rules we've seen above require some caution when it comes to division.

In general, the results of addition, subtraction, and multiplication between integers are also integers, which is mathematically sound and doesn't pose any issues. However, the same cannot be said for division between integers. In VCSSL, as in many programming languages, dividing one integer by another always returns an integer result. This can lead to tricky pitfalls until you get used to it.

For example, try this:

- Execution Result -

When you execute this program, it displays an integer "0."

Mathematically, it should be "0.5." This is a common behavior among many programming languages.

To obtain a result with a decimal point, cast operands to a float-type like this:

- Execution Result -

When you execute this, it correctly displays "0.5" as the result.

If you are performing operations directly on values and not variables, you can also add ".0" at their tail to make them float-type values:

- Execution Result -

This will also correctly display "0.5." However, note that adding ".0" to variables does not convert their types to float.

## Specifying the Order of Operations

When performing multiple operations, the order of the operations becomes an issue. For example, consider the following case:

- Execution Result -

In such cases, multiplication and division are calculated before addition and subtraction. This means the value of 2*3=6 is added to 1, resulting in an output of 7.

If you want to calculate 1+2 first, enclose it with parentheses ( ) like this:

- Execution Result -

This way, 1+2=3 is calculated first, and then it is multiplied by 3, resulting in the output of 9.

## Precedence of Arithmetic Operators

In expressions containing multiple operators, those with higher precedence are processed first. The precedence is set in the following order:

- Precedence or operators -

This means that multiplication and division have higher precedence and are processed before addition and subtraction. Also, when operators of the same precedence, such as multiplication and division, are adjacent, they are processed from left to right.

## String Concatenation

Also, for the string type, the behaviour of operators differs significantly from other data-types. First, operators other than addition (+) cannot be used for the string type. Moreover, when performing addition between a string type operand and an operand of another data-type, all operands are converted to the string type before the operation.

For example, try executing the following example.

- Execution Result -

When you execute this, it displays "12." This happens because the operation is treated as concatenating the string '1' with the string '2'."

**Acknowledgement**: We greatly appreciate the cooperation of two ChatGPT AIs in translating this article!

» How we translated this article