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

Z

z-man

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;
}
 
T

Thomas Fritsch

z-man said:
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;
}

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.
 
Z

z-man

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!
 
Z

z-man

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.

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!"
 
C

Chris Brat

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
 
P

Patricia Shanahan

z-man said:
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;
}

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
 
T

Tor Iver Wilhelmsen

Chris Brat said:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top