There is no actual such thing as an accumulator variable but it is a concept or rather a technique for using a normal variable to accumulate a value.
The following statement declares an int variable and assigns it a value of 0:
int num = 0;
if we then write another statement:
num = 10;
...what we have done is overwrite the value of num from 0 to 10.
But often, we want to increase the value of num with another number, rather than overwrite it's value.
In this case, we can use the add and (+=) operator, rather than the assignment operator (=):
int num = 0;
num += 10;
num += 3;
The value of num has accumulated a value of 13.
Consider the following example, which uses a for loop to accumulate a value:
int num = 0;
for (int x = 0 ; x < 10 ; x ++ ) {
num += 10;
}
The loop runs 10 times and on each pass of the loop, num accumulates another value of 10. The end result is that num has a value of 100.
In the next example, we use the Java increment operator (++) to increase the value of num by 1:
int num = 0;
for (int x = 0 ; x < 10 ; x ++ ) {
num ++;
}
You should also note that the loop counter itself makes use of the increment operator to add 1 to itself on each pass of the loop.
What about decrementing?
int num = 10;
num -= 5;
num -= 1;
Using the minus and (-=) operator, num now equals 4
and in a loop we might:
int num =10;
for (int x = 0 ; x < 10 ; x ++ ) {
num --;
}
Using the decrement operator (--), num equals 0.
What about Strings?
Use the add and (+=) operator to append 1string to another:
String name = "Sabrina";
name += "Albright";
Showing posts with label Increment. Show all posts
Showing posts with label Increment. Show all posts
Friday, September 20, 2013
Tuesday, January 8, 2013
Incrementing a number ++
Increment - increase a value.
The increment operator is a double plus ++
The increment operator increases the value to it's left by 1.
Example use with int:
int one = 10;
one ++;
In the first line, an int variable called one is declared and assigned a value of 10.
In the second line, the increment operator is applied to the variable called one to increment it's value.
The result is that the variable called one, now has a value of 11.
Example use with double:
double one = 12.55;
one ++;
In the first line, a double variable called one is declared and assigned a value of 12.55.
In the second line, the increment operator is applied to the variable called one to increment it's value.
The result is that the variable called one, now has a value of 13.55.
Note that where the increment operator is used with the double data type, the increment value is 1 significant number (the number on the left of the floating point is incremented).
See also Decrementing a number
Decrementing a number --
The decrement operator is a double minus --
The decrement operator decreases the value to it's left by 1.
Example use with int:
int one = 10;
one --;
In the first line, an int variable called one is declared and assigned a value of 10.
In the second line, the decrement operator is applied to the variable called one to decrement it's value.
The result is that the variable called one, now has a value of 9.
Example use with double:
double one = 12.55;
one --;
In the first line, a double variable called one is declared and assigned a value of 12.55.
In the second line, the decrement operator is applied to the variable called one to decrement it's value.
The result is that the variable called one, now has a value of 11.55.
Note that where the decrement operator is used with the double data type, the decrement value is 1 significant number (the number on the left of the floating point is decremented).
See also Incrementing a number
The decrement operator decreases the value to it's left by 1.
Example use with int:
int one = 10;
one --;
In the first line, an int variable called one is declared and assigned a value of 10.
In the second line, the decrement operator is applied to the variable called one to decrement it's value.
The result is that the variable called one, now has a value of 9.
Example use with double:
double one = 12.55;
one --;
In the first line, a double variable called one is declared and assigned a value of 12.55.
In the second line, the decrement operator is applied to the variable called one to decrement it's value.
The result is that the variable called one, now has a value of 11.55.
Note that where the decrement operator is used with the double data type, the decrement value is 1 significant number (the number on the left of the floating point is decremented).
See also Incrementing a number
Sunday, April 29, 2012
Arithmetic operators
DOWNLOAD THE CODE: ArithmeticOpsPkg
From http://www.tutorialspoint.com/java/java_basic_operators.htm
Labels:
-,
--,
*,
/,
+,
++,
Addition,
Arithmetic,
cat,
Decrement,
Demo Code Project,
Division,
Increment,
Multiplication,
Operators,
Subtraction
Subscribe to:
Posts (Atom)

