Increment and Decrement Operators

From CompSciWiki
Revision as of 14:07, 22 July 2009 by WikiSysop (Talk | contribs)

Jump to: navigation, search

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

REVIEW! What will the following code output?

  int marbles= 2;
  System.out.println("You have " + marbles + " marbles.");
  marbles ++;
  System.out.println("Now you have " + marbles + " marbles.");
  marbles --;
  marbles --;
  marbles --;
  System.out.println("Now you have " + marbles + " marbles.");
  marbles --;
  System.out.println("Now you have " + marbles + " marbles.");


REVIEW OUTPUT

  You have 2 marbles.
  Now you have 3 marbles.
  Now you have 0 marbles.
  Now you have -1 marbles.

NOTE: The expression marbles++ is pronounced "marbles plus plus" and marbles-- is pronounced "marbles minus minus". Does it make sense to you that the programming language C++ ("C Plus Plus") is based on the programming language C?

Combined Assignment Operators

Now what if you want to use a shortcut for increasing a variable but you don't want to increase the variable by 1? You can use a Combined Assignment Operator. For example x = x+ y can also be written as x+=y. Or a = a+ 2 can be written as a+=2. The below table shows you common combined assignment operators.

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


Postfix and Prefix Modes

The operators ++ and x-- update the variable at different times depending on whether they are placed 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.

In simple expressions it doesn't make a difference if you write x++ or ++x. Also, in simple expressions x-- is the same as --x.

But in more complex expressions the placement of the operator changes the result of the expression. The following code snippet demonstrates the difference between these four operators:

  int number = 5;
  int y; 

  //the next four simple expressions show how the placement of the operators sometimes works the same way
  ++number; // preincrement, the variable 'number' stores the value 6
  number++; // postincrement, the variable 'number' now stores the value 7
  --number; // predecrement, the variable 'number' stores the value 6
  number--; // postdecrement, the variable 'number' stores the value 5

  //now, watch how the value of y changes depending on the placement of the operators
  y = number++;  //the value of y is equal to the value in x BEFORE the increment takes place. So y = 5 and number = 6;
  y = ++number;  //this time the value of number is increased before the value is assigned to y. So y = 7 and number = 7.



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.


Summary

This section shows ways to speed up your typing by using less characters. You can always write expressions the long way, but you may prefer to use increment, decrement or combined assignment operators to save key strokes. You also need to know these shorthand strategies so you can read and understand expressions in programs that you read.

The increment and decrement operators are used to add or subtract one from a variable. 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, such as in a loop.