This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes unreachable statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "unreachable statement" Watch "unreachable statement" New topic

unreachable statement

Danelle Chamberlain

Joined: Sep 26, 2003
Posts: 2
I am trying to work on a do while statement but I'm getting an unreachable statement error when running the compiler. where did I go wrong with my code?
Jason Menard

Joined: Nov 09, 2000
Posts: 6450
The way your class is written, these lines following your break statement are unreachable:

Also, what the break statement is doing is breaking you out of your do-while loop, which in effect means that the code inside the loop will only ever execute once. I suspect you want the break statement after your do-while loop.
[ September 26, 2003: Message edited by: Jason Menard ]
Danelle Chamberlain

Joined: Sep 26, 2003
Posts: 2
thank you for your help
AJ Keats

Joined: Sep 27, 2003
Posts: 2
As a point of interest, unreachable code is always an error in Java. Unreachable code means that there is no path of execution that will cause the code to execute. This can occur a number of different ways, including a misplaced break statement, as is the case with your code. Here is another example:
class Opps {
public static void main(String args[]) {
int i = 99;

if(i < 100) {
System.out.println("Less than 100.");
} else {
System.out.println("Greater than or equal to 100.");
System.out.println("This is unreachable!");
In this program, both paths of execution through the if/else end with a return. Thus, the last call to println() is unreachable. That is, there is no path of execution that can lead to the final println().
Keep in mind that javac can't find all unreachable code. It can only find code that is unreachable because there is no path of execution that can possibly lead to the unreachable code. Code that is unreachable because of logic errors in your program won't be found. For example, assume that your program has a switch statement that contains a case value that will never occur during the execution of the program. This case statement is NOT flagged as unreachable because the compiler has no way of knowing that this case value will never occur at runtime.
I agree. Here's the link:
subject: unreachable statement
jQuery in Action, 3rd edition