Code: Select all
#define LENGTH 16
if idx > LENGTH*3/4 then print "Almost full"
Code: Select all
if idx > 16*3/4 then print "Almost full"
Also, how does the compiler handle the following examples, where variables and literal expressions are mixed?
Code: Select all
dim a as integer
a = 3
' I would expect these expressions to be evaluated in the following ways:
print a*(5/2) ' => a*(2) [6] (A)
print a*5/2 ' => a*5/2 [7] (B)
print 5*2*a ' => (10)*a [30] (C)
print a*5*2 ' =? a*(10) [30] (D)
print a*6/2 ' =? a*(3) [9] (E)
Expressions D and E are less obvious. Though optimization would change the order in which the expression is evaluated, the result should be the same, unless integer overflow occurs. This would also be more difficult for the compiler to detect, as these situations often cannot be identified by syntax alone.
Edit:
What if I use `CONST` instead of `#define` in the first example? As in:
Code: Select all
CONST LENGTH = 16
if idx > LENGTH*3/4 then print "Almost full"