Java

TOPIC 07 – ARITHMETIC EXPRESSIONS

 

 

LESSON NOTE

 

 

ASSIGNMENT STATEMENTS

 

We have seen that if we want to store a value in a variable, we can use a statement such as the following:

 

            x = 8;

 

The statement above of course requires that the variable x already be declared.

 

Statements such as above that assign a value to a variable are called assignment statements.

 

It is important that both sides of the statement be of the same type.  More on this later.

 

GENERAL FORM

 

In general, assignment statements have the following form:

 

            varName = value;

 

LITERALS & TYPES

 

The numbers as they appear in assignment statements are what is called literals.  It is important to note that these literals have a type.

 

The number 7 will be an integer while the number 8.3 will be a double.  If you want the number 7 to be taken as a double, then you need to put 7.0.

 

COMMON ERROR – UNMATCHING TYPES

 

A common error is to try to store a double value inside of an integer.

 

            int c = 7.5;   //will cause an error

 

The opposite does not cause an error as Java does a built-in conversion.

 

            double b = 7;   //no error, Java converts 7 to 7.0


ARITHMETIC EXPRESSION

 

An arithmetic expression is simply an expression consisting of one or more arithmetic operations.  The value part of an assignment statement can be an arithmetic expression that the computer will calculate into a value.

 

EXAMPLES OF SIMPLE ARITHMETIC EXPRESSIONS

 

Here are examples of assignment statements containing arithmetic expressions:

 

            totalMinutes = 72 + 70 + 71 + 70;

 

            difference = 109 – 71;

 

            q = 9 / 7;

 

MAY CONTAIN VARIABLES

 

Arithmetic expressions can contain numbers, operators and variables.  Note that variables inside the expression must be initialized (must have a value).


EXAMPLE

 

Here is a program that shows arithmetic expressions that contain previously initialized variables.

     double diameter = 12.5

     double radius = diameter / 2;

     double circumference = 2 * 3.14 * radius;

     double area = 3.14 * radius * radius;

 

Note that for the program above to do anything of value, we would have to output to screen the variables of interest.

 

COMMON ARITHMETIC OPERATORS

 

Here are the common arithmetic operators used in Java.  Of note, we use the asterisk to denote multiplication.  We also use the slash to denote division.

 

Meaning

Operator

Example

Addition

+

a + b

Subtraction

-

a – b

Multiplication

*

a * b

Division

/

a / b

Modulo (remainder)

%

a % b

Increment by 1

++

a++

Decrement by 1

--

a--

 

BEDMAS

 

The order of which all operations inside an arithmetic expression follows BEDMAS. The % operator has the same precedence as multiplication and division.

 

To use brackets, you use the following symbols:  (  )

 

BEDMAS EXAMPLES

 

In each example below, try to figure out what value the variable answer will get.

 

a) x = 3 + 4 * 2;

b) x = 2 * 5 + 14 / 2;

c) x = 2 + 10 / 2

 

The solutions are 11, 17 and 7.

 

INTEGER DIVISION

 

When we do a division, if the two numbers involved are integers, the result will also be an integer. Therefore, 11 / 2 is 5 while 11.0/2.0 is 5.5.

 

TYPE CASTING

 

We saw earlier that we cannot give a double value to an integer variable.  This would lead to an error.

 

If we ever need to convert a double value to an int value, we can use type casting to do so. Type casting simply means to force the conversion of data from one type to another.

 

To type cast from double to int, we use the following:

 

double d = 2.93;

int x = (int)d;

 

The (int) automatically converts the value of d to an integer. However, it is important to understand that the decimal places are simply removed (truncated). There is no rounding that takes place. In the code above, x would get 2.

 

VARIABLES ON BOTH SIDES OF ASSIGNMENT STATEMENTS

 

An assignment statement can have the same variable appear on both sides of the assignment statement. 

 

Simply remember that the right hand side is calculated by the computer without any consideration for the left hand side.  Once an answer is calculated, it is stored in the variable that appears on the left hand side.

 

Here are some examples:

 

Example 1 – Increasing a variable’s value by one

 

                       count = count + 1;

 

Example 2 – Decreasing a variable’s value by one

 

                       counter = counter  - 1;

 

Example 3 – Doubling a variable’s value

 

                       num = num * 2;

 

Example 4 – Squaring a variable’s value

 

                       x = x * x;