Results 1 to 7 of 7
  1. #1
    Thorton is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Default for loop without {} does not compile (does with {})

    Why does this not compile?
    for (int i = 0; i < 5; i++)
    int j = i;

    But this does:
    for (int i = 0; i < 5; i++) {
    int j = i;
    }

    Never mind that it doesn't really make sense to do either.

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    It indeed doesn't. Interesting question :) I don't know :P
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Because of scope. When you do { }, int j is existence inside the brackets. Without, you're trying to overload a variable name.

    Java Code:
    int j;
    for (int i=0; i<5; i++)
    j = i;
    will work w/out brackets.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    It's not scope.
    It's to do with what you're allowed to have as the single line of an if statement without {}. A declaration is not allowed. It has to be a statement.

    I'd quote the specifics, but I haven't got the JLS to hand.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    I'd quote the specifics, but I haven't got the JLS to hand.
    I have: the syntax of a for-loop statement is basically: "for ( ... ) Statement" and a "Statement" is syntactically explained in paragraph "14.5 Statements"; a declaration is not a "Statement", it is part of a block which in turn is syntactically equivalent to a "Statement".

    kind regards,

    Jos

  6. #6
    Thorton is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Default for loop without {} does not compile (does with {})

    Yes, after some research:

    Variable declarations are not allowed for short statements:
    Blocks and Statements
    Blocks and Statements

    This is apparently to prevent dangling else statements. In a for statement, there wouldn't be a dangling else statement, but I can see why the same check is performed for short if statements and short for/while statements.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    When you think of it if a declaration would've been a statement if strongly complicates the scoping rules, e.g.

    Java Code:
    for (int i= 0; i < n; i++)
       int x;
    x= 42; // does x exist?
    The body of the for-loop is never executed if n < 0. Should it be determinable at runtime? That is not how lexical scoping works, so Gosling wisely decided that a declaration cannot be a statement.

    kind regards,

    Jos
    Last edited by JosAH; 01-10-2010 at 12:09 PM.

Similar Threads

  1. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  2. Code will not compile
    By ShotGunRockets in forum New To Java
    Replies: 17
    Last Post: 05-10-2009, 03:31 AM
  3. compile problem
    By chrisbremen in forum New To Java
    Replies: 8
    Last Post: 11-13-2008, 06:20 PM
  4. doesn't compile?!
    By jon80 in forum New To Java
    Replies: 8
    Last Post: 06-14-2008, 05:42 PM
  5. Not able to compile
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-09-2008, 10:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •