Reputation: 47
I don't get it why i
in fillArray
method ends up being equal to 10
even though the array score is filled only up to index 9
.
As per my understanding i
has to be smaller than 10
,so how can it end up being 10
at the end, it should have incremented.
I tried another loop to test if for loop executes incrementation at the end if condition is true.
In test loop i
ends up being only 10 which makes sense but the two for loop contradict.
public class GoldScores {
public static final int MAX_NUMBER_SCORES = 10;
public static void main(String[] args) {
double[] score = new double[MAX_NUMBER_SCORES];
int numberUsed = 0;
System.out.println("This program reads gold scores and shows");
System.out.println("how much each differs from the average.");
System.out.println("Enter gold scores:");
//numberUsed = fillArray(score);
// showdifference(score,numberUsed);
for(int i=1; i<11; i++){ //Test loop
System.out.println("Count is: " + i);
}
}
private static void showdifference(double[] score, int numberUsed) {
// TODO Auto-generated method stub
}
public static int fillArray(double[] a){
System.out.println("Enter up to " + a.length + " nonnegative numbers.");
System.out.println("Mark the end of the list with a negative number.");
Scanner keyboard = new Scanner(System.in);
double next = keyboard.nextDouble();
int i = 0;
for(i = 0;(next>=0 && i<a.length);i++){ //HELP!!!!
a[i] = next;
next = keyboard.nextDouble();
}
return i;
}
Upvotes: 0
Views: 1447
Reputation: 2492
While others have already explained in detail , to eliminate confusion you could modify your code to something like :
double next = keyboard.nextDouble();
int i = 0;
int current_i = i;
for( i = 0; ( next >= 0 && i < a.length ); i++ )
{
current_i = i;
a[i] = next;
next = keyboard.nextDouble();
}
return current_i;
instead of
double next = keyboard.nextDouble();
int i = 0;
for(i = 0;(next>=0 && i<a.length);i++){ //HELP!!!!
a[i] = next;
next = keyboard.nextDouble();
}
return i;
Upvotes: 0
Reputation: 126
in For loop, increment occurs after testing loop condition, not before. So in last iteration when your condition is checked, I is already equal to 10 and that is exactly what is being returned. Consider this, if your I would still be 9 in last iteration, your condition would still be true which would mean one more execution in loop.
Upvotes: 0
Reputation: 178293
You must understand exactly how the for
loop works to understand what is going on, and why i
is 10
after the for
loop in fillArray
.
false
, break out of the loop.In the last iteration of the i
for
loop, i
is 9
, and index 9
is assigned in the array, step 3. Step 4 performs the increment, and i
is now 10
. Then the condition is tested, which is false
, and the loop is exited. i
is now 10
.
However, in your main
for
loop, you print the value in the body instead of examining the looping variable afterwards. The last iteration is when i
is 10
, because the condition is different: i < 11
. If you were to print i
after that for
loop, you'll see it's 11
.
Upvotes: 6