• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unreachable statement

 
Danelle Chamberlain
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your help
 
AJ Keats
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic