aspose file tools*
The moose likes Beginning Java and the fly likes for loop without curly braces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop without curly braces" Watch "for loop without curly braces" New topic
Author

for loop without curly braces

Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Hi All,

This is a very silly doubt; but I could not find out what the trick is:

The following code compiles:



But if the curly braces are removed it does not compile:



I know it's the declaration of 'k' within the loop which causes the issue; but why I could not figure out

Somebody please help me clear this stupid doubt!

SCJP, SCWCD, SCBCD
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



Jisha,

May be you should post your complete code to see if mistake is in other lines.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Balu Sadhasivam is right about posting more code, but I notice you have a declaration inside a single-line loop, so k is out of scope everywhere and can never be used again.

A class expected error message is either caused by not spelling or declaring class correctly at the beginning of the class, or by unmatched {} later on. If it says line 16 you probably have too many } and too few {.
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Hi All,

I did not post the whole code because there is nothing much in the code
Anyways the following is the complete code which gave me compilation error:


If I put curly braces for the 'for' loop the compilation is a success:


I tried to put the code into a method in the class; but again the same errors

Dear Campbell,
I initially had 2 nested classes in my code; hence the line no was 16; I removed them all; but the error persists
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31076
    
233

Jisha,
The removal of curly braces is only allowed if a single statement follows. Like "System.out.println("hello");. While your statement is a single line, I'm thinking it's not really a single statement.

My guess as to what the compiler thinks is going on:


because it is equivalent to:


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Dear Jeanne,

Thanks a lot! That clears my doubt.
If the code is split as you said it gives the same 2 errors Thanks again
So any line of code which the compiler breaks into multiple lines is prone to this error; right?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The technical explanation is that the body of a for loop is a single statement. A "variable declaration" is not a "statement," but a block of code with braces around it is, indeed, a "compound statement."


[Jess in Action][AskingGoodQuestions]
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Jeanne Boyarsky wrote:Jisha,
The removal of curly braces is only allowed if a single statement follows. Like "System.out.println("hello");. While your statement is a single line, I'm thinking it's not really a single statement.

My guess as to what the compiler thinks is going on:


because it is equivalent to:


Jeanne, IMO, that is not a correct explanation. If it were merely multiple statements, the first statement would be part of the “for statement” and it would compile fine. Here is the BNF for "for statement":



And "statement" is defined as :



While the "block" is defined as :



As you can see "LocalVariableDeclarationStatement" is not considered a valid statement unless it is in a block, it is not a statement by itself. A “for statement” expects a “statement” after the closing parenthesis.

To prove this point following code, although much simpler, generates exactly same error:

Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

This was a learning experience ... I guess that's why mt teacher always insisted on putting curly brackets for every if ... even if it had just one statement!!!


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Thanks a lot Moojid! That clears all my pending doubts regarding loops and almost all which I might encounter in future

Thanks again!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: for loop without curly braces