|
|
|
| Expression | Value | | 3 + 6 | 9 | | 3.4 - 6.1 | -2.7 | | 2 * 3 | 6 | | 8 / 2 | 4 | | 8.0 / 2.0 | 4.0 | | 8 / 8 | 1 | | 8 / 9 | 0 | | 8 / 7 | 1 | | 8 % 8 | 0 | | 8 % 9 | 8 | | 8 % 7 | 1 | | 0 % 7 | 0 | | 5 % 2.3 | error (both operands must be integers) |
|
|
|
|
|
|
Be careful with division and modulus. The expressions 7.0 / 0.0, 7 / 0, and 7 % 0 all produce errors. The computer cannot divide by zero. |
|
|
|
|
|
|
|
|
Because variables are allowed in expressions, the following are valid assignments: |
|
|
|
|
|
|
|
|
alpha = num + 6;
alpha = num / 2;
num = alpha * 2;
num = 6 % alpha;
alpha = alpha + 1;
num = num + alpha; |
|
|
|
|
|
|
|
|
Notice that the same variable can appear on both sides of the assignment operator. In the case of |
|
|
|
|
|
|
|
|
the value in num and the value in alpha are added together, then the sum of the two values is stored into num, replacing the previous value stored there. This example shows the difference between mathematical equality and assignment. The mathematical equality |
|
|
|
|
|
|
|
|
is true only when alpha equals zero. The assignment statement |
|
|
|
|
|