Difference between revisions of "Increment and Decrement Operators"

From CompSciWiki
Jump to: navigation, search
Line 23: Line 23:
 
</pre>
 
</pre>
  
When the expression is only the increment or decrement operator, there is no difference between post and pre. The results are the same. You can change ++number with number++ and still get the same results.
+
When an expression is only the increment or decrement operator, there is no difference between post and pre. The results are the same. You can change ++number with number++ and still get the same results.
  
 
These increment and decrement expressions are really shorthand for:
 
These increment and decrement expressions are really shorthand for:

Revision as of 15:58, 1 December 2007

COMP 1010 Home > Java Fundamentals


Introduction

Increment and decrement are arithmetic operators which quickly add or subtract one from a variable. These operators work on basic data types such as int and long. These operators can appear as an expression by themselves or as part of a complex expression. Increment and decrement can be used to add or subtract one from a variable.

   

{{{Body}}}

The Increment and Decrement Operators

The increment and decrement operators increase or decrease the value of a variable by 1. The increment operator is ++ and decrement is --. These operators can be used on int, long, float and double data types. They are primarily used on integer value variables such as int and long. Increasing or decreasing the value of a variable can be done by using the increment or decrement operators.

Usage

There are two ways to use these operators. We can use these operators on their own along with a variable, or as part of a larger expression. Using them as part of a larger expression may be difficult depending on the complexity of the expression. As useful as these operators are, we should always keep in mind that code readability is important. Care should be taken when using these operators in complex expressions. We look at the two ways of using increment and decrement below.


We can use these operators by placing them before or after the variable. If the operator is placed before a variable, it is known as preincrement or predecrement. Pre - increment/decrement means the variable is increased or decreased before we use it for anything else. If the operator is placed after a variable, it is known as postincrement or postdecrement. Post - increment/decrement means the variable is used by the expression and then modified after.

The following code snippet demonstrates the difference between these four operators:

  int number = 5;

  ++number; // preincrement, the variable 'number' would be 6 as a result
  number++; // postincrement, the variable 'number' would be 7 as a result
  --number; // predecrement, number = 6
  number--; // postdecrement, number = 5

When an expression is only the increment or decrement operator, there is no difference between post and pre. The results are the same. You can change ++number with number++ and still get the same results.

These increment and decrement expressions are really shorthand for:

  number = number + 1; // longer way of writing number++ or ++number
  number = number - 1; // longer way of writing number-- or --number

Note that the regular rules for arithmetic in Java apply here, so be careful not to unintentionally increment or decrement beyond the range a variable can hold.

Within an Expression

These increment and decrement operators can occur as part of a larger expression as well. One might write the operator this way for conciseness, that is, to avoid writing the increment or decrement on its own line. In this case, the meaning of the expression actually changes depending on whether the operator is placed before or after a variable name. Until you are comfortable using these operators, you should not attempt to use them as part of larger expressions.

When a preincrement is done, the variable is changed before it is used in the expression. Oppositely, when a postincrement is done the variable is changed after the expression is evaluated.

The following example illustrates the difference between pre- and post- increment.

  int number = 3;
  int value = (++number) * 2; 
  // value = 8, number = 4

  int number = 3;
  int value = (number++) * 2; 
  //value = 6, number = 4  

As you can see, whether or not the modified value of number is used as part of the expression in which it occurs depends on which side of the variable the ++, and likewise --, occur.

Precautions

Note that some expressions have unclear meaning, such as number = number++ or number = ++number. Does the variable number receive its incremented value, or is number incremented after the assignment has taken place? It is best to avoid assignment to a varaible when that variable is incremented or decremented as part of the assignment's expression.