This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes One more with {} and forloop!! 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 "One more with {} and forloop!!" Watch "One more with {} and forloop!!" New topic
Author

One more with {} and forloop!!

manasa teja
Ranch Hand

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


MT
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

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

Joined: Jun 13, 2002
Posts: 13
Take a very close look at the expression in your for loop then read section 14.20 of theJLS about unreachable statements.


SJCP2
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
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.


ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
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

Joined: Jun 04, 2002
Posts: 6
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

Joined: Jun 04, 2002
Posts: 133

I think this is an exception to the unreachable statements rule - it's for debugging purposes.
manasa teja
Ranch Hand

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

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
Thats a really nice explanation..
thanks!!
murthy
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
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<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Compiling with v.1.3.1 does not give any errors.
What version are you using that give compile errors?
manasa teja
Ranch Hand

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

Joined: Jun 04, 2002
Posts: 133
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

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

Joined: Mar 20, 2002
Posts: 182
i used jikes, with the jdk1.4 in the classpath, and 1.4 javac.
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182

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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
 
jQuery in Action, 2nd edition
 
subject: One more with {} and forloop!!
 
Similar Threads
StringBuffer class
if and while
Rules Roundup beta error
local variable
integer addition