Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unreachable statements? (in program's switch statement)

 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Programmers,
I have been trying to get the following program : 'Flowers' to run.
It is meant to run off a group of subclasses to abstract class Flower.
I have been getting the following output to the console, though:-

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\simon.BLUECHIP-KDOCDA>cd c:\lab_files\Lesson_3

C:\lab_files\Lesson_3>set PATH=c:\Sun\AppServer\jdk\bin;%PATH%


C:\lab_files\Lesson_3>javac Flowers.java
Flowers.java:19: unreachable statement
break;
^
Flowers.java:22: unreachable statement
break;
^
Flowers.java:25: unreachable statement
break;
^
Flowers.java:28: unreachable statement
break;
^
Flowers.java:31: unreachable statement
break;
^
Flowers.java:35: missing return statement
}
^
6 errors

C:\lab_files\Lesson_3>

I take it therefore that the switch statment is amiss, somehow?
If so, then the error must lie with the two 'for' loops, which compiler okays now, but didn't previously.
Hoping you might be able to give me a clue.
Thank you for your attention.
Yours
Simon.
code follows:-







 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your cases all look like



The "break" is unreachable because the statement right before it exits the function. The compiler is trying to tell you that all of your "break"s are superfluous and should be removed.

The last error (missing return statement) occurs because it's at least theoretically possible to hit the end of the function without hitting one of those "return" statements; therefore there has to be a "return null" or something at the end of the function, even if it's never going to be used in practice. A better solution is to add a "default" case to the switch, which throws an IllegalArgumentException or something similar, so that the end of the function is definitely never reached.
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Okay, for this case, it returns an object, and then??? When does the "break" get executed? Heck, can it be executed? What do you think the compiler is saying when it is complaining that this line is not reachable?

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got beaten to the answer by one minute...
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Henry Wong & Ernest Friedman Hill,
I got rid of the break statements as you suggested, see code excerpt below:-

getting:-

or:-

Which I am pleased about, all the same I was wondering - only one of the switch statements is required to be

called at a time - so why aren't the 'break' statements necessary? I thought they were integral to the switch

statement - or else all the other statements would execute- okay so they don't in this case - so why didn't

they ?- sorry to be a pain- but you must've experienced similar such conundrums ie: Why isn't it not working?
Thank you for your help, all the same, hoping to hear,
Yours
Simon.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Simon,

The issue is just that Java won't let you include "dead code" in a method -- i.e., statements that can never be executed are not legal. A "break" immediately after a return can never be executed, so it's not allowed.

In a "normal" switch -- i.e., a switch in which the various cases did not include a return statement -- the break statements would, indeed, be necessary (and, of course, legal.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic