Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic Expressions & Data Conversions

Similar presentations


Presentation on theme: "Arithmetic Expressions & Data Conversions"— Presentation transcript:

1 Arithmetic Expressions & Data Conversions

2 Arithmetic Expressions
An expression is a combination of one or more operands and their operators Arithmetic expressions compute numeric results and make use of the arithmetic operators: Addition + Subtraction - Multiplication * Division / Remainder % With arithmetic expressions, two ints will always result in an int but if either or both operands associated with an arithmetic operator are floating point (decimal), the result is a floating point.

3 Division and Remainder
If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) Any attempt to divide by zero will produce a run-time error called an ArithmeticException. The program crashes when it tries to do a divide by zero operation. 14 / equals? 4 8 / equals? When you divide an int by an int in Java, the result is an int. This is referred to as integer division. The decimal portion of the answer is ignored. The technical way to say it is that the result is truncated, which means that the decimal point of the answer is discarded. It is never rounded. For instance, if the you divided two ints and the answers was , in Java the int division answer is 13. If either of the operands is a double, the result will be a double but if both operands are ints, the result if an int.

4 Modulus (Remainder) The remainder operator (%), known as modulus, returns the remainder after dividing the second operand into the first The modulus (or mod) operator appears often on the AP Computer Science A exam. White is not deliberately taught in most math classes, it is something that you are familiar with. The modulus operator uses the percent system, and produces the remainder after doing a division. Back in grade school you have been taught that the answer to14 divided by 3 was 4 R2, where the R stood for remainder. With the modulus, always think long-division The modulus operator is great for determining if a number is even or odd. If someNumber % 2 = 0, then someNumber is event. Or, if someNumber % 2 = 1, then someNumber is odd.

5 result = total + count / max - offset;
Operator Precedence Operators can be combined into complex expressions result = total + count / max - offset; Operators have a well-defined precedence which determines the order in which they are evaluated Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Arithmetic operators with the same precedence are evaluated from left to right Parentheses can be used to force the evaluation order Java handles all mathematical calculations using the order of operations that you learning in math class. The order of operations does exactly that: it tells you the order to evaluate any expression that may contain parentheses, multiplication, division, addition, subtraction, and so on.

6 Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated answer = sum / 4 + MAX * lowest; 4 1 3 2 Then the result is stored in the variable on the left hand side

7 Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count count = count + 1; Then the result is stored back into count (overwriting the original value)

8 Data Conversions Sometimes it is convenient to convert data from one type to another For example, we may want to treat an integer as a floating point value during a computation Conversions must be handled carefully to avoid losing information Widening conversions are safest because they usually do not lose information (int to double) Narrowing conversions can lose information (double to int)

9 Data Conversions In Java, data conversions can occur in three ways:
assignment conversion arithmetic promotion casting Assignment conversion occurs when a value of one type is assigned to a variable of another Only widening conversions can happen via assignment Arithmetic promotion happens automatically when operators in expressions convert their operands

10 result = (double) total / count;
Casting Casting is the most powerful, and dangerous, technique for conversion Both widening and narrowing conversions can be accomplished by explicitly casting a value To cast, the type is put in parentheses in front of the value being converted For example, if total and count are integers, but we want a floating point result when dividing them, we can cast total: result = (double) total / count; On the AP CSA exam, you will be tested on the correct way to cast variables. Casting is a way to tell a variable to temporarily become a different data type for the sake of performing some action, such as division. The most common types of casts are from an int to a double or a double to an int.

11 Try an example result = total / count; result = ?
int total = 16; count 5; double result; result = total / count; result = ? result = (double) total / count; result = ? Result = (double) (total / count); result = ? Let’s try an example. Let’s say that total and count are ints and have been initialized with the values 16 and 5. Result has been declared as a double.


Download ppt "Arithmetic Expressions & Data Conversions"

Similar presentations


Ads by Google