MEMORY AND ARITHMETIC

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:
 
 
Java Operation
Arithmetic Operator
Algebraic Expression
Java Expression
Addition
+
f + 7
f + 7
Subtraction
-
p - c
p - c
Multiplication
*
bm
b * m
Division
/
x/y or x
          y
x / y
Modulus
%
r mod s
r % s

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.
 
 
Operator(s)
Operation(s)
Order of Evaluation (Precedece)
()
Parentheses
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
*, / and %
Multipication,
Division, and
Modulus
Evaluated second.  If there are several of this type of operator, they are evaluated from left to right.
+ and -
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.
 
Standard algebraic equality or relational operator
Java equality or relational operator
Example of Java condition
Meaning of Java Condition
Equality Operators      
equal
==
x == y
x is equal to y
not equal
!=
x != y
x is not equal to y
Relational Operators      
greater than
>
x > y
x is greater than y
less than
<
x < y
x is less than y
greater than or equal to
>=
x >= y
x is greater than or equal to y
less than or equal to
<=
x <= y
x is less than or equal to y

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.