• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One more with {} and forloop!!

 
manasa teja
Ranch Hand
Posts: 325
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please observe the following code...
class forloop5 {
public static void main(String args[]) {
for(int x=0;false;x++)
{
System.out.println("hi");
}
}
}
When I compile the above code, it is giving me the error: unreachable statement, ( as expected!! due to false in the forloop...)
But, whereas the below code works fine...
class forloop5 {
public static void main(String args[]) {
for(int x=0;false;x++)
{
//System.out.println("hi");
}
}
}

Whats happening here.. Why just {} is not giving any problem... Do forloops and {} have any deal with each other
Can anybody throw some light on this??
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with the {} at all.
The "unreachable statement" error message should provide a clue. It's telling you that there is a statement that has no chance of being executed. When you comment out the statement, the problem goes away.
 
Bonnie Beach-Morin
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a very close look at the expression in your for loop then read section 14.20 of theJLS about unreachable statements.
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reachability is determined at compile time. In the second case, there is not statement to reach to (atleast not in the for loop) commented statements are not counted and hence you do not get any errors.
 
Paul Villangca
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's odd, I get compile errors in both examples.
BTW, I also saw that problem in Valentin's Mock Exam. It says there that there are errors in these codes:

[ June 16, 2002: Message edited by: Paul Villangca ]
 
Charles Earwood
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forte gives compiler errors for both as well.
However, the following does compile.

[ June 17, 2002: Message edited by: Charles Earwood ]
 
Paul Villangca
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think this is an exception to the unreachable statements rule - it's for debugging purposes.
 
manasa teja
Ranch Hand
Posts: 325
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
I agree with you.
The problem of "unreachable statement" comes only in case of loops. But, do not know the exact reason , why loops only bother about the statement , which is unreachable
Can anybody look into this please??
( With this message, I have become a ranchhand!! A pramotion to me )
[ June 17, 2002: Message edited by: Murthy Ram ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unreachable statements are not only possible with loops but also with other constructs, like return and so on...
None of the following programs compile:



And so on... Unreachable really means that there is no way for a statement to be reached during the execution...
As for the if(false), JLS 14.20 Unreachable Statements clearly states that:

if (false) { x=3; }
does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.
The rationale for this differing treatment is to allow programmers to define "flag variables" such as:
static final boolean DEBUG = false;
and then write code such as:
if (DEBUG) { x=3; }
The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.

[ June 18, 2002: Message edited by: Valentin Crettaz ]
 
manasa teja
Ranch Hand
Posts: 325
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a really nice explanation..
thanks!!
murthy
 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, just to clarify, the code:

gives a compile error because anything in the for block is unreacheable, even though there isn�t anything, right?

Francisco
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiling with v.1.3.1 does not give any errors.
What version are you using that give compile errors?
 
manasa teja
Ranch Hand
Posts: 325
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Francisco A Guimaraes:
So, just to clarify, the code:

gives a compile error because anything in the for block is unreacheable, even though there isn�t anything, right?
Francisco


Francisco,
The above does not gve you any compilation error.
If you remove the comments for
System.out.println("hi");
then, and it will give the compile error as the statement is unreachable.
The moral of the story is {} and
{
// System.out.println("hi");
} are one and the same and will not any give any compilation errors.
 
Paul Villangca
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to disagree with you on this one, Murphy. It doesn't compile under Forte, and it doesn't compile in the command line (1.4) either.
Update: Well, whaddaya know. It does compile under 1.3.1, heh. Now which one should we believe? I stick with my statement, though (compile error.)
[ June 19, 2002: Message edited by: Paul Villangca ]
 
manasa teja
Ranch Hand
Posts: 325
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not verified with hgher version. I am using 1.2 only..
in my previous post, I just answered Francisco's question.
thanks
murthy
 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i used jikes, with the jdk1.4 in the classpath, and 1.4 javac.
 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I just thought of something: if i�m taking the 310-025 exam(the actual SCPJ2), I would have to say the above code would compile and run?
Francisco
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think they are so devious to ask such a question.
This from JLS 14.20 may be helpful

The contained statement is reachable iff the for statement is reachable and the condition expression is not a constant expression whose value is false.

It seems this does unreacheable even no statement. However if different compilers gives different results, it is likely they don't ask such a question.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic