Game Development Reference
In-Depth Information
4.2.5 Priority of Operators
When multiple operators are used in an expression, the regular arithmetic rules
of precedence apply: 'multiplication before addition'. The result of the expression
1
3 therefore is 7, not 9. Addition and subtraction have the same priority, and
multiplication and division as well.
If an expression contains multiple operators of the same priority, then the expres-
sion is computed from left to right. So, the result of 10
+
2
2 equals 3, not 7. When
you want to deviate from these standard precedence rules, parentheses can be used,
for example ( 1
5
5 ) . In practice, these expressions will gener-
ally also contain variables, otherwise you could have calculated the results (9 and 4)
yourself.
Using more parentheses than needed is not forbidden: 1
+
2 )
3 and 3
+
( 6
+ ( 2
3 ) , you can go
completely crazy with this if you want: (( 1 ) + ((( 2 )
3 ))) . However, your program
will be a lot harder to read if you do.
In summary, an expression can be a constant value (such as 12), it can be a
variable, it can be another expression in parentheses, or it can be an expression
followed by an operator followed by another expression. As a result, this is the
syntax diagram representing an expression:
4.2.6 Other Numerical Types
Another numerical type is the double type. Variables of that type can contain num-
bers with decimals. After the declaration:
double d;
We can give the variable a value with the assignment operation:
d = 3.141592653;
Variables of type double can also contain whole numbers:
d = 10;
Search WWH ::




Custom Search