Difference between revisions of "Increment and Decrement Operators"
(updated w/ new content) |
(headerization) |
||
Line 1: | Line 1: | ||
− | This section will | + | {{Template:1010Topic|Chapter_TOC=[[Your First Java Program]]|Introduction=Increment and decrement are concise arithmetic operators that quickly add or subtract one from an integer variable type, such as int and long.|Overview=This section will teach you about the increment and decrement operators, how to use them, the distinction between pre/post-increment and pre/post-decrement, and about special cases to watch out for.}} |
− | == | + | == What Increment and Decrement Operators Are == |
− | + | The increment and decrement operators increase or decrease the value of a [[Your_First_Java_Program#Introduction_3]] by 1. These two operators are <code>++</code> and <code>--</code>, respectively. One would use an increment or decrement operator to concisely add or substract 1 from a variable. | |
− | == | + | == Usage == |
− | + | There are two ways to use these operators, either on their own line along with a variable, or as part of a larger expression. Using them as part of a larger expression is more difficult because the operators may change the value before or after the expression is evaluated, depending on their placement. | |
− | + | == Basic Usage == | |
− | + | ||
− | + | ||
− | + | ||
− | + | We can use these operators by simply placing them before or after the variable to modify. If the operator is placed before a variable, it is known as a preincrement or predecrement, and if the operator is placed after a variable, it is known as a postincrement or postdecrement. | |
− | + | The following code snippet demonstrates the difference between these four operators: | |
− | |||
− | |||
− | |||
− | |||
− | |||
<pre> | <pre> | ||
− | + | int number = 5; | |
− | + | ||
+ | ++number; // preincrement, the variable 'number' would be 6 as a result | ||
+ | number++; // postincrement, the variable 'number' would be 6 as a result | ||
+ | --number; // predecrement, number = 4 | ||
+ | number--; // postdecrement, number = 4 | ||
</pre> | </pre> | ||
− | + | These increment and decrement expressions are really shorthand for: | |
− | == | + | <pre> |
+ | number = number + 1; // longer way of writing number++ or ++number | ||
+ | number = number - 1; // longer way of writing number-- or --number | ||
+ | </pre> | ||
− | + | Note that the regular [[Your_First_Java_Program#Rules_for_Evaluating_Expressions|rules for arithmetic]] in Java apply here, so be careful not to unintentionally increment or decrement outside the value of a variable to outside of its range. | |
− | + | == 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. | |
− | + | <pre> | |
− | + | int number = 3; | |
− | + | int value = (++number) * 2; | |
− | + | // value = 8, number = 4 | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | int number = 3; | |
+ | int value = (number++) * 2; | ||
+ | //value = 6, number = 4 | ||
+ | </pre> | ||
− | ''' | + | 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 <code>++</code>, and likewise <code>--</code>, occur. |
− | + | == Precautions == | |
− | + | Note that some expressions have unclear meaning, such as <code>number = number++</code> or <code>number = ++number</code>. 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. |
Revision as of 15:33, 20 March 2007
COMP 1010 Home > Your First Java Program
IntroductionIncrement and decrement are concise arithmetic operators that quickly add or subtract one from an integer variable type, such as int and long.
|
---|
{{{Body}}}
What Increment and Decrement Operators Are
The increment and decrement operators increase or decrease the value of a Your_First_Java_Program#Introduction_3 by 1. These two operators are ++
and --
, respectively. One would use an increment or decrement operator to concisely add or substract 1 from a variable.
Usage
There are two ways to use these operators, either on their own line along with a variable, or as part of a larger expression. Using them as part of a larger expression is more difficult because the operators may change the value before or after the expression is evaluated, depending on their placement.
Basic Usage
We can use these operators by simply placing them before or after the variable to modify. If the operator is placed before a variable, it is known as a preincrement or predecrement, and if the operator is placed after a variable, it is known as a postincrement or postdecrement.
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 6 as a result --number; // predecrement, number = 4 number--; // postdecrement, number = 4
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 outside the value of a variable to outside of its range.
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.