Arithmetic Expressions

index
BASIC

An arithmetic expression is an expression that results in a numeric value. There are two kinds of numeric values, integers (whole numbers), and real or floating point numbers (numbers containing a decimal point).

The simplest arithmetic expressions are literals (the number itself, written with digits) and variables (named values):

Example Description
12 A literal integer, representing the number 12.
-5 A literal integer, representing the number negative 5.
-5.0 A literal real, representing the number negative 5.
5.0E4 A literal real, representing the number 50000.
count A variable. If it was declared as int count, it will hold an integer value; but if declared as double count, it will hold a real value.

More complex arithmetic expressions can be formed by connecting literals and variables with one of the arithmetic operators:

Operator Meaning
+ Add.
- Subtract.
* Multiply (it's difficult to type the usual multiplication symbol).
/ Divide (it's difficult to type the usual division symbol).
Division of two integer values will give an integer result (any fractional part is just discarded). For example, 14/5 gives 2. This is called integer division.
% Mod (remainder).
Used for integers only, this operation gives the remainder of a division; for example, 14%5 gives 4. The sign (positive or negative) of the result is the same as the sign of the first number.

Parentheses may be used to control the order in which the operators are applied. If you don't use parentheses, operations with higher precedence are done first.

Notes: