Difference between revisions of "Increment and Decrement Operators"

From CompSciWiki
Jump to: navigation, search
(Change the codeblock tag to outputblock in Review Output)
 
(19 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Template:1010Topic|Chapter_TOC=[[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.
+
{{Template:1010Topic
|Overview=This section shows you that <code>x=x+1</code> can also be written using the increment operator as <code>x++</code> or using a combined assignment operators as <code>x+=1</code>. The decrement operator uses a similar notation to decrease values by one. Also combined assignment operators can be used when you want to change the value by a value other than 1. These shorthand save time when typing. These operators are especially common when updating counter variables in loops. }}
+
|Chapter_TOC=[[Java Fundamentals]]
 +
|Previous=[[Logical Operators]]
 +
|Next=[[Strings]]
 +
|Body=
 +
 
 +
==Introduction==
 +
 
 +
This section outlines alternative syntax for increasing or decreasing the value of a variable. These methods help by allowing code to be written quicker and can even increase legibility. The increment and decrement operators increase or decrease a variable by 1. The combined assignment operator can be used for values other than 1.
 +
 
 +
See page 179 in the Gaddis text: http://tinyurl.com/gaddis179
 +
 
  
 
== The Increment and Decrement Operators ==
 
== The Increment and Decrement Operators ==
  
The increment and decrement operators increase or decrease the value of a variable by 1. The increment operator is <code>++</code> and decrement is <code>--</code>. 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.
+
Then increment operator increases the value of a variable by 1 (equivalent to x = x+1). The decrement operator decreases the value of an operator by 1 (equivalent to x = x-1). The increment operator is <code>++</code> and decrement is <code>--</code>. These operators can be used on int, long, float, and double data types. They are primarily used on integers such as int and long.  
 +
 
  
 
== Usage ==
 
== Usage ==
  
A shorter way of writing <code>x = x + 1</code> is to use the increment operator to write <code>x++</code>. You can also decrease the value in a variable by using the decrement opertor. For example, a shorter way of writing <code> y = y - 1</code> is to write <code>y--</code>. The below code shows how these shorthand operators work.  
+
The below code shows how these shorthand operators work.  
<pre>
+
{{CodeBlock
 +
|Code=
 
   x = 1;
 
   x = 1;
 
   x++; //is a shorter way of writing x = x + 1
 
   x++; //is a shorter way of writing x = x + 1
 
   x--; //is a shorter way of writing x = x - 1
 
   x--; //is a shorter way of writing x = x - 1
 
   //note: at this point the value of x is 1.  
 
   //note: at this point the value of x is 1.  
 +
}}
  
</pre>
+
These shorthand notations do not change any logic in your programming but they do reduce the number of keystrokes. This shorthand notation is especially useful when using loops with counters.
  
These shorthand notations do not change any logic in your programming but they do reduce the nubmer of keystrokes. Plus since they're often used in programs you'll need to be able to understand them. Also, when we start to use loops you'll find it often very helpful to use this shorter notation.
 
  
 
REVIEW! What will the following code output?
 
REVIEW! What will the following code output?
<pre>
+
{{CodeBlock
 +
|Code=
 
   int marbles= 2;
 
   int marbles= 2;
 
   System.out.println("You have " + marbles + " marbles.");
 
   System.out.println("You have " + marbles + " marbles.");
Line 31: Line 44:
 
   marbles --;
 
   marbles --;
 
   System.out.println("Now you have " + marbles + " marbles.");
 
   System.out.println("Now you have " + marbles + " marbles.");
</pre>
+
}}
 
+
  
 
REVIEW OUTPUT
 
REVIEW OUTPUT
<pre>
+
{{OutputBlock
  You have 2 marbles.
+
|Code=
  Now you have 3 marbles.
+
You have 2 marbles.
  Now you have 0 marbles.
+
Now you have 3 marbles.
  Now you have -1 marbles.
+
Now you have 0 marbles.
</pre>
+
Now you have -1 marbles.
 +
}}
  
 
NOTE: The expression <code>marbles++</code> is pronounced "marbles plus plus" and <code>marbles--</code> 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?
 
NOTE: The expression <code>marbles++</code> is pronounced "marbles plus plus" and <code>marbles--</code> 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 ==  
 
== 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 <code>x = x+ y</code> can also be written as <code>x+=y</code>. Or <code>a = a+ 2</code> can be written as <code>a+=2</code>. The below table shows you common combined assignment operators.  
+
If you want to use a shortcut for increasing a variable by a value other than 1, you can use a combined assignment operator. For example <code>x = x + y</code> can also be written as <code>x += y</code> or <code>a = a + 2</code> can be written as <code>a += 2</code>. The below table shows you common combined assignment operators.  
  
{| border = 1 align="center"
+
<table border=1>
|
+
<tr>
{|
+
<td>Operator
  |-
+
<td>Meaning  
! Operator !! Meaning  
+
</tr>
|-
+
<tr>
| x += y  
+
<td>x += y  
| x = x + y
+
<td>x = x + y
|-
+
<tr>
| x -= y  
+
<td>x -= y  
| x = x - y
+
<td>x = x - y
|-
+
<tr>
| x *= y  
+
<td>x *= y  
| x = x * y
+
<td>x = x * y
|-
+
<tr>
| x /= y  
+
<td>x /= y  
| x = x / y
+
<td>x = x / y
|-
+
<tr>
| x %= y  
+
<td>x %= y  
| x = x % y
+
<td>x = x % y
|}
+
</tr>
|}
+
</table>
  
  
 
==Postfix and Prefix Modes==
 
==Postfix and Prefix Modes==
The operators <code>++</code> and <code>x--</code> 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.
+
The operators <code>++</code> and <code>--</code> 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. Preincrement/predecrement 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. Postincrement/postdecrement means the variable is used by the expression and then modified after.
  
In simple expressions it doesn't make a difference if you write <code>x++</code> or <code>++x</code>. Also, in simple expressions <code>x--</code> is the same as <code>--x</code>.
+
In simple expressions it doesn't make a difference if whether you use postfix or prefix mode. In more complex expressions however, the placement of the operator changes the result of the expression. The following code snippet demonstrates the difference between these four operators:
  
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:
+
{{CodeBlock
 
+
|Code=
<pre>
+
 
   int number = 5;
 
   int number = 5;
 
   int y;  
 
   int y;  
Line 94: Line 107:
  
  
</pre>
+
}}
  
  
Note that the regular [[Your_First_Java_Program#Rules_for_Evaluating_Expressions|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.
+
Be careful not to unintentionally increment or decrement beyond the range a variable can hold. Strange things can happen when this occurs.
 
+
  
  
Line 106: Line 118:
  
 
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.
 
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.
 +
  
 
==Review==
 
==Review==
 
1. What will be printed by the following code?
 
1. What will be printed by the following code?
<pre>
+
{{CodeBlock
 +
|Code=
 
   int x=2;
 
   int x=2;
 
   int y;
 
   int y;
Line 119: Line 133:
 
   System.out.print(y);
 
   System.out.print(y);
 
   System.out.print(x);
 
   System.out.print(x);
</pre>
+
}}
 +
 
  
 
2. Note the code below puts the expressions x++ and x+=2 in the println statement. Does output change?
 
2. Note the code below puts the expressions x++ and x+=2 in the println statement. Does output change?
<pre>
+
{{CodeBlock
 +
|Code=
 
   int x=2;
 
   int x=2;
 
   int y;   
 
   int y;   
Line 130: Line 146:
 
   System.out.print(y);
 
   System.out.print(y);
 
   System.out.print(x);
 
   System.out.print(x);
</pre>
+
}}
 +
 
  
ANSWERS: 1) 3556 2) Yes, the value of x is printed first (as 2) then increased to 3. But the combined operator expression x+=2
+
ANSWERS: 1) 3556 2) Yes, the value of x is printed first (as 2) then increased to 3. But the combined operator expression x+=2 does not change the output when placed in the print statement.
 +
}}

