Java Reference
In-Depth Information
+sales2014[4]+sales2014[5]+sales2014[6]+sales2014[7]+sales2014[8]+sales2014[9]
+sales2014[10]+sales2014[11];
You can immediately spot the redundancy in this code. To find a better solution, simply describe
what it is you would like to do. For every month of the year, divide the sales by the staff and sum
the sales. In order to implement this in Java, you can use a for loop. A for loop executes a block
of code over a range of values. An index variable keeps track of the loop. The standard syntax for a
for loop is as follows:
for (/*Initialization*/; /*Termination*/; /*Increment*/){
/*execute these statements*/
}
Rather than just a condition as you saw in the if-then statement, for loops require three parts.
Initialization declares the index variable for the loop and its starting value; commonly this is int i = 0 .
Termination specifies a stopping criterion, or maximum value for the index variable. Increment indicates
how the index variable should change after each iteration, commonly this is i ++, meaning that the value
of i will increase by one after each loop. Alternatively, you can use i - for the increment and a minimum
value in the termination; in this way your looping will count down, rather than up. Finally, all the state-
ments that should be executed during each loop are placed between the curly brackets.
You can implement the previous example in the following for loop:
int[] sales2014 = {500,720,515,377,400,435,510,1010,894,765,992,1125};
int[] staff2014 = {7,5,5,5,5,6,6,7,7,8,9,9};
int[] salesPerStaff = new int[12];
int totalSales2014 = 0;
for (int i=0; i<sales2014.length; i++){
salesPerStaff[i] = sales2014[i]/staff2014[i];
totalSales2014 = totalSales2014 + sales2014[i];
}
This for loop starts with an index value of 0 and evaluates salesPerStaff at month 0 and total-
Sales2014 at month 0. At the end of this iteration of the loop, the index increments to 1 , and those
same variables are evaluated for month 1 and so on, until month 11. The value of sales2014 .
length is the number of elements in the sales2014 array, which is 12. So when the index incre-
ments to 12 , it will evaluate i<sales2014 .length as false , and the looping will terminate.
There are several benefits to this improved loop implementation. You might notice it is easier to read
and less prone to typing errors than the longer and more tedious version. Recall that loops improve
maintainability and flexibility. Now that you have an example, it might be easier to visualize the
impact of these factors. Easier maintenance means that future changes to the application are easier
to implement. If you needed to change how SalesPerStaff is calculated, you would have to make
12 changes in the first version versus only one in the for loop. The flexibility of loops is based
on the self-determined termination criteria. In the example, the loop iterates 12 times, because
the array has 12 elements. Now imagine your company begins recording weekly sales instead of
monthly sales, increasing the elements of the sales array from 12 to 52 per year. In the first version
of the program, the number of statements would increase by the same amount, also increasing the
chance of errors. However, the for loop in the second version would accommodate arrays of any
length without need for manual modification.
Search WWH ::




Custom Search