wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "unreachable statement" Watch "unreachable statement" New topic
Author

unreachable statement

Danelle Chamberlain
Greenhorn

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
Sheriff

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
Greenhorn

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

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.");
return;
} else {
System.out.println("Greater than or equal to 100.");
return;
}
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: unreachable statement
 
Similar Threads
Accepting Numeric User Input
HELP
payroll
Alright, my eyes hurt from staring at this monitor trying to figure this out.
DUE AT MIDNIGHT TONIGHT..1 ERROR..HELP