Decision Making: Equality and Relational Operators
Memory Concepts:
Variable names correspond to locations in the computer's memory. Every variable has a name, type, size, and a value. Whenever a value is placed in a memory location, the value replaces the previous value in that location. The previous value is destroyed (i.e. lost). Consider the statement:
sum = number1 + number2;
everything on the right hand side must be known, that is the value of number1 and number2. The statement adds these values and stores them in a location called sum. The values of number1 and number2 are unchaged. When a value is read from a memory location, the process is nondestructive.
Arithmetic:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
y |
|
|
|
|
|
|
Note: The multiplication sign * is required and not
implicit as in algebra. Integer division yields an integer quotient,
so 7 / 4 is 1 and not 1.75. The number is not rounded. Modulus
is the remainder after integer division. So 7 % 4 yields 3.
There is no arithmetic for exponention in Java. We have to use a
library function Math for that. The order of operations is
the same as in algebra.
|
|
|
|
|
|
|
Evaluated first. If the parentheses are nested, the expressin in the innermost pair is evaluated first. If there are several pairs f parentheses on the same level, they are evaluated from left to right |
|
|
Multipication,
Division, and Modulus |
Evaluated second. If there are several of this type of operator, they are evaluated from left to right. |
|
|
Addition
Subtraction |
Evaluated last. If there are several of this type of operator, they are evaluated from left to right. |
To find the average of five numbers:
In algebra:
m = a + b + c + d + e
5
In Java: m = ( a + b + c + d
+ e ) / 5; If the parentheses are not used, because of the order
of precedence,
m will be equal to a + b
+ c + d + e/5.
Decision Making: Equality and Relational Operators
Java's if structure allows a program to make a decision
based on the truth or falsity of some condition. If the condition
is satisfied, the condition is true and the statement(s) in the body of
the if structure is executed. Otherwise, the statement(s)
will not be executed. Conditions in if structures can be formed
by using the equality operators and relational operators.
|
|
|
|
|
| Equality Operators | |||
|
|
|
|
|
|
|
|
|
|
| Relational Operators | |||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The relational operators hae the same order of precedence and operate from left to right. The equakity operators have the same level of precedence, operate from left to right, and have a lower order of precedence than the relational operators.