for loop and index variable declaration

Discussion in 'Java' started by Neroku, Dec 2, 2006.

  1. Neroku

    Neroku Guest

    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.

    Neroku, Dec 2, 2006
    1. Advertisements

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

    for ( VariableModifiersopt Type Identifier: Expression) Statement

    (See [1])

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


    Flo 'Irian' Schaetz, Dec 2, 2006
    1. Advertisements

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

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

    Flo 'Irian' Schaetz, Dec 2, 2006

  4. You can't declare the index variable in the new sort of for loop outside the
    for statement.
    Mike Schilling, Dec 3, 2006
  5. Neroku wrote:
    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 Shanahan, Dec 3, 2006
  6. Neroku

    Chris Smith Guest

    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.
    Chris Smith, Dec 3, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.