Presentation is loading. Please wait.

Presentation is loading. Please wait.

Side Effect Operators Changing the value of variables

Similar presentations


Presentation on theme: "Side Effect Operators Changing the value of variables"— Presentation transcript:

1 Side Effect Operators Changing the value of variables
Copyright Curt Hill

2 There are operators and there are operators
Suppose the following statement: a = 2+(b = c+d*e); The values of a and b are changed The values of c, d and e are not The assignment operator is a side effect operator, while add and multiply are not Copyright Curt Hill

3 How many side effect operators are there?
Most arithmetic or comparison operators are not of the side effect variety However, there are several side effect operators They are added for convenience and conciseness Copyright Curt Hill

4 The Unary Side effect Operators
There are two unary operators that can each be used in two ways The two operators: ++ -- These operators have a different effect if before or after the variable These are handy for statements like: x = x + 1; They are very common in many programs Copyright Curt Hill

5 The Increment Operator
The ++ increments a variable The exact effect depends on position If the ++ follows the variable the effect is: Obtain the value for the rest of the expression Increment the variable If the ++ precedes the variable the effect is: Copyright Curt Hill

6 Example Consider the following code: int a = 2, b = 2, c, d; c = ++a; // Prefix d = b++; // Postfix What values will result? The variables a, b and c will end with three Variable d will end in 2 Copyright Curt Hill

7 Reconsidered Another look: int a = 2,c; c = ++a;
The increment makes a equal to 3 Then the assignment occurs and both end at 3 Then: int b = 2, d; d = b++; The old value of b (2) is given to d Then the increment raises b to 3 Copyright Curt Hill

8 The Decrement The -- works about the same except that it decrements
The positioning is similar: If it follows the decrement occurs after obtaining the value If if precedes the decrement occurs before the value is obtained Copyright Curt Hill

9 Miscellaneous Rules Both ++ and -- have very high precedence, like any unary operator Only a variable may be incremented or decremented Like any side effect operator So ++i++ is not legal These may be used as a statement i++; or as part of an expression: d = a * ++b – c--; Copyright Curt Hill

10 More Side Effect Operators
The increment and decrement are a nice shorthand for a common operation There are others If you look at a lot of code, you see a lot of assignments of the form: x = x # expr; where # is common operator and expr is any expression The most common are: x = x + 1; x = x - 1; but others also exist Copyright Curt Hill

11 Compound Assignments A compound assignment operator is composed of an arithmetic operator attached to an assignment operator Thus: a += 4; is the same as: a = a + 4; Most of the two operand arithmetic operators have corresponding compound assignments: -= *= /= %= Copyright Curt Hill

12 Precedence The only unusual thing about the compound assignment is the precedence The precedence of all assignments is very low It must be to allow the other operators to produce a value This causes some problems concerning when an operation should occur Copyright Curt Hill

13 Example Consider the two assignments: x *= a + b; x = x * a + b;
These are not equivalent, though we might expect them to be We expect multiply to have higher precedence than addition The compound assignment involving multiplication still has the lower precedence of assignment Copyright Curt Hill

14 In other words Thus: x *= a + b; is equivalent to: x = x * (a + b); but not with: x = x * a + b; This is directly from the precedence rules Copyright Curt Hill

15 Precedence Revisited () ++ -- (postfix)
++ -- (prefix) + - (unary) casts * / % + - = *= += -= /= %= Copyright Curt Hill

16 An Example Expression a *= b-- + ++c/2;
All variables are integers and start at 3 First, b’s value of 3 is obtained before decrement Next, c is incremented to 4 Division reduces value to 2 Addition of 3 and 2 give 5 The value of a (3) multiplied with 5 Results: a set to 15, b to 2, c to 4 Copyright Curt Hill

17 A Slightly Different Example
a *= --b + ++c/2; All variables are integers and start at 3 First, b’s value of 3 is decremented Next, c is incremented to 4 Division reduces value to 2 Addition of 2 and 2 give 4 The value of a (3) multiplied with 4 Results: a set to 12, b to 2, c to 4 Only difference is position of – Copyright Curt Hill

18 Where can they be? Usually find side effect operators alone or in an assignment: a += 2*b++ - c--; They may also be in conditions: if(++a == c) or any other place a variable can be: System.out.println(a++); Not on left hand side of an assignment: a++ = 2*r; is illegal Copyright Curt Hill

19 Last Thought Do not use a changed variable twice in an expression such as: c = a++ * 2 – b*a; The problem is: does the a get incremented before or after the multiply? Java has a precise way of handling it, so that it is consistent on every machine However, it is difficult to understand for people Instead use: c = a * 2 – b*a; a++; Copyright Curt Hill


Download ppt "Side Effect Operators Changing the value of variables"

Similar presentations


Ads by Google