Latest revision as of 15:23, 8 December 2011

COMP 1010 Home > Java Fundamentals


Introduction

This section outlines alternative syntax for increasing or decreasing the value of a variable. These methods help by allowing code to be written quicker and can even increase legibility. The increment and decrement operators increase or decrease a variable by 1. The combined assignment operator can be used for values other than 1.

See page 179 in the Gaddis text: http://tinyurl.com/gaddis179


The Increment and Decrement Operators

Then increment operator increases the value of a variable by 1 (equivalent to x = x+1). The decrement operator decreases the value of an operator by 1 (equivalent to x = x-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 integers such as int and long.


Usage

The below code shows how these shorthand operators work.

 x = 1;
  x++; //is a shorter way of writing x = x + 1
  x--; //is a shorter way of writing x = x - 1
  //note: at this point the value of x is 1. 

These shorthand notations do not change any logic in your programming but they do reduce the number of keystrokes. This shorthand notation is especially useful when using loops with counters.


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

If you want to use a shortcut for increasing a variable by a value other than 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 -- 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. Preincrement/predecrement 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. Postincrement/postdecrement means the variable is used by the expression and then modified after.

In simple expressions it doesn't make a difference if whether you use postfix or prefix mode. In more complex expressions however, 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. 


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.


Review

1. What will be printed by the following code?

 int x=2;
   int y;
   x++;   
   System.out.print(x);
   x+=2;
   System.out.print(x);
   y=x++;
   System.out.print(y);
   System.out.print(x); 


2. Note the code below puts the expressions x++ and x+=2 in the println statement. Does output change?

 int x=2;
   int y;   
   System.out.print(x++);
   System.out.print(x+=2);
   y=x++;
   System.out.print(y);
   System.out.print(x); 


ANSWERS: 1) 3556 2) Yes, the value of x is printed first (as 2) then increased to 3. But the combined operator expression x+=2 does not change the output when placed in the print statement.

Previous Page: Logical Operators Next Page: Strings