The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes strange behavior of for loop ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "strange behavior of for loop ??" Watch "strange behavior of for loop ??" New topic

strange behavior of for loop ??

manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
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.
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

Joined: Feb 26, 2001
Posts: 6529

"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.
I agree. Here's the link:
subject: strange behavior of for loop ??
It's not a secret anymore!