Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

'break' statements to jump among switch cases: how to?

Thread view: 
z-man - 05 Oct 2006 14:20 GMT
Hi all

Is there a legal way to use break statements to jump from one switch
case to another (see below)?

Thanks!

switch(mode)
{
case Partial:
 if(reader == null)
  break Full;

 ...
 break;
case Full:
 ...
 break;
}
Chris Brat - 05 Oct 2006 14:31 GMT
No.

The break lable; jump is restricted to loops AFAIK.
z-man - 05 Oct 2006 15:18 GMT
> No.
>
> The break lable; jump is restricted to loops AFAIK.

If it works this way, I think that the Java switch statement flow is
unconsistent, because of the fall-through behavior (that is nothing but
a degenerate, implicit jump to a subsequent case statement!).

IMHO, fall-through is too much limitative: adding
break-to-labeled-statement statements to the switch syntax would be a
useful and elegant extension to the current simple break statement (the
same purpose of the ugly-but-effective 'go to' statement of C#). To be
used wisely and sparingly, obviously!
Chris Brat - 05 Oct 2006 15:27 GMT
> If it works this way, I think that the Java switch statement flow is
> unconsistent, because of the fall-through behavior (that is nothing but
> a degenerate, implicit jump to a subsequent case statement!).

No it is consistent -  You are guaranteed that a switch statement will
execute from top-to-bottom and fallthrough will occur untill the switch
ends (either by reaching the end of the switch or until a break
statement is executed).

> IMHO, fall-through is too much limitative: adding
> break-to-labeled-statement statements to the switch syntax would be a
> useful and elegant extension to the current simple break statement (the
> same purpose of the ugly-but-effective 'go to' statement of C#).

This is very much like the 'goto' which IMHO is an ingredient for
spagetti code

> To be used wisely and sparingly, obviously!
famous last words
Tor Iver Wilhelmsen - 05 Oct 2006 16:27 GMT
> This is very much like the 'goto' which IMHO is an ingredient for
> spagetti code

And hence why C# has it :)

(There you need to end every case block with either a break or a "goto
case label", there is no fallthrough.)
Thomas Fritsch - 05 Oct 2006 15:01 GMT
> Is there a legal way to use break statements to jump from one switch
> case to another (see below)?
[quoted text clipped - 13 lines]
>   break;
> }

The only way I know is: run from one case into the next case by *not*
doing a "break". The code below does what you wanted in your code above.

switch(mode)
{
 case Partial:
  if(reader != null)
  {
    ...
    break;
  }
  /*FALL THROUGH*/
 case Full:
  ...
  break;
}

But you should do such things only when absolutely needed. At least put
a fat comment there, stating that you omitted the "break" on purpose and
not by accident.

Signature

Thomas

z-man - 05 Oct 2006 15:21 GMT
>> Is there a legal way to use break statements to jump from one switch
>> case to another (see below)?
[quoted text clipped - 34 lines]
> a fat comment there, stating that you omitted the "break" on purpose and
> not by accident.

Thanks, Thomas.

I wrote the following post to Chris just before reading your comment:

"If it works this way, I think that the Java switch statement flow is
unconsistent, because of the fall-through behavior (that is nothing but
a degenerate, implicit jump to a subsequent case statement!).

IMHO, fall-through is too much limitative: adding
break-to-labeled-statement statements to the switch syntax would be a
useful and elegant extension to the current simple break statement (the
same purpose of the ugly-but-effective 'go to' statement of C#). To be
used wisely and sparingly, obviously!"
Patricia Shanahan - 05 Oct 2006 16:50 GMT
> Hi all
>
[quoted text clipped - 15 lines]
>   break;
> }

Not as arbitrary jumps among cases. See
http://www.acm.org/classics/oct95/ for the main arguments against doing it.

Possible solutions include running the switch multiple times, changing
mode as needed, or putting the common work in a method, called in each
situation in which it is needed.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.