Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop without curly braces

 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Jisha,

May be you should post your complete code to see if mistake is in other lines.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48952
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Moojid! That clears all my pending doubts regarding loops and almost all which I might encounter in future

Thanks again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic