Difference between revisions of "Increment and Decrement Operators"

From CompSciWiki
Jump to: navigation, search
Line 19: Line 19:
 
   x += 1;    //x now stores the value 3
 
   x += 1;    //x now stores the value 3
 
   x++;      //x now stores the value 4
 
   x++;      //x now stores the value 4
 +
  //the above lines show three different ways of increasing the value of x by 1
 
</pre>
 
</pre>
  
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.
+
Want to decrease the value of a variable? You can use a similar approach using the decrement operator. The below code shows three different ways of decrementing the value of x by 1.
 +
<pre>
 +
  x = 5;
 +
  x = x - 1; //x now stores the value 4
 +
  x -= 1;    //x now stores the value 3
 +
  x--;      //x now stores the value 2
 +
  //the above lines show three different ways of decreasing the value of x by 1
 +
</pre>
 +
 
 +
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:
 
The following code snippet demonstrates the difference between these four operators:

Revision as of 14:39, 21 July 2009

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

How many ways can you increase the value of a variable by 1? Obviously you can write

  x = 1;
  x = x + 1; //x now stores the value 2

A shorter way of writing x = x+1 is writing x+=1. An even shorter way is to use the increment operator and write x++. As review, the below code shows uses three different approaches to incrementing the value of x by 1:

  x = 1;
  x = x + 1; //x now stores the value 2
  x += 1;    //x now stores the value 3
  x++;       //x now stores the value 4
  //the above lines show three different ways of increasing the value of x by 1

Want to decrease the value of a variable? You can use a similar approach using the decrement operator. The below code shows three different ways of decrementing the value of x by 1.

  x = 5;
  x = x - 1; //x now stores the value 4
  x -= 1;    //x now stores the value 3
  x--;       //x now stores the value 2
  //the above lines show three different ways of decreasing the value of x by 1

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. Be careful not to unintentionally increment or decrement beyond the range a variable can hold. Strange things can happen when this occurs.

Combined Assignment Operators

Often, you will not want to increase or decrease a variable by one, but by a different value which may be a constant (like 2) or stored in a variable. Fortunately, there are combined assignment operators which are shorthand for these types of statements:

Operator Meaning
x += y x = x + y
x -= y x = x - y
x *= y x = x * y
x /= y x = x / y
x %= y x = x % y

Summary

The increment and decrement operators are used to add or subtract one from a variable. These operators can be used on the basic data types of int, long, float, and double. The operators can be used as part of a complex expression or as a simple expression. The most common use for the increment operator is as a counter. The increment and decrement operators easily add or subtract one to a variable.