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 For Loop. Show all posts
Showing posts with label For Loop. Show all posts
Friday, September 20, 2013
Wednesday, April 17, 2013
Bubble Sort algorithm
The Bubble Sort algorithm is named as such after the way in which values bubble to the top.
It is a suitable sorting algorithm for small unsorted lists and arrays.
You may like to watch the video at the end of this post before reading on (skip to about 50 seconds in).
Let's take the example of a small array of 5 elements:
int numList[] = {100,40,2,1,89};
In it's sorted form, the list of numbers would look like this:
1,2,40,89,100
So how do we get there?
Using a for-loop, we will bubble sort the values by checking if the current value is greater than the next value in the array:
for (int x = 0; x < numList.length - 1; x++) {
if (numList[x] > numList[x+1] )
If true, we need to swap the numbers in the array, but to do so, we will need to store the current value in a temporary variable, so that the next value can be placed into the current value's place in the array - like so:
int temp = numList[x];
numList[x] = numList[x+1];
numList[x+1] = temp;

If false, we do nothing and allow the loop to continue.
Note that the loop is primed so that the loop counter (x) is never greater than the array's length minus one - which in this example would mean that x is never greater than 3. This is because the final swap possible is between indexes 3 & 4 and we don't want to generate an ArrayIndexOutOfBoundsException when we execute the expression numList[x+1].
If the length of the list we need to sort is 5, it will take 4 loops to sort the list, once.
First pass:
100,40,2,1,89 becomes 40,2,1,89,100
We can see that 100 has bubbled to the end of the list, however, the list is still not sorted. If the length of the list is 5, it will take 16 loops (maximum) to ensure that each position is sorted.
We will need to use an outer loop and and inner loop (nested loops) to ensure that all elements are sorted.
And here is the output of each outer loop:
The video is quite cool :)
It is a suitable sorting algorithm for small unsorted lists and arrays.
You may like to watch the video at the end of this post before reading on (skip to about 50 seconds in).
Let's take the example of a small array of 5 elements:
int numList[] = {100,40,2,1,89};
In it's sorted form, the list of numbers would look like this:
1,2,40,89,100
So how do we get there?
Using a for-loop, we will bubble sort the values by checking if the current value is greater than the next value in the array:
for (int x = 0; x < numList.length - 1; x++) {
if (numList[x] > numList[x+1] )
If true, we need to swap the numbers in the array, but to do so, we will need to store the current value in a temporary variable, so that the next value can be placed into the current value's place in the array - like so:
int temp = numList[x];
numList[x] = numList[x+1];
numList[x+1] = temp;
If false, we do nothing and allow the loop to continue.
Note that the loop is primed so that the loop counter (x) is never greater than the array's length minus one - which in this example would mean that x is never greater than 3. This is because the final swap possible is between indexes 3 & 4 and we don't want to generate an ArrayIndexOutOfBoundsException when we execute the expression numList[x+1].
If the length of the list we need to sort is 5, it will take 4 loops to sort the list, once.
First pass:
100,40,2,1,89 becomes 40,2,1,89,100
We can see that 100 has bubbled to the end of the list, however, the list is still not sorted. If the length of the list is 5, it will take 16 loops (maximum) to ensure that each position is sorted.
We will need to use an outer loop and and inner loop (nested loops) to ensure that all elements are sorted.
Note that in this example, it actually only took 3 outer loops (12 loops total) to sort the array. If we used the following array:
5,4,3,2,1
... it would take 16 repetitions to sort the array completely - this is because the largest number in the array (5) is in the first position and therefore has the entire length of the array to be sorted through.
Here's the output of bubble sorting that list:
Tuesday, April 16, 2013
The For loop
Loops allow us to perform a set of instructions repeatedly until a certain condition is reached.
A video game is basically a continuous loop that handles input, handles updates and draws - maybe 60 times per second!
There are a few different kinds of loops. This post is about the For-Loop, however all loops have some things in common:
- A loop needs a start condition - where to start
- A loop needs an end condition - where to end
- A loop contains a body - the statements to execute repeatedly.
If a loop does not know where to end, we get an INFINITE LOOP and that's really no good.
The For-Loop
The for-loop is primed with the following information about the loop:
- The value of the loop counter when the loop starts - initialization
- The value that flags the end of the loop - termination
- How the loop counter should be incremented on each pass of the loop - increment
Format of the syntax:
for (initialization; termination; increment) {
statement(s)
}
Note:
- The increment condition may also be a decrement condition.
- The loop counter can begin at any value - for instance 0 or 1000 (it depends on the task).
The body of the loop is where we place the instructions which need to be repeated.
In the following example, the numbers 0 to 9 are printed to screen. The loop has been primed to start the counter variable at 0. The loop will continue while the counter variable is less than 10. The counter variable will be incremented on each pass of the loop.
"The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/ZtqqHM )
Subscribe to:
Posts (Atom)