Download presentation

Presentation is loading. Please wait.

Published byLandon Goodwin Modified over 4 years ago

1
Computation u When processing data we subject it to a variety of transformations or computations. u The type of data determines the set of valid operations. u Arithmetic would be a typical operation on numeric data. u As we have seen, with strings we often split large values into smaller units or join two or more pieces of text together. u An object has a set of operations defined by its class.

2
Operators for int Data Types u The following operators are defined for the int data types :- +addition -subtraction *multiplication /division yielding quotient (e.g. 8/5 = 1) % division yielding remainder (e.g. 8%5 = 3)

3
Operators for int Data Types u The / and % operators deserve special mention. u If I and J are integers then I / J is the quotient obtained when I is divided by J with the fractional portion dropped when the result is not an integer. u If I and J are integers then I % J is the remainder after dividing I by J. u What is the result of u 8/5 u 8%5 u N%M

4
Operators for float u The following operators are defined for the float and double data types :- + addition - subtraction * multiplication / division u The essential difference between float and int expressions is the type of the result. u Also, the division operator behaves differently. u Obviously there is no % for floats.

5
Problems with Expressions u A problem sometimes arises with the evaluation of expressions. u Consider the statement SomeVariable = 4 + 3 * 7 ; u The value assigned depends on how the computation proceeds. Strategy 1. Evaluate left to right 4 + 3 = 7 * 7 = 49 Strategy 2. Do multiplication first 4 + (3 * 7) = 4 + 21 = 25 u To avoid confusion Java applies the standard mathematical rules for expression evaluation.

6
u When Java encounters an expression involving several operators it evaluates them in the following order OperatorMeaningOrder -Unary minusRight to Left *MultiplicationLeft to Right /Division %Modulus +AdditionLeft to Right -Subtraction Operator Precedence

7
Using Brackets in Expressions u Brackets may be used to change the order of evaluation. For example AverageTemp = HighTemp + LowTemp / 2 should be written AverageTemp = (HighTemp + LowTemp) / 2 u Brackets may also be used to emphasise the order of evaluation. For example SomeVariable = 4 + (3 * 7 )

8
Java ShortCuts - Assignment u We have seen several examples of the importance and frequency of occurrence of counting in algorithms. u Some solutions have included statements of the type count = count + 1 ; u This type of tedious variable name duplication tends to encourage cryptic variable naming.

9
Java ShortCuts - Assignment u The Java language designers have solved this problem by including an Increment operator, a Decrement operator and a set of Combined Operators all of which can be used to abbreviate assignment statements. u It should be noted that the exploitation of shortcuts for frequently occurring operations is widespread in programming languages. u We are interested in how Java does it but other language designers have used similar but different solutions.

10
Java ShortCuts - Assignment u Java uses ++ as an Increment operator. u Java uses -- as a Decrement operator. u Thus Count++ ; is equivalent to Count = Count + 1 ; Count-- ; is equivalent to Count = Count - 1 ; i++ ; is equivalent to i = i +1 u The Increment and Decrement operators are exploited extensively by Java programmers so it is worthwhile becoming familiar with them.

11
Java ShortCuts - Assignment u Obviously, the Increment and Decrement operators only substitute for the addition and subtraction operators and only allow the variables to be incremented or decremented by 1. u The Combined Operators provide a more general mechanism for abbreviating assignment statements which involve the same variable name on both sides of the assignment operator.

12
Java ShortCuts - Assignment OperatorExampleEquivalent +=Total += CountTotal=Total + Count N += 5N = N + 5 -=Total -= CountTotal=Total - Count N -= 5N = N - 5 *=Salary *= 1.1Salary=Salary * 1.1 /=Loot /= GangSizeLoot=Loot/GangSize %=Pence %= 100Pence = Pence % 100

13
C ShortCuts - Assignment u When using the Combined Operators it is important to note that the variable to the left of the assignment operator is applied to the complete expression on the right. u For example Salary *= PercentageIncrease + 1 is equivalent to Salary = Salary * (PercentageIncrease + 1) and not Salary = Salary * PercentageIncrease + 1

14
C ShortCuts - Assignment u Frequently we encounter situations where a collection of variables have to be assigned the same value. u We can of course code individual statements for these assignments, however, Java provides a multiple assignment feature which allows the same value to be assigned to a collection of variables. Thus a = b = c = 6 height = width = depth = 12

Similar presentations

OK

Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.

Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

View my ppt online View ppt on ipad Ppt on steam turbine manufacturing download Ppt on technical analysis of indian stocks Ppt on save environment poster Ppt on cross site scripting error Ppt on magic and science Ppt on bluetooth technology download Ppt on information technology companies Free ppt on uses of water