• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

statement not reachable??

 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

can anyone explain me the answer??

sri
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this web page from sun & scroll down to 14.21 Unreachable Statements.
[ October 13, 2005: Message edited by: Srinivasa Raghavan ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

You are right..




I copied it wrongly...

It does give compilation error....

 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic