So can a computer
That includes syntax for describing arithmetic expressions
I will take advantage of your knowledge in arithmetic expressions from your elementary school education.
|
All of the floating point (arithmetic) operators should look familiar to you...
| Operator symbol | Operation | Note |
|---|---|---|
| + | addition | Binary operator, e.g.: 4.0 + 5.0 = 9.0 |
| − | subtraction | Binary operator, e.g.: 4.0 − 5.0 = −1.0 |
| * | multiplication | Binary operator, e.g.: 4.0 * 5.0 = 20.0 |
| / | division | Binary operator, e.g.: 4.0 / 5.0 = 0.8 |
| ( ... ) | brackets | Changes order of computation |
| − | negation |
Changes the sign of the value:
− 4.0 = (−4.0)
(Read as: − 4.0 = negative 4.0) |
Result of the integer operation 5.0 + 3.0 is 8.0 Result of the integer operation 5.0 - 3.0 is 2.0 Result of the integer operation 5.0 * 3.0 is 15.0 Result of the integer operation 5.0 / 3.0 is 1.666666666666... Use of brackets changes the order of the operations |
|
public class AreaOfCircle
{
public static void main(String[] args)
{
double r; // variable containing the radius
double area; // variable containing the area
r = 4; // Give the radius
area = 3.1415 * r * r; // Compute the area of the circle
System.out.print("The radius = ");
System.out.println(r);
System.out.print("The area = ");
System.out.println(area);
}
}
|
Notes:
|
| Operator | Priority | Note |
|---|---|---|
| ( .... ) | Highest | |
| − (negation) | Higher | Unary operator, e.g.: −3.0 |
| * / | High | Binary operator, e.g.: 4.0 * 5.0 |
| + − | Lowest | Binary operator, e.g.: 4.0 + 5.0 |
|
Floating point expression: 22.0 - 3.0 * 4.0
Evaluated as follows: 22.0 - 3.0 * 4.0
= 22.0 - 12.0
= 10.0
|
Floating point expression: (22.0 - 3.0) * 4.0
Evaluated as follows: (22.0 - 3.0) * 4.0
= 19.0 * 4.0
= 76.0
|
Floating point expression: 22.0 - - 3.0 * 4.0
Evaluated as follows: 22.0 - - 3.0 * 4.0
= 22.0 - (-3.0) * 4.0
= 22.0 - (-12.0)
= 34.0
|
How to run the program:
|
public class Average
{
public static void main(String[] args)
{
double a, b, c, avg; // Define 4 variables
a = 3.0;
b = 4.0;
c = 6.0;
avg = (a + b + c)/3.0;
System.out.print("The average = ");
System.out.println(avg);
}
}
|
How to run the program:
|
|
Example of left-associative operators:
|
Example:
Floating point expression: 7.0 - 2.0 - 1.0
Evaluated as follows: 7.0 - 2.0 - 1.0 (group from left to right)
5.0 - 1.0
4.0
|
|
Example of right-associative operators:
|
Example:
Floating point expression: - - - - 1.0
Evaluated as follows: - - - - 1.0
= - - - (-1.0) (Note: (-1.0) denotes negative 1.0
= - - (1.0)
= - (-1.0)
= 1.0
|
How to run the program:
|
|
Floating point expression:
7.0 / 2.0 - 6.0 * 2.0 / 4.0 - 4.0 / 2.0 / 2.0
Evaluated as follows:
1. Make groups containing only operators of the highest priority
(7.0 / 2.0) - (6.0 * 2.0 / 4.0) - (4.0 / 2.0 / 2.0)
2. Within each group, apply the operator associativity rule:
(7.0 / 2.0) - (6.0 * 2.0 / 4.0) - (4.0 / 2.0 / 2.0)
= (3.5) - (12.0 / 4.0) - (2.0 / 2.0)
= (3.5) - (3.0) - (1.0)
= 3.5 - 3.0 - 1.0
Repeat:
1. Make groups containing only operators of the highest priority
(3.5 - 3.0 - 1.0) (only one group !)
2. Within each group, apply the operator associativity rule:
(3.5 - 3.0 - 1.0)
= (0.5 - 1.0)
= -0.5
|