Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
v3:expressions [2022/02/12 10:33] – 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 automatically, where BYTE is the lowest and FLOAT is the highest type. The table below summarizes the results of a partially evaluated expression | + | 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 | | ||
- | | ^ BYTE ^ WORD ^ INT ^ FLOAT ^ | + | <adm note> |
- | ^ BYTE | BYTE | WORD | INT | FLOAT | | + | DECIMAL types can not be used together with other types. |
- | ^ WORD | WORD | WORD | INT | FLOAT | | + | </ |
- | ^ INT | INT | INT | INT | FLOAT | | + | |
- | ^ FLOAT | FLOAT | FLOAT | FLOAT | FLOAT | | + | |
- | 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 '' | + | 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 two numeric literals | + | Likewise, adding |
DIM a AS INT | DIM a AS INT | ||
a = 250 + 6 | a = 250 + 6 | ||
- | PRINT a ' Outputs | + | PRINT a ' Outputs |
- | Surprisingly at first glance, since both 250 and 6 are of type BYTE, the result will also be of type BYTE, regardless the type of '' | + | 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 | DIM a AS INT | ||
Line 36: | Line 44: | ||
PRINT a ' Outputs 256 | PRINT a ' Outputs 256 | ||
+ | <- operators|Previous page ^ expressions|Arithmetic expressions ^ strings|Next page -> |