Shortcut Operators

A common operation in programming is adding 1 to a number (incrementing) or subtracting 1 from a number (decrementing). For example, if you were counting how many data items the user entered, every time you read another data item, you would add 1 to a count variable. Because incrementing or decrementing a value is so common in programming, Java provides shortcut operators to do this: ++ and −−. (Note that there are no spaces between the two plus and minus signs.) The statement

Screen Shot 2018-12-18 at 6.32.15 AM

adds 1 to the value of count, and the statement

Screen Shot 2018-12-18 at 6.34.07 AM

subtracts 1 from the value of count. Thus,

Screen Shot 2018-12-18 at 6.32.15 AM

is equivalent to

Screen Shot 2018-12-18 at 6.41.41 AM.png

and

Screen Shot 2018-12-18 at 6.34.07 AM

is equivalent to

Screen Shot 2018-12-18 at 6.44.23 AM.png

Both of these operators have prefix and postfix versions. The prefix versions precede the variable name (++a or −−a) whereas the postfix versions follow the variable name (a++ or a−−). Both increment or decrement the variable. If they are used as a single, atomic statement (as in the preceding statements), there is no difference between the two versions. So

Screen Shot 2018-12-18 at 5.21.36 PM.png

is functionally equivalent to

Screen Shot 2018-12-18 at 5.23.44 PM.png

and

Screen Shot 2018-12-18 at 5.31.30 PM

is functionally equivalent to

Screen Shot 2018-12-18 at 5.33.27 PM

However, if they are used inside a more complex expression, then they differ as follows: The prefix versions increment or decrement the variable first, then the new value of the variable is used in evaluating the expression. The postfix versions increment or decrement the variable after the old value of the variable is used in the expression.

The example below illustrates the difference:

Screen Shot 2018-12-18 at 5.44.24 PM

Lines 9 and 10 declare and initialize two int variables, a and b, to 6 and 2, respectively. In order to illustrate the effect of both the prefix and postfix increment operators, we output their original values at lines 12 and 16. At line 13, we use the prefix increment operator to increment a inside an output statement; a is incremented before the output statement is executed, resulting in the output statement using the value 7 for a. At line 17, we use the postfix increment operator to increment b inside an output statement; b is incremented after the output statement is executed, resulting in the output statement using the value 2 for b. Lines 14 and 18 simply output the values of a and b after the prefix and postfix operators were used at lines 13 and 17. Below is the output of this example:

Screen Shot 2018-12-18 at 5.51.46 PMAnother set of shortcut operators simplify common calculations that change a single value. For example, the statement

Screen Shot 2018-12-18 at 5.53.22 PM

can be simplified as

Screen Shot 2018-12-18 at 5.53.29 PM.png

The value added to the target variable can be a variable name or a larger expression. The shortcut addition operator (+=) is a single operator; there are no spaces between the + and the =. Also, be careful not to reverse the order of the operators. For example, in the following statement, the operators are reversed, so the compiler interprets the statement as “assign a positive 2 to a.”

Screen Shot 2018-12-18 at 5.57.19 PM

Java provides shortcut operators for each of the basic arithmetic operations: addition, subtraction, multiplication, division, and modulus. These operators are especially useful in performing repetitive calculations and in converting values from one scale to another. For example, to convert feet to inches, we multiply the number of feet by 12. So we can use the *= shortcut operator:

Screen Shot 2018-12-18 at 5.59.04 PM.png

Converting from one scale to another is a common operation in programming. For example, in an earlier post we invented a concept and partitioned it into its constituent at different levels of granularity. You might also need to convert hours to seconds, feet to square feet, or Fahrenheit temperatures to Celsius.

Below are demonstrated each of the shortcut arithmetic operators and the output.

Screen Shot 2018-12-18 at 6.36.59 PM

These are the shortcut operators:

Screen Shot 2018-12-18 at 6.38.10 PM

As with lobsters, humans, angels, and superintelligences, operators have a hierarchy/ order of precedence:

Screen Shot 2018-12-18 at 6.39.12 PM

Those at the top are always first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s