473,408 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

goto to switch labels

Can you goto switch labels?

int i=0; /* arbitrary */

switch( i ) {
case 0:
if( !some_validity_check() ) {
goto error; /* could be default as well */
}
/* proceed normally */
break;
case 1:
if( !some_other_validity_check() ) {
goto error;
}
/* proceed normally */
break;
case 2:
if( !yet_another_validity_check() ) {
goto error;
}
/* proceed normally */
break;
error:
default: /* invalid input */
handle_error();
exit( EXIT_FAILURE ); /* or something */
}
/* proceed normally */

If this is possible, is it a reasonable choice stylistically, to avoid
re-coding the error handling code?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
7 10135
Christopher Benson-Manica wrote:
Can you goto switch labels?

int i=0; /* arbitrary */

switch( i ) {
case 0:
if( !some_validity_check() ) {
goto error; /* could be default as well */
}
/* proceed normally */
break;
case 1:
if( !some_other_validity_check() ) {
goto error;
}
/* proceed normally */
break;
case 2:
if( !yet_another_validity_check() ) {
goto error;
}
/* proceed normally */
break;
error:
default: /* invalid input */
handle_error();
exit( EXIT_FAILURE ); /* or something */
}
/* proceed normally */

If this is possible, is it a reasonable choice stylistically, to avoid
re-coding the error handling code?


It's possible, AFAIK, but it's ugly: burying the
error target inside a `switch' case is a good way to
hide it (for extra obfuscation, label it `defualt:').

Here's a rearrangement that (I think) more readable:

switch (i) {
case 1:
if (! valid1())
goto error;
stuff1();
break;
case 2:
if (! valid2())
goto error;
stuff2();
break;
...
default:
goto error;
}
...
return HOORAY_IT_FINALLY_WORKED;

error:
handle_error();

(Anyone who finds the extra `goto' "inefficient"
is invited to ponder the payoff of optimizing the error
path, especially if the error is fatal.)

--
Er*********@sun.com

Nov 14 '05 #2
In article <news:cj**********@chessie.cirr.com>
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Can you goto switch labels?
If you mean, is there some syntax by which you could eliminate
the separate label "error" here, then "no". On the other hand,
if you mean is it OK to write "goto error" inside the switch,
and then include the parts I left below, then the answer is
"yes":
switch( i ) { [various cases that sometimes "goto error"] break;
error:
default: /* invalid input */
handle_error();
exit( EXIT_FAILURE ); /* or something */
}
/* proceed normally */

If this is possible, is it a reasonable choice stylistically, to avoid
re-coding the error handling code?


I tend to dislike it myself. You could recode it as:

void f(void) {
...
switch (i) {
case ...
... various cases that sometimes "goto error" ...
break;
default:
goto error;
}
... /* proceed normally */
return;

error:
handle_error();
exit(EXIT_FAILURE); /* or something */
}

and I would not object (or at least, would object far less :-) ).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3


Eric Sosman wrote:
Christopher Benson-Manica wrote:

Here's a rearrangement that (I think) more readable:

switch (i) {
case 1:
if (! valid1())
goto error;
stuff1();
break;
case 2:
if (! valid2())
goto error;
stuff2();
break;
...
default:
goto error;
}
...
return HOORAY_IT_FINALLY_WORKED;

error:
handle_error();

(Anyone who finds the extra `goto' "inefficient"
is invited to ponder the payoff of optimizing the error
path, especially if the error is fatal.)


This would be my preference; it doesn't use a goto or negative logic,
and by specifying the value for err you could pass the reason for the
error into handl_error() if you so desired, so that the user would get a
specific error message rather than a general one that could mean
anything (which I find in my programs is more useful, even if I just
output the error number, at least I can then look in the code):

err=0;

switch (i) {
case 1:
if (valid1())
stuff1();
else
err=1;
break;

case 2:
if (valid2())
stuff2();
else
err=2;
break;

default:
err=3;
break;
}

if (err)
handle_error(err);

Dave.
Nov 14 '05 #4

