Gillius's Programming Forums

Hosted Forum: West Visayas State University - Philippines => Object Oriented Programming with Java Class Forum => Topic started by: louiecerv on January 23, 2006, 02:20:25 AM

Title: Use a For-Loop to step through an Array
Post by: louiecerv on January 23, 2006, 02:20:25 AM
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: [Select]

for (int index=0; index<temp.length; index++)
{
    temp[index] = keyboard.nextDouble();
    sum += temp[index];
}
Title: while loop
Post by: mamas_pizza on March 02, 2006, 09:07:54 AM
Also this expression is equivalent to while loop

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

int index=0;
while (index <temp.length) {
    temp[index] = keyboard.nextDouble();
    sum += temp[index];
}
Title: Opps
Post by: louiecerv on March 02, 2006, 03:58:04 PM
Hey Mark, you forgot to increment index inside the while loop.