Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
v3:expressions [2022/02/12 10:39] neilsv3: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    | ''NOT'', ''-'' (unary) | | Highest    | ''NOT'', ''-'' (unary) |
Line 9: Line 11:
 | Lowest    | ''AND'', ''OR'', ''XOR'' | | Lowest    | ''AND'', ''OR'', ''XOR'' |
  
-The arithmetic operators, ''+'', ''-'', ''*'', and ''/'', work in the expected fashion, however, the result of arithmetic on type BYTE, WORD or INT cannot have a fractional result. Therefore ''5 / 2'' evaluates as 2, not 2.5 (any fraction is always discarded). However, ''5.0 / 2.0'' evaluates as 2.5, because the presence of the decimal point tells the compiler that the numbers are FLOAT.+The arithmetic operators, ''+'', ''-'', ''*'', and ''/'', work in the expected fashion, however, the result of arithmetic on type BYTE, WORDINT or LONG cannot have a fractional result. Therefore ''5 / 2'' evaluates as 2, not 2.5. However, ''5.0 / 2.0'' evaluates as 2.5, because the presence of the decimal point tells the compiler that the numbers are of type FLOAT.
  
   PRINT 5/2 ' Outputs 2   PRINT 5/2 ' Outputs 2
Line 16: Line 18:
 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. 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 ^ FLOAT ^ +             ^ BYTE            ^ WORD          ^ INT ^ LONG ^ FLOAT ^ DECIMAL 
-^ BYTE    | BYTE          | WORD        | INT | FLOAT | +^ BYTE    | BYTE          | WORD        | INT | LONG | FLOAT |  -  
-^ WORD  | WORD        | WORD        | INT | FLOAT | +^ WORD  | WORD        | WORD        | INT | LONG | FLOAT |  -  
-^ INT        | INT             | INT              | INT | FLOAT | +^ INT        | INT             | INT              | INT | LONG | FLOAT |  -  | 
-^ FLOAT  | FLOAT        | FLOAT        | FLOAT | 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. 
 +</adm>
  
-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 ''x'' is a variable of type BYTE which has been previously assigned the value of 254, then the expression ''x + 4'' will NOT have a value of 258, but 2. This is because BYTE variables can only take on values between and 255, so that when you add 4 to 254, the result is (258-256) = 2.+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 ''x'' is a variable of type BYTE which has been previously assigned the value of 254, then the expression ''x + 4'' will NOT have a value of 258, but 2. This is because BYTE variables can only take on values between and 255, so that when you add 4 to 254, the result is (258-256) = 2.
  
-Likewise, adding two numeric literals are subject to overflow, even if the left hand side of the assignment is a variable that could fit the result.+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   DIM a AS INT
Line 30: Line 38:
   PRINT a ' Outputs 0   PRINT a ' Outputs 0
  
-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 ''a''. To overcome this problem, use the typecasting functions [[v3:cbyte|]], [[v3:cword|]], [[v3:cint|]] and [[v3:cfloat|]].+Since both 250 and 6 are of type BYTE, the result will also be of type BYTE, regardless the type of ''a''. To overcome this problem, use the typecasting functions [[v3:cbyte|]], [[v3:cword|]], [[v3:cint|]] and [[v3:cfloat|]].
  
   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 ->