Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
v3:expressions [2022/02/12 10:20] – created neils | v3:expressions [2024/07/30 22:51] (current) – neils | ||
---|---|---|---|
Line 2: | Line 2: | ||
When no parentheses are present, arithmetic expressions are evaluated from left to right, with multiplication and division having a higher priority than addition and subtraction. | When no parentheses are present, arithmetic expressions are evaluated from left to right, with multiplication and division having a higher priority than addition and subtraction. | ||
+ | |||
+ | The following table explains operator precedence in XC=BASIC: | ||
| Highest | | Highest | ||
Line 9: | Line 11: | ||
| Lowest | | Lowest | ||
- | The arithmetic operators, '' | + | The arithmetic operators, '' |
PRINT 5/2 ' Outputs 2 | PRINT 5/2 ' Outputs 2 | ||
PRINT 5.0/2.0 ' Outputs 2.5 | PRINT 5.0/2.0 ' Outputs 2.5 | ||
+ | Operators, except unary operators, accept two operands. These two operands do not have to be of the same type. In a mixed expression, the operands are promoted to the higher type, being BYTE the lowest and FLOAT the highest type. The table below summarizes the results of a partially evaluated expression. | ||
+ | |||
+ | ^ ^ BYTE ^ WORD ^ INT ^ LONG ^ FLOAT ^ DECIMAL ^ | ||
+ | ^ BYTE | BYTE | WORD | INT | LONG | FLOAT | - | | ||
+ | ^ WORD | WORD | WORD | INT | LONG | FLOAT | - | | ||
+ | ^ INT | INT | INT | INT | LONG | FLOAT | - | | ||
+ | ^ LONG | LONG | LONG | LONG | LONG | FLOAT | - | | ||
+ | ^ FLOAT | FLOAT | FLOAT | FLOAT | FLOAT | FLOAT | - | | ||
+ | ^ DECIMAL | - | - | - | - | - | DECIMAL | | ||
+ | |||
+ | <adm note> | ||
+ | DECIMAL types can not be used together with other types. | ||
+ | </ | ||
+ | |||
+ | The type of the data being operated on must be considered. For example, adding two values of type BYTE will always result in a value which is also of type BYTE, even if the result is too large to fit in a BYTE. For example, if '' | ||
+ | |||
+ | Likewise, adding or multiplying two numeric literals is subject to overflow, even if the left hand side of the assignment is a variable that could otherwise fit the result. | ||
+ | |||
+ | DIM a AS INT | ||
+ | a = 250 + 6 | ||
+ | PRINT a ' Outputs 0 | ||
+ | |||
+ | Since both 250 and 6 are of type BYTE, the result will also be of type BYTE, regardless the type of '' | ||
+ | |||
+ | DIM a AS INT | ||
+ | a = CINT(250) + CINT(6) | ||
+ | PRINT a ' Outputs 256 | ||
+ | <- operators|Previous page ^ expressions|Arithmetic expressions ^ strings|Next page -> |