File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes while statement error 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 "while statement error" Watch "while statement error" New topic

while statement error

Sachin Kapoor

Joined: Jul 01, 2007
Posts: 19
The question is which line is first to cause an error?

1 class Char
2 {
3 public static void main(String arg[])
4 {
5 while(false)
6 {
7 System.out.println("Hello");
8 }
9 while(false)
10 {
11 }
12 do;
13 while(false);
14 do
15 {
16 ;
17 }
18 while(false);
19 }
20 }

Choices are
1. Line no. 5
2. Line no. 9
3. Line no. 12
4. Line no. 16

Answer is Choice 1 is correct. It will give you error for unreached statement. All other statements are valid.

Does anyone know why exactly does line 5 give an error? If there is some code inside a while(false) loop, does it always give an error?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Yes, all these are unreacheable code, if there is really code in between the brackets does not matter:


all events occur in real time
raghu nagabandi
Ranch Hand

Joined: Aug 14, 2007
Posts: 35
it always give compile time error
subject: while statement error
Similar Threads
statement not reachable??
statement not reached error in while() loop
unreachable statement again
labels, breaks, and continue questions
unreachable statement