In article <cj**********@chessie.cirr.com>, Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:

switch( i ) {
case 0:
if( !some_validity_check() ) {
goto error; /* could be default as well */
}
/* proceed normally */
break;
error:
default: /* invalid input */
handle_error();
exit( EXIT_FAILURE ); /* or something */
}


Chris and Eric have already weighed in against this practice, on
style grounds, but I thought I'd mention that I find it perfectly
reasonable, if you added a bit more vertical whitespace to make it
more readable. I find it neither ugly nor error-prone.

--
Michael Wojcik mi************@microfocus.com

I would never understand our engineer. But is there anything in this world
that *isn't* made out of words? -- Tawada Yoko (trans. Margaret Mitsutani)
Nov 14 '05 #5
"Michael Wojcik" <mw*****@newsguy.com> wrote in message
news:cj*********@news2.newsguy.com...

In article <cj**********@chessie.cirr.com>, Christopher Benson-Manica

<at***@nospam.cyberspace.org> writes:

switch( i ) {
case 0:
if( !some_validity_check() ) {
goto error; /* could be default as well */
}
/* proceed normally */
break;
error:
default: /* invalid input */
handle_error();
exit( EXIT_FAILURE ); /* or something */
}


Chris and Eric have already weighed in against this practice, on
style grounds, but I thought I'd mention that I find it perfectly
reasonable, if you added a bit more vertical whitespace to make it
more readable. I find it neither ugly nor error-prone.


It is probably actually better than the "fall thru" technique, as it is
more explicit. However I would just set a flag and check it at the end
of the switch.

error_flag = NO_ERR;
switch (i) {
case OOPS:
if( !some_validity_check() )
error_flag = ERR_OOPS;
break;
}
if (error_flag != NO_ERR)
handle_errors(error_flag);
return error_flag;

--
Mabden
Nov 14 '05 #6
Mabden <mabden@sbc_global.net> spoke thus:
It is probably actually better than the "fall thru" technique, as it is
more explicit. However I would just set a flag and check it at the end
of the switch.


I could do that, of course, but something about it strikes me as
inelegant... YMMV :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in
message news:cj**********@chessie.cirr.com...
Mabden <mabden@sbc_global.net> spoke thus:
It is probably actually better than the "fall thru" technique, as it is more explicit. However I would just set a flag and check it at the end of the switch.


I could do that, of course, but something about it strikes me as
inelegant... YMMV :)


Another thought: How often is the "Default:" case going to a good place
to do error processing? Generally, default a catch-all for an unknown
switch item and needs a message like "Bad input detected, get a new
programmer" but an error produced by a valid switch item will usually
need a different kind of message like "File does not exist, I can't do
what you want".

--
Mabden
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Samee Zahur | last post by:
In a recent thread, I saw the topic of goto coming up, so here's a question: With a background of asm langs, I know exactly why goto is to be avoided. But does anyone know of a good way for...
45
by: Debashish Chakravarty | last post by:
K&R pg.66 describes two situations when using goto makes sense. Has anyone here come across situations where using goto provided the most elegant solution. --...
3
by: Vishal Naidu | last post by:
i m new to the C world... i ve been told by my instructors not to use goto stmt.. but no one could give me a satisfactory answer as to why it is so.. plz help me out of this dilemma, coz i use...
17
by: Mike Hofer | last post by:
While I'd toyed with C, C++, and Java over the last 20 years or so, my principal language has been BASIC, QBASIC, then Visual Basic, and finally Visual Basic .NET. But lately, I've been using C#...
77
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that...
3
by: electrician | last post by:
Yes, no GOTO. This is a major blunder on part of the creators of these tools. GOTO gives the programmer the absolute control over the program. Yes, no matter what, a GOTO sends the program to...
67
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is...
15
by: Frank | last post by:
Before I start, please let's not discuss whether goto is evil or not. For generated code, this can make perfect sense. It would be interesting to know a trick how to get C to do a goto to an...
59
by: raashid bhatt | last post by:
why are GOTO's not used they just a simple JMP instructions what's bad about them
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.