| Author |
strange behavior of for loop ??
|
manasa teja
Ranch Hand
Joined: May 27, 2002
Posts: 325
|
|
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 ]
|
MT
|
 |
Junilu Lacar
Bartender
Joined: Feb 26, 2001
Posts: 4118
|
|
|
"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.
|
 |
 |
|
|
subject: strange behavior of for loop ??
|
|
|