Hosted Forum: West Visayas State University - Philippines > Object Oriented Programming with Java Class Forum

Use a For-Loop to step through an Array

(1/1)

louiecerv:
The For-Loop is the perfect mechanism for stepping through the elements of an array.  For example, the following for loop illustrates how you can step through an array:


--- Code: ---
for (int index=0; index<temp.length; index++)
{
    temp[index] = keyboard.nextDouble();
    sum += temp[index];
}

--- End code ---

mamas_pizza:
Also this expression is equivalent to while loop

***********************************

int index=0;
while (index <temp.length) {
    temp[index] = keyboard.nextDouble();
    sum += temp[index];
}

louiecerv:
Hey Mark, you forgot to increment index inside the while loop.

Navigation

[0] Message Index

Go to full version