Author Topic: Use a For-Loop to step through an Array  (Read 8394 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Use a For-Loop to step through an Array
« 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];
}
Analyze. Design. Develop. Debug. Deploy. Maintain.

mamas_pizza

  • Guest
while loop
« Reply #1 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];
}

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Opps
« Reply #2 on: March 02, 2006, 03:58:04 PM »
Hey Mark, you forgot to increment index inside the while loop.
Analyze. Design. Develop. Debug. Deploy. Maintain.