• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strange behavior of for loop ??

 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I wrote the following code and compile..
class forloop4 {
public static void main(String args[]) {
for(int j=0; j<10;j++)
int k=10;
}
}
This gives a compile error: .class expected, not a statement.
But,
{
int k=10;
} works fine!!
Even , ";" after forloop works fine, but why not int k=10; without any braces??
Is this not a strange behavior??

[ June 14, 2002: Message edited by: Murthy Ram ]
 
Junilu Lacar
Bartender
Posts: 7480
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"int k = 10;" is a local variable declaration statement (JLS 14.4) and is not one of the valid statements (JLS 14.5) that can be specified with the for statement (JLS 14.13). The braces make it a block statement, which is acceptable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic