Side Effect Operators Changing the value of variables

Side Effect Operators Changing the value of variables Copyright © 2004 - 2016 Curt Hill

There are operators and there are operators • Consider 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 • The >> when used with cin is also • While << when used with cout only changes cout Copyright © 2004 - 2016 Curt Hill

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 © 2004 - 2016 Curt Hill

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 © 2004 - 2016 Curt Hill

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: – Increment the variable – Obtain the value for the rest of the expression Copyright © 2004 - 2016 Curt Hill

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 © 2004 - 2016 Curt Hill

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 © 2004 - 2016 Curt Hill

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 © 2004 - 2016 Curt Hill

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 © 2004 - 2016 Curt Hill

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 © 2004 - 2016 Curt Hill

Compound Assignments • A compound assignment operator is composed of an arithmetic or other 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 © 2004 - 2016 Curt Hill

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 is perceived to occur Copyright © 2004 - 2016 Curt Hill

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 © 2004 - 2016 Curt Hill

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 © 2004 - 2016 Curt Hill

Precedence Revisited • () • ++ -- (postfix) • ++ -- (prefix) + - (unary) casts • * / % • + • = *= += -= /= %= Copyright © 2004 - 2016 Curt Hill

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 © 2004 - 2016 Curt Hill

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 © 2004 - 2016 Curt Hill

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: cout << a++; • Not on left hand side of an assignment: a++ = 2*r; is illegal Copyright © 2004 - 2016 Curt Hill

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? • C++ does not have a precise way of handling it, so that it may be different on any machine • However, it is difficult to understand for people • Instead use: c = a * 2 – b*a; a++; Copyright © 2004 - 2016 Curt Hill
- Slides: 19