aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes statement not reachable?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "statement not reachable??" Watch "statement not reachable??" New topic
Author

statement not reachable??

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252


answer is a..statement is not reachable..i didnt get it ..

can anyone explain me the answer??

sri


Thanks & Regards<br /> <br />-Srikanth
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Have a look at this web page from sun & scroll down to 14.21 Unreachable Statements.
[ October 13, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,
During compilation .....the compiler finds that the while loops

are all having false condition..making the statements...inside them as

unreachable..
The compiler will flag error near lines 5,9,18..

Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by A Kumar:
Hi,
During compilation .....the compiler finds that the while loops

are all having false condition..making the statements...inside them as

unreachable..
The compiler will flag error near lines 5,9,18..




Kumar I dont think it would give any error on line 18.
also I think that the Error by compiler is generated on line 6 and 10 instead of 5 and 9. statements 5 and 9 are surely reachable, but 6 and 10 are unreachable.
How come the option provided are 5 and 9?
Sri have you modified the code ..like for eg put the brace in next line
like instead of
[CODE]
while(false){
}

you put
while(false)
{

}
[CODE]


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979


I think that the Error by compiler is generated on line 6 and 10 instead of 5 and 9. statements 5 and 9 are surely reachable, but 6 and 10 are unreachable.




Yes you are right..about that ... 6 and 10 are the lines unreachable

But 18 is also unreachable..since ; is there after while(false)

You can put ; in the next line and you will get unreachable in the next line..

18 while(false)
19 ;

Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Kumar,
Try compiling the code after commenting lines from 5 to 11
and see what is the result, I am sure the compiler wont agree with you
Liang Tong
Greenhorn

Joined: Oct 14, 2005
Posts: 2
I think it will be like that.
We can divide the code in main method into 5 parts.
part1: It's a individual "while" loop
5 while(false)
6 {
7 System.out.println("Hello");
8 }

part2: It's a individual "while" loop too
9 while(false)
10 {
11 }

part3: It's a "do" statement,here will do nothing,but can compile
12 do;

part4: It's a "while()" statement which has no loop body,because there's a semicolon follows it directly,just like "if(...);",it can compile
13 while(false);

part5: It's a typical "do...while" loop.In the loop body,does nothing
14 do
15 {
16 ;
17 }
18 while(false);

part 3--5 will compile correctly
part 1 && 2 will both cause a "statement is not reachable " Exception.
beacuase "while(true)" will be accepted by compiler,it will create a infinite loop.However "while(false)" means the loop never execute in the code.

Your question is "Which will be the first line to cause an error in the following code? "

So the correct answer is (a)
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Kumar,
Try compiling the code after commenting lines from 5 to 11
and see what is the result, I am sure the compiler wont agree with you


I tried and am getting compilation error at 18...

Am using j2sdk1.4
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Kumar according to you Line 18 unreachable:
14 do
15 {
16 ;
17 }
18 while(false);

But I think its a Do loop...so the compiler will atleast once enter the loop to execute an empty statement ( and then it will check the condition in the while loop, as it will find it false it will not re enter the loop and exit the method.

Correct me if I am wrong
By the way I run the code in JDK 1.5 and it compiled fine.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi..

You are right..




I copied it wrongly...

It does give compilation error....

Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by A Kumar:
Hi..

You are right..




I copied it wrongly...

It does give compilation error....




I think you want to say it DOES'NT give compilation error..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: statement not reachable??