7

I hope this isn't a stupid question but I have looked up every example I can find and it still seems like I have this code right and it still isn't working... I enter one number and it moves on to the next line of code instead of looping. I'm using this to fill an array with user input numbers. I appreciate any help, thanks.

for(i=0; i<9; i++);
{  
    System.out.println ("Please enter a number:");  
    Num[i] = keyboard.nextDouble();  
    Sum += Num[i];      
    Product *= Num[i];      
}   
3
  • 1
    This could should not even compile. The i is not declared. Unless you have i declared globally somewhere at the top. Commented Jan 1, 2011 at 12:38
  • 1
    By that rational, neither is Num, keyboard, Sum, or Product. Commented Jan 1, 2011 at 16:05
  • You should probably select an answer to your question. Commented Jan 10, 2011 at 0:44

8 Answers 8

17

The ; at the end of the for loop is taken as an empty statement, the equivalent of an empty block for your for-loop. The compiler is reading your code as:

int i;
....
for(i=0; i<9; i++)
    /* no-op */;

/* inline block with no relation to for-loop */
{  
    System.out.println ("Please enter a number:");  
    Num[i] = keyboard.nextDouble();  
    Sum += Num[i];      
    Product *= Num[i];      
} 

Remove the ; to get your intended behavior.


If you don't need the i outside of the loop, you could move its declaration within the for statement.

for(int i=0; i<9; i++)
{
   // `i` is only usable here now
}
// `i` is now out of scope and not usable

Using this syntax when the erroneous semicolon ; was present would have produced a compile error that would alerted you to the erroneous ; earlier. THe compiler would see this:

for(int i=0; i<9; i++)
    /* no-op */;

/* inline block with no relation to for-loop */
{  
    System.out.println ("Please enter a number:");  
    Num[i] = keyboard.nextDouble();     // compile error now - `i` is out-of-scope
    Sum += Num[i];      
    Product *= Num[i];      
} 

This would be an example why it is good practice to limit the scope of variables when possible.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow I knew it was going to be something stupid... arrrrgh. Thanks so much though!! It's now working as intended :)
7

Noticed the ';' at the end of for(i=0; i<9; i++); ? ^_^

Comments

4

remove the last character semicolon from for loop line ............

Comments

4

To avoid this mistake in future you should always use a fresh variable in a for loop. Instead of:

for (i = 0; ...

write

for (int i = 0; ...

That way it would be a compile-time error, since the variable i would not be in scope in the following block.

3 Comments

Although I would recommend your tactic, I can't see how this would have avoid this mistake of Steve
in the code for (int i = 0; i < 5; i++); { i++; } the identifier i goes out of scope after the semicolon and before the block.
It would have reduced the probability of the mistake, since i would be scoped to the for loop. Now Num[i] in the block below it works since i obviously has been defined before it, but otherwise a compile error would follow and the programmer would more easily realize the compound statement (the block) and the for loop are separated.
3

There shouldn't be a semicolon at the end of the first line. It indicates your loop is empty.

Comments

0

The answer has already been given, but I'd like to add that if you are using an IDE*, there will probably be a warning for those kinds of empty statements and other easy to make, easy to overlook type of mistakes (assinging instead of comparing in conditions for example).

Comments

0

just to let you know. something like this:

for(;;)
  ;

should send your program into busy-waiting. happened to me in the early days. :)

Comments

0

Just remove that semi-colon after the "for(....)"

for(i=0; i<9; i++); //<---------- over here

you don't have to put a semi-colon over here. Most probably you were aware of that, it happens. Additional points: The syntax for "for" is:

for(initializer; condition; change){
//code here
}

The syntax for "while" is:

while(condition without semi-colon){
//code here
}

The syntax for "do-while" is:

do{
//code here
}while(condition);//<---------------semi-colon here.

Most probably you were confused between "for" and "do-while" loops, that happens. I too, when I was a beginner, used to be confused of such things.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.