for loop and index variable declaration

N

Neroku

Hello, consider the following code:

public static int sum(int ... num)
{
int sum = 0;
for(int n : num)
sum += n;
return sum;
}

The method works fine (It sums all the numbers of the list), but now If
I declare the n variable outside the for loop as shown below:

public static int sum(int ... num)
{
int sum = 0;
int n;
for(n : num)
sum += n;
return sum;
}

I know the difference between the two declarations: in the first one
the n variable only exits inside the for loop, and in the second
declaration the variable exits in the entire method.

But the second declaration yields a compiler error ("not a statement"),
I don't understand why.

TIA
 
F

Flo 'Irian' Schaetz

Neroku said:
But the second declaration yields a compiler error ("not a statement"),
I don't understand why.

If I don't miss something, the enhanced for statement is defined - in the
Language Specification - as...

EnhancedForStatement:
for ( VariableModifiersopt Type Identifier: Expression) Statement

(See [1])

So i = 0 is not valid... But I could be wrong here.

Flo

[1]
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14
 
F

Flo 'Irian' Schaetz

Flo 'Irian' Schaetz said:
So i = 0 is not valid... But I could be wrong here.

Ups, my error: Should of course be...

So (i : n) is not valid...

Flo
 
M

Mike Schilling

Neroku said:
Hello, consider the following code:

public static int sum(int ... num)
{
int sum = 0;
for(int n : num)
sum += n;
return sum;
}

The method works fine (It sums all the numbers of the list), but now
If I declare the n variable outside the for loop as shown below:

public static int sum(int ... num)
{
int sum = 0;
int n;
for(n : num)
sum += n;
return sum;
}

I know the difference between the two declarations: in the first one
the n variable only exits inside the for loop, and in the second
declaration the variable exits in the entire method.

But the second declaration yields a compiler error ("not a
statement"), I don't understand why.


You can't declare the index variable in the new sort of for loop outside the
for statement.
 
P

Patricia Shanahan

Neroku wrote:
....
public static int sum(int ... num)
{
int sum = 0;
int n;
for(n : num)
sum += n;
return sum;
}

I know the difference between the two declarations: in the first one
the n variable only exits inside the for loop, and in the second
declaration the variable exits in the entire method.

But the second declaration yields a compiler error ("not a statement"),
I don't understand why.

The actual problem is the missing type in the for. It needs to be

for(int n : num)

Note the the scope of the variable n in this type of for statement is
the repeated statement, and it is a distinct variable from any other
declarations of n.

The secondary problem is understanding why you got the rather
uninformative message you got for it.

The compiler seems to go on trying for a valid program as long as it
possibly can. After processing "for(n" there is one way, and only one
way, the program could be valid. It would have to be the start of an
old-style for like "for(n=3;". The compiler is trying to parse a
statement, and complains about not finding one when it processes the ":".

Patricia
 
C

Chris Smith

You can't declare the index variable in the new sort of for loop outside the
for statement.

The remaining question one might still have now, not addressed in other
posts, is "why would that be prohibited?" I'm not a mind-reader, but my
guess is that it's to avoid exposing too many unnecessary details that
programmers may have to learn. The new for loops operate at a more
logical level ("for each element"), while the old ones are more about
specifying specific statements that are run at specific times. There
might be several equivalent ways that the new loop could be rewritten in
terms of sequences of specific statements, and different ways of writing
the loop may result in different things being left over in n when it's
done. Java doesn't want to specify a specific choice of details and
have programmers rely on it, because it would make the new language
construct more complex. Java also doesn't like leaving behavior
undefined (it hurts portability, and sometimes security as well, both of
which are specific goals of Java). So the remaining option is to hide
the results of these irrelevant choices by letting the variable go out
of scope.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top