Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"unreachable statement" when trying to compile a "while" statement

I'm new to Java and working through some coursework. However, on the following piece of code I'm getting the error "Unreachable statement" when trying to compile. Any pointers as to what I'm doing wrong?

public String getDeliveredList() {
   int count = 0;
   while (count < deliveredList.size()){
       return ("Order # " + count + deliveredList.get(count).getAsString());
       count++;
   }
}
like image 239
Iain Standing Avatar asked Feb 20 '26 02:02

Iain Standing


1 Answers

Once you've returned from the function, logically it can no longer execute anything after that point -- the count++ statement will never be reached.

while (count < deliveredList.size()){

   // function always ends and returns value here
   return ("Order # " + count + deliveredList.get(count).getAsString());

   // this will never get run
   count++;
}
like image 107
McGarnagle Avatar answered Feb 22 '26 16:02

McGarnagle