wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "strange behavior of for loop ??" Watch "strange behavior of for loop ??" New topic
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: 4442
    
    5

"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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: strange behavior of for loop ??
 
Similar Threads
Array Indexing
Continue Label Question
for loop question
Getting cannot find symbol error
loop