Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #42502 > unrolled thread
| Started by | rivkaumiller@gmail.com |
|---|---|
| First post | 2014-04-02 18:48 -0700 |
| Last post | 2014-04-03 09:49 +0100 |
| Articles | 20 on this page of 31 — 14 participants |
Back to article view | Back to comp.lang.c
Can I jump to a case inside a switch using goto? rivkaumiller@gmail.com - 2014-04-02 18:48 -0700
Re: Can I jump to a case inside a switch using goto? Keith Thompson <kst-u@mib.org> - 2014-04-02 19:24 -0700
Re: Can I jump to a case inside a switch using goto? <william@wilbur.25thandClement.com> - 2014-04-02 20:02 -0700
Re: Can I jump to a case inside a switch using goto? rivkaumiller@gmail.com - 2014-04-02 23:04 -0700
Re: Can I jump to a case inside a switch using goto? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-03 07:51 +0000
Re: Can I jump to a case inside a switch using goto? "BartC" <bc@freeuk.com> - 2014-04-03 09:39 +0100
Re: Can I jump to a case inside a switch using goto? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-03 11:52 +0100
Re: Can I jump to a case inside a switch using goto? "BartC" <bc@freeuk.com> - 2014-04-03 12:42 +0100
Re: Can I jump to a case inside a switch using goto? Keith Thompson <kst-u@mib.org> - 2014-04-03 08:04 -0700
Re: Can I jump to a case inside a switch using goto? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-03 15:59 +0000
Re: Can I jump to a case inside a switch using goto? Lowell Gilbert <lgusenet@be-well.ilk.org> - 2014-04-03 14:24 -0400
Re: Can I jump to a case inside a switch using goto? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-04 23:06 +0100
Re: Can I jump to a case inside a switch using goto? Paul N <gw7rib@aol.com> - 2014-04-03 04:15 -0700
Computed GOTOs (Was: Can I jump to a case inside a switch using goto?) gazelle@shell.xmission.com (Kenny McCormack) - 2014-04-03 11:33 +0000
Re: Computed GOTOs Richard <rgrdev_@gmail.com> - 2014-04-03 12:38 +0100
Re: Can I jump to a case inside a switch using goto? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-04 23:22 +0100
Re: Can I jump to a case inside a switch using goto? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-04-03 01:20 -0700
Re: Can I jump to a case inside a switch using goto? rivkaumiller@gmail.com - 2014-04-03 14:51 -0700
Re: Can I jump to a case inside a switch using goto? rivkaumiller@gmail.com - 2014-04-03 14:56 -0700
Re: Can I jump to a case inside a switch using goto? rivkaumiller@gmail.com - 2014-04-02 23:08 -0700
Re: Can I jump to a case inside a switch using goto? Keith Thompson <kst-u@mib.org> - 2014-04-03 08:09 -0700
Re: Can I jump to a case inside a switch using goto? JohnF <john@please.see.sig.for.email.com> - 2014-04-03 06:36 +0000
Re: Can I jump to a case inside a switch using goto? James Kuyper <jameskuyper@verizon.net> - 2014-04-03 07:48 -0400
Re: Can I jump to a case inside a switch using goto? JohnF <john@please.see.sig.for.email.com> - 2014-04-03 12:21 +0000
Re: Can I jump to a case inside a switch using goto? James Kuyper <jameskuyper@verizon.net> - 2014-04-03 10:16 -0400
Re: Can I jump to a case inside a switch using goto? Keith Thompson <kst-u@mib.org> - 2014-04-03 08:12 -0700
Re: Can I jump to a case inside a switch using goto? James Kuyper <jameskuyper@verizon.net> - 2014-04-03 12:44 -0400
Re: Can I jump to a case inside a switch using goto? JohnF <john@please.see.sig.for.email.com> - 2014-04-04 07:06 +0000
Re: Can I jump to a case inside a switch using goto? Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-14 16:38 -0700
Re: Can I jump to a case inside a switch using goto? rivkaumiller@gmail.com - 2014-04-03 00:18 -0700
Re: Can I jump to a case inside a switch using goto? "BartC" <bc@freeuk.com> - 2014-04-03 09:49 +0100
Page 1 of 2 [1] 2 Next page →
| From | rivkaumiller@gmail.com |
|---|---|
| Date | 2014-04-02 18:48 -0700 |
| Subject | Can I jump to a case inside a switch using goto? |
| Message-ID | <1af492a6-6797-401a-af65-afe48c7d83fc@googlegroups.com> |
while((c=getchar())!=EOF){
switch(flower){
case rose:
switch(color){
case white:
// do something 1
case pink:
// do something 2
...
}
case lily:
switch(color){
case white:
// do something 1
case pink:
// do something 2
...
}
...
case unique:
// do something 3
...
}
}
basically, I can write the above statement completely using
if(flower==rose) goto
if(color==white) goto
type statements.
unfortunately, some of the flowers have strange names such as the ascii value of '/' or other characters like '%' and so on so switch-case gives me brevity.
However, inside switch-case I want to jump to certain cases before breaking to the end.
for example from (rose,pink) I may want to goto unique: . Can I do that by a
goto unique: ?
Note its a unique label. It is also an alias for another case where I want to do something unique before going into the while cycle which will read a char and then break down to the end.
I have a few choices.
One is that I have that label outside both of the switch-cases and its not a switch case label. There, if I want to fall through the cases below, I will have to use goto end: for all of them.
However, I can also put some dummy labels with break in the outermost switch-case and use them as labels.
In crux, the question is if I can use a switch-case's case-colon as a label for goto or not?
If so, then can I use a case like '%'-colon as a case for goto using
goto '%' ?
Thanks
[toc] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2014-04-02 19:24 -0700 |
| Message-ID | <lnmwg3dub4.fsf@nuthaus.mib.org> |
| In reply to | #42502 |
rivkaumiller@gmail.com writes:
[snip]
> In crux, the question is if I can use a switch-case's case-colon as a
> label for goto or not?
[snip]
No, you can't. The target of a goto must be a the name of a label
defined with the "identifer ": syntax; a case label doesn't qualify.
You can always add labels as needed:
switch (...) {
case 0:
L0:
/* ... */
break;
case 1:
L1:
/* ... */
break;
/* ... */
default:
Default:
/* ... */
break;
}
/* ... */
goto L1;
goto L2;
goto Default;
But I'd really advise you to rethink your design. Careful use of goto
statements, usually to a pointer *later* in the code, can be useful for
error handling and for breaking out of nested loops, but heavy use of
gotos can easily result in spaghetti code.
If you're implementing a finite state machine, I suggest either a switch
statement in a loop, where the switch executes some chunk of code
depending on the current state, *or* a sequence of labelled blocks with
gotos. (I personally prefer the former; for one thing, encoding the
current state in a variable rather than having it be implicit in the
current location in the program can be helpful). Mixing case labels and
gotos could easily get out of control.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | <william@wilbur.25thandClement.com> |
|---|---|
| Date | 2014-04-02 20:02 -0700 |
| Message-ID | <pivu0b-r1u.ln1@wilbur.25thandClement.com> |
| In reply to | #42504 |
Keith Thompson <kst-u@mib.org> wrote: > rivkaumiller@gmail.com writes: > [snip] >> In crux, the question is if I can use a switch-case's case-colon as a >> label for goto or not? > [snip] > > No, you can't. The target of a goto must be a the name of a label > defined with the "identifer ": syntax; a case label doesn't qualify. > > You can always add labels as needed: <snip> > If you're implementing a finite state machine, I suggest either a switch > statement in a loop, where the switch executes some chunk of code > depending on the current state, *or* a sequence of labelled blocks with > gotos. (I personally prefer the former; for one thing, encoding the > current state in a variable rather than having it be implicit in the > current location in the program can be helpful). Mixing case labels and > gotos could easily get out of control. It's a shame the committee never took up computed gotos. For complex state machines you often need to jump around. Sure, you can set the next state and break out of the switch statement. But often times code will be nested inside loops, so you need to use a goto, anyhow, to break out. And that goto often takes you back to the _top_ of some outer loop. Plus, if you care about performance, you want to try to thread your instructions to avoid the loop conditional. I suppose it's possible with switch statements, as long as your compiler is smart enough, but computed gotos make it so much easier. I've had some success with macro solutions which hide two implementations--one using switch and another computed gotos. With GCC and clang computed goto machines are always significantly faster. (And I never use GCC's recommendation of storing label offsets, because that's a gigantic pain in the butt--impossible if you generate cases or labels with __LINE__--and in the age of C++ nobody will notice the insignicant link-time costs.)
[toc] | [prev] | [next] | [standalone]
| From | rivkaumiller@gmail.com |
|---|---|
| Date | 2014-04-02 23:04 -0700 |
| Message-ID | <bae63adc-d1d2-465d-9aeb-ffb2032121bd@googlegroups.com> |
| In reply to | #42505 |
On Wednesday, April 2, 2014 8:02:17 PM UTC-7, wil...@wilbur.25thandClement.com wrote: > Keith Thompson <kst-u@mib.org> wrote: > > > riv...@gmail.com writes: > > > [snip] > > >> In crux, the question is if I can use a switch-case's case-colon as a > > >> label for goto or not? > > > [snip] > > > > > > No, you can't. The target of a goto must be a the name of a label > > > defined with the "identifer ": syntax; a case label doesn't qualify. > > > > > > You can always add labels as needed: > > <snip> > > > If you're implementing a finite state machine, I suggest either a switch > > > statement in a loop, where the switch executes some chunk of code > > > depending on the current state, *or* a sequence of labelled blocks with > > > gotos. (I personally prefer the former; for one thing, encoding the > > > current state in a variable rather than having it be implicit in the > > > current location in the program can be helpful). Mixing case labels and > > > gotos could easily get out of control. > Wil... , you seem to cover a number of topics rapidly in your reply. Unfortunately, google does not give an option to email you either. What is computed goto and which languages have it? > > It's a shame the committee never took up computed gotos. For complex state > > machines you often need to jump around. Sure, you can set the next state and > > break out of the switch statement. But often times code will be nested > > inside loops, so you need to use a goto, anyhow, to break out. And that goto > > often takes you back to the _top_ of some outer loop. > I wanted to see some examples. > > Plus, if you care about performance, you want to try to thread your > > instructions to avoid the loop conditional. I suppose it's possible with > > switch statements, as long as your compiler is smart enough, but computed > > gotos make it so much easier. > Again some concrete examples would clarify the vision in your mind. > > I've had some success with macro solutions which hide two specific macros and the example? > implementations--one using switch and another computed gotos. With GCC and > > clang computed goto machines are always significantly faster. (And I never > > use GCC's recommendation of storing label offsets, because that's a gigantic > > pain in the butt--impossible if you generate cases or labels with > > __LINE__--and in the age of C++ nobody will notice the insignicant link-time > > costs.) Again, more writing is needed to clarify your point and generate benefit.
[toc] | [prev] | [next] | [standalone]
| From | glen herrmannsfeldt <gah@ugcs.caltech.edu> |
|---|---|
| Date | 2014-04-03 07:51 +0000 |
| Message-ID | <lhj3tc$5kd$1@speranza.aioe.org> |
| In reply to | #42509 |
rivkaumiller@gmail.com wrote:
> On Wednesday, April 2, 2014 8:02:17 PM UTC-7, wil...@wilbur.25thandClement.com wrote:
(snip)
> What is computed goto and which languages have it?
As far as I know, it started with Fortran I in 1956.
GOTO (10, 20, 30, 40, 50, 60), J
Goes to the first statement number if J is 1, the second if J is 2,
and so on. In early Fortran (before 1977) it was undefined if J
was less than 1 or greater than the number of labels. Many as an
extension, and added in Fortran 77, if J is out of range, it goes
to the following statement.
Many BASIC systems implement computed GOTO as
100 ON J GOTO 10, 20, 30, 40, 50, 60
PL/I has LABEL variable arrays, which can either be initialized
with the appropriate labels on the DECLARE, or by specifying an
array element as a statement label. The latter doesn't look so
different from switch/case.
DCL X(5) LABEL INITIAL(ONE, TWO, THREE);
or:
DCL Y(5) LABEL;
then label statements such as:
Y(3): PUT LIST('Three');
Then GOTO X(I); or GOTO Y(J); will go to the appropriate statement.
I think ALGOL-68 has one, but I am not sure. The description is
in its own language, and not so easy to figure out.
As I happen to have a COBOL manual nearby, though haven't actually
written any programs, it seems to have:
GOTO labels DEPENDING ON variable.
-- glen
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2014-04-03 09:39 +0100 |
| Message-ID | <B79%u.322873$rF1.279656@fx15.am4> |
| In reply to | #42520 |
"glen herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in message
news:lhj3tc$5kd$1@speranza.aioe.org...
> rivkaumiller@gmail.com wrote:
>> On Wednesday, April 2, 2014 8:02:17 PM UTC-7,
>> wil...@wilbur.25thandClement.com wrote:
> (snip)
>
>> What is computed goto and which languages have it?
>
> As far as I know, it started with Fortran I in 1956.
>
> GOTO (10, 20, 30, 40, 50, 60), J
So what's the difference between that, and:
switch (J) {
case 1:
case 2:
....
case 6:
?
> I think ALGOL-68 has one, but I am not sure. The description is
> in its own language, and not so easy to figure out.
More than likely; you'd just have a row of labels, and perhaps use it
directly as in goto (L1,L2,L3)[J].
But it also has case in... out.
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2014-04-03 11:52 +0100 |
| Message-ID | <0.14b58597798c773ac5d7.20140403115200BST.87r45er8hb.fsf@bsb.me.uk> |
| In reply to | #42525 |
"BartC" <bc@freeuk.com> writes: > "glen herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in message > news:lhj3tc$5kd$1@speranza.aioe.org... <snip> >> I think ALGOL-68 has one, but I am not sure. The description is >> in its own language, and not so easy to figure out. > > More than likely; you'd just have a row of labels, and perhaps use it > directly as in goto (L1,L2,L3)[J]. Not exactly. A label evaluates to a procedure (of mode PROC VOID) so you can make a row of them, but you have to "call" the procedure, rather than use goto directly. [] PROC VOID labels = (L1, L2, L3); ... labels[j]; > But it also has case in... out. or rather "case in ... out ... esac". -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2014-04-03 12:42 +0100 |
| Message-ID | <ZOb%u.1$4t5.0@fx12.am4> |
| In reply to | #42527 |
"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
news:0.14b58597798c773ac5d7.20140403115200BST.87r45er8hb.fsf@bsb.me.uk...
> "BartC" <bc@freeuk.com> writes:
>
>> "glen herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in message
>>> I think ALGOL-68 has one, but I am not sure. The description is
>>> in its own language, and not so easy to figure out.
>>
>> More than likely; you'd just have a row of labels, and perhaps use it
>> directly as in goto (L1,L2,L3)[J].
>
> Not exactly. A label evaluates to a procedure (of mode PROC VOID) so
> you can make a row of them, but you have to "call" the procedure, rather
> than use goto directly.
>
> [] PROC VOID labels = (L1, L2, L3);
> ...
> labels[j];
I've just tried it, and you're right. But like this, it's not quite as
succinct. (I base my own syntaxes on A68; with a dynamic language, I can
actually write goto (L1,L2,L3)[J], but will also need goto labels[j] for a
static one. In both cases though I insist on 'goto' to make it clear what
this is.)
But, the fact that A68 treats such a label as a kind of local proc name
gives me an idea for an idea for a language feature that might help the OP
(if it could somehow be implemented today in C).
This is a lightweight call that would pass control to a labelled block
somewhere in this function, just like goto, but then returns at the end of
the block. Access to all local variables is maintained as normal.
Encountering the block also executes it as normal:
rose:
dosomething2();
gosub dosomething3; // use gosub rather than goto or ()
...
unique:
dosomething3:{
.....
}
Well, it's an idea ...
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2014-04-03 08:04 -0700 |
| Message-ID | <lnioqqe9nv.fsf@nuthaus.mib.org> |
| In reply to | #42531 |
"BartC" <bc@freeuk.com> writes:
[...]
> But, the fact that A68 treats such a label as a kind of local proc name
> gives me an idea for an idea for a language feature that might help the OP
> (if it could somehow be implemented today in C).
>
> This is a lightweight call that would pass control to a labelled block
> somewhere in this function, just like goto, but then returns at the end of
> the block. Access to all local variables is maintained as normal.
> Encountering the block also executes it as normal:
>
> rose:
> dosomething2();
> gosub dosomething3; // use gosub rather than goto or ()
> ...
> unique:
> dosomething3:{
> .....
> }
>
> Well, it's an idea ...
gcc supports nested functions as an extension, which strike me as a
cleaner way to do the same thing. The only difference would be that a
goto can't (as far as I know) jump from one function to another, even if
one is nested in the other, but I don't think that's a disadvantage.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | glen herrmannsfeldt <gah@ugcs.caltech.edu> |
|---|---|
| Date | 2014-04-03 15:59 +0000 |
| Message-ID | <lhk0hu$hf9$1@speranza.aioe.org> |
| In reply to | #42537 |
Keith Thompson <kst-u@mib.org> wrote:
> "BartC" <bc@freeuk.com> writes:
(snip on Algol-68 feature)
>> This is a lightweight call that would pass control to a labelled block
>> somewhere in this function, just like goto, but then returns at the end of
>> the block. Access to all local variables is maintained as normal.
>> Encountering the block also executes it as normal:
>> rose:
>> dosomething2();
>> gosub dosomething3; // use gosub rather than goto or ()
>> ...
>> unique:
>> dosomething3:{
>> .....
>> }
>> Well, it's an idea ...
> gcc supports nested functions as an extension, which strike me as a
> cleaner way to do the same thing. The only difference would be that a
> goto can't (as far as I know) jump from one function to another, even if
> one is nested in the other, but I don't think that's a disadvantage.
Pascal and PL/I allow GOTO out of internal functions, but not in.
In PL/I, you can do it with label variables.
C has longjmp() if you really want to do it.
-- glen
[toc] | [prev] | [next] | [standalone]
| From | Lowell Gilbert <lgusenet@be-well.ilk.org> |
|---|---|
| Date | 2014-04-03 14:24 -0400 |
| Message-ID | <44ppkyuv96.fsf@lowell-desk.lan> |
| In reply to | #42527 |
Ben Bacarisse <ben.usenet@bsb.me.uk> writes: > "BartC" <bc@freeuk.com> writes: > >> "glen herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in message >> news:lhj3tc$5kd$1@speranza.aioe.org... > <snip> >>> I think ALGOL-68 has one, but I am not sure. The description is >>> in its own language, and not so easy to figure out. >> >> More than likely; you'd just have a row of labels, and perhaps use it >> directly as in goto (L1,L2,L3)[J]. > > Not exactly. A label evaluates to a procedure (of mode PROC VOID) so > you can make a row of them, but you have to "call" the procedure, rather > than use goto directly. > > [] PROC VOID labels = (L1, L2, L3); > ... > labels[j]; But that's just a funtion table, which is even more common in these types of languages... -- Lowell Gilbert, embedded/networking software engineer http://be-well.ilk.org/~lowell/
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2014-04-04 23:06 +0100 |
| Message-ID | <0.61bdc8d1f45ca7b05a09.20140404230612BST.87lhvkrbqj.fsf@bsb.me.uk> |
| In reply to | #42546 |
Lowell Gilbert <lgusenet@be-well.ilk.org> writes: > Ben Bacarisse <ben.usenet@bsb.me.uk> writes: > >> "BartC" <bc@freeuk.com> writes: >> >>> "glen herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in message >>> news:lhj3tc$5kd$1@speranza.aioe.org... >> <snip> >>>> I think ALGOL-68 has one, but I am not sure. The description is >>>> in its own language, and not so easy to figure out. >>> >>> More than likely; you'd just have a row of labels, and perhaps use it >>> directly as in goto (L1,L2,L3)[J]. >> >> Not exactly. A label evaluates to a procedure (of mode PROC VOID) so >> you can make a row of them, but you have to "call" the procedure, rather >> than use goto directly. >> >> [] PROC VOID labels = (L1, L2, L3); >> ... >> labels[j]; > > But that's just a funtion table, which is > even more common in these types of languages... Yes, it looks like a function table but it's not quite the same as one. For one thing, the values don't actually derive from functions, and they also become invalid if the table is returned form the function that contains the labels. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Paul N <gw7rib@aol.com> |
|---|---|
| Date | 2014-04-03 04:15 -0700 |
| Message-ID | <d2d61fb2-b51f-498b-9a89-c230b7f55ff9@googlegroups.com> |
| In reply to | #42520 |
On Thursday, 3 April 2014 08:51:08 UTC+1, glen herrmannsfeldt wrote:
> rivkaumiller@gmail.com wrote:
> > On Wednesday, April 2, 2014 8:02:17 PM UTC-7, wil...@wilbur.25thandClement.com wrote:
> (snip)
>
> > What is computed goto and which languages have it?
>
> As far as I know, it started with Fortran I in 1956.
>
> GOTO (10, 20, 30, 40, 50, 60), J
>
> Goes to the first statement number if J is 1, the second if J is 2,
> and so on. In early Fortran (before 1977) it was undefined if J
> was less than 1 or greater than the number of labels. Many as an
> extension, and added in Fortran 77, if J is out of range, it goes
> to the following statement.
>
> Many BASIC systems implement computed GOTO as
>
> 100 ON J GOTO 10, 20, 30, 40, 50, 60
>
> PL/I has LABEL variable arrays, which can either be initialized
> with the appropriate labels on the DECLARE, or by specifying an
> array element as a statement label. The latter doesn't look so
> different from switch/case.
>
> DCL X(5) LABEL INITIAL(ONE, TWO, THREE);
>
> or:
>
> DCL Y(5) LABEL;
>
> then label statements such as:
>
> Y(3): PUT LIST('Three');
>
> Then GOTO X(I); or GOTO Y(J); will go to the appropriate statement.
>
> I think ALGOL-68 has one, but I am not sure. The description is
> in its own language, and not so easy to figure out.
>
> As I happen to have a COBOL manual nearby, though haven't actually
> written any programs, it seems to have:
>
> GOTO labels DEPENDING ON variable.
In BCPL, labels were numbers just like any other variable. So you could do:
GOTO val -> lab1 , lab2
to choose one of two targets ( -> is the BCPL equivalent of ? : )
or you could use an array; or you could even do
GOTO lab + 4
if jumping to slightly after a label is your idea of fun.
[toc] | [prev] | [next] | [standalone]
| From | gazelle@shell.xmission.com (Kenny McCormack) |
|---|---|
| Date | 2014-04-03 11:33 +0000 |
| Subject | Computed GOTOs (Was: Can I jump to a case inside a switch using goto?) |
| Message-ID | <lhjgut$f5q$1@news.xmission.com> |
| In reply to | #42528 |
In article <d2d61fb2-b51f-498b-9a89-c230b7f55ff9@googlegroups.com>,
Paul N <gw7rib@aol.com> wrote:
...
>In BCPL, labels were numbers just like any other variable. So you could do:
>
>GOTO val -> lab1 , lab2
>
>to choose one of two targets ( -> is the BCPL equivalent of ? : )
>
>or you could use an array; or you could even do
>
>GOTO lab + 4
>
>if jumping to slightly after a label is your idea of fun.
Note, incidentally, that you *can* implement computed goto in C - in fact,
in entirely standard (on topic) C. The details of how to do it escape me
at the moment, but it involves setjmp and longjmp. Basically, you set up
an array of jmpbufs and then longjmp to the one you want. Somebody figured
this out and explained it to me sometime back in the 80s.
Perhaps someone here will take it on as a challenge...
--
Atheism:
It's like being the only sober person in the car, and nobody will let you drive.
[toc] | [prev] | [next] | [standalone]
| From | Richard <rgrdev_@gmail.com> |
|---|---|
| Date | 2014-04-03 12:38 +0100 |
| Subject | Re: Computed GOTOs |
| Message-ID | <874n2a8wxk.fsf@gmail.com> |
| In reply to | #42529 |
gazelle@shell.xmission.com (Kenny McCormack) writes: > In article <d2d61fb2-b51f-498b-9a89-c230b7f55ff9@googlegroups.com>, > Paul N <gw7rib@aol.com> wrote: > ... >>In BCPL, labels were numbers just like any other variable. So you could do: >> >>GOTO val -> lab1 , lab2 >> >>to choose one of two targets ( -> is the BCPL equivalent of ? : ) >> >>or you could use an array; or you could even do >> >>GOTO lab + 4 >> >>if jumping to slightly after a label is your idea of fun. > > Note, incidentally, that you *can* implement computed goto in C - in fact, > in entirely standard (on topic) C. The details of how to do it escape me > at the moment, but it involves setjmp and longjmp. Basically, you set up > an array of jmpbufs and then longjmp to the one you want. Somebody figured > this out and explained it to me sometime back in the 80s. > > Perhaps someone here will take it on as a challenge... Now you've done it. -- "Avoid hyperbole at all costs, its the most destructive argument on the planet" - Mark McIntyre in comp.lang.c
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2014-04-04 23:22 +0100 |
| Message-ID | <0.2d563f49ad86e6d3dc2f.20140404232259BST.87eh1crayk.fsf@bsb.me.uk> |
| In reply to | #42528 |
Paul N <gw7rib@aol.com> writes:
> On Thursday, 3 April 2014 08:51:08 UTC+1, glen herrmannsfeldt wrote:
>> rivkaumiller@gmail.com wrote:
>> > On Wednesday, April 2, 2014 8:02:17 PM UTC-7, wil...@wilbur.25thandClement.com wrote:
>> (snip)
>>
>> > What is computed goto and which languages have it?
>>
>> As far as I know, it started with Fortran I in 1956.
>>
>> GOTO (10, 20, 30, 40, 50, 60), J
<snip>
> In BCPL, labels were numbers just like any other variable. So you could do:
>
> GOTO val -> lab1 , lab2
>
> to choose one of two targets ( -> is the BCPL equivalent of ? : )
>
> or you could use an array; or you could even do
>
> GOTO lab + 4
>
> if jumping to slightly after a label is your idea of fun.
There's two kinds of "computed" here: the index into a list of labels
and the label list itself. In Fortran, the list was static --
effectively a literal array -- and only the index is "computed". That
kind of computed goto can be done in C:
switch (J) { case 1: goto L1; case 2: goto L2; ... }
It's clumsy to write, but the functionality is there (though I'm blessed
never to have see it used!).
In BCPL and in Algol 68 (and C with gcc's extensions) you can compute
with the labels themselves, though there may be very severe
restrictions on exactly what you can do. Once you can play with the
value of a label in some way (however limited) you get more
opportunities for "fun".
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2014-04-03 01:20 -0700 |
| Message-ID | <ddc710db-6296-4bd4-ae05-19b63eaa7ad7@googlegroups.com> |
| In reply to | #42509 |
On Thursday, April 3, 2014 7:04:00 AM UTC+1, rivkau...@gmail.com wrote: > > > What is computed goto and which languages have it? > Fortran has it. Minibasic http://sourceforge.net/directory/?q=minibasic also has it. You use integers for lablels, then goto x; is allowed, where x is an integer variable. In Minibasic, is was a simply a case of writing the parser as goto (expression) rather than goto (number). But I rather regret allowing it, because it made writing the optimised version of the interpreter a lot more difficult. Switch is a bit slow in a deeply nested loop. For example, when you're emulating a processor in C, you need to switch on the instruction. That's often the rate-limiting step in the program. A computed goto can often prove faster (shift the instruction up four bits, add to the base, jump, and make sure all your ops are exactly 16 instructions long).
[toc] | [prev] | [next] | [standalone]
| From | rivkaumiller@gmail.com |
|---|---|
| Date | 2014-04-03 14:51 -0700 |
| Message-ID | <73044e06-8b97-43e3-ab84-11843e9fa01e@googlegroups.com> |
| In reply to | #42505 |
On Wednesday, April 2, 2014 8:02:17 PM UTC-7, wil...@wilbur.25thandClement.com wrote: > Keith Thompson <kst-u@mib.org> wrote: > > > riv...@gmail.com writes: > > > [snip] > > >> In crux, the question is if I can use a switch-case's case-colon as a > > >> label for goto or not? > > > [snip] > > > > > > No, you can't. The target of a goto must be a the name of a label > > > defined with the "identifer ": syntax; a case label doesn't qualify. > > > > > > You can always add labels as needed: > > <snip> > > > If you're implementing a finite state machine, I suggest either a switch > > > statement in a loop, where the switch executes some chunk of code > > > depending on the current state, *or* a sequence of labelled blocks with > > > gotos. (I personally prefer the former; for one thing, encoding the > > > current state in a variable rather than having it be implicit in the > > > current location in the program can be helpful). Mixing case labels and > > > gotos could easily get out of control. > There is a phenomenal amount of context in this thread, could you just give a 10 sentence summary of the consensus - if one exists? > > It's a shame the committee never took up computed gotos. For complex state > > machines you often need to jump around. Sure, you can set the next state and > > break out of the switch statement. But often times code will be nested > > inside loops, so you need to use a goto, anyhow, to break out. And that goto > > often takes you back to the _top_ of some outer loop. > > > > Plus, if you care about performance, you want to try to thread your > > instructions to avoid the loop conditional. I suppose it's possible with > > switch statements, as long as your compiler is smart enough, but computed > > gotos make it so much easier. > > > > I've had some success with macro solutions which hide two > > implementations--one using switch and another computed gotos. Could you or someone else give a real example? > With GCC and > > clang computed goto machines are always significantly faster. (And I never > > use GCC's recommendation of storing label offsets, because that's a gigantic > > pain in the butt--impossible if you generate cases or labels with > > __LINE__--and in the age of C++ nobody will notice the insignicant link-time > > costs.) Again, could you or someone else give a real example?
[toc] | [prev] | [next] | [standalone]
| From | rivkaumiller@gmail.com |
|---|---|
| Date | 2014-04-03 14:56 -0700 |
| Message-ID | <501b1019-353c-4567-ae8b-af96a57a36cd@googlegroups.com> |
| In reply to | #42505 |
On Wednesday, April 2, 2014 8:02:17 PM UTC-7, wil...@wilbur.25thandClement.com wrote: > Keith Thompson <kst-u@mib.org> wrote: > > > riv...@gmail.com writes: > > > [snip] > > >> In crux, the question is if I can use a switch-case's case-colon as a > > >> label for goto or not? > > > [snip] > > > > > > No, you can't. The target of a goto must be a the name of a label > > > defined with the "identifer ": syntax; a case label doesn't qualify. > > > > > > You can always add labels as needed: > > <snip> > > > If you're implementing a finite state machine, I suggest either a switch > > > statement in a loop, where the switch executes some chunk of code > > > depending on the current state, *or* a sequence of labelled blocks with > > > gotos. (I personally prefer the former; for one thing, encoding the > > > current state in a variable rather than having it be implicit in the > > > current location in the program can be helpful). Mixing case labels and > > > gotos could easily get out of control. > There is a phenomenal amount of content in this thread, could you just give a 10 sentence summary of the consensus - if one exists? Could you reword and expand claims in your post below by real examples where I indicate a need for clarity? > > It's a shame the committee never took up computed gotos. For complex state > > machines you often need to jump around. Sure, you can set the next state and > > break out of the switch statement. But often times code will be nested > > inside loops, so you need to use a goto, anyhow, to break out. And that goto > > often takes you back to the _top_ of some outer loop. > > > > Plus, if you care about performance, you want to try to thread your > > instructions to avoid the loop conditional. I suppose it's possible with > > switch statements, as long as your compiler is smart enough, but computed > > gotos make it so much easier. > > > > I've had some success with macro solutions which hide two > > implementations--one using switch and another computed gotos. Could you or someone else give a real example? > With GCC and > > clang computed goto machines are always significantly faster. (And I never > > use GCC's recommendation of storing label offsets, because that's a gigantic > > pain in the butt--impossible if you generate cases or labels with > > __LINE__--and in the age of C++ nobody will notice the insignicant link-time > > costs.) Again, could you or someone else give a real example?
[toc] | [prev] | [next] | [standalone]
| From | rivkaumiller@gmail.com |
|---|---|
| Date | 2014-04-02 23:08 -0700 |
| Message-ID | <0736c397-c286-4bb3-abe1-ba79eded66ec@googlegroups.com> |
| In reply to | #42504 |
On Wednesday, April 2, 2014 7:24:15 PM UTC-7, Keith Thompson wrote:
> riv...@gmail.com writes:
>
> [snip]
>
> > In crux, the question is if I can use a switch-case's case-colon as a
>
> > label for goto or not?
>
> [snip]
>
>
>
> No, you can't. The target of a goto must be a the name of a label
>
> defined with the "identifer ": syntax; a case label doesn't qualify.
>
>
>
> You can always add labels as needed:
>
>
>
> switch (...) {
>
> case 0:
>
> L0:
>
> /* ... */
>
> break;
>
> case 1:
>
> L1:
>
> /* ... */
>
> break;
>
> /* ... */
>
> default:
>
> Default:
>
> /* ... */
>
> break;
>
> }
>
> /* ... */
>
> goto L1;
>
> goto L2;
>
> goto Default;
>
>
>
> But I'd really advise you to rethink your design. Careful use of goto
>
> statements, usually to a pointer *later* in the code, can be useful for
>
> error handling and for breaking out of nested loops, but heavy use of
>
> gotos can easily result in spaghetti code.
>
>
>
> If you're implementing a finite state machine, I suggest either a switch
>
> statement in a loop, where the switch executes some chunk of code
>
> depending on the current state, *or* a sequence of labelled blocks with
>
> gotos. (I personally prefer the former; for one thing, encoding the
>
> current state in a variable rather than having it be implicit in the
>
> current location in the program can be helpful). Mixing case labels and
>
> gotos could easily get out of control.
>
Thanks. I understood all your points except this one.
"for one thing, encoding the
>
> current state in a variable rather than having it be implicit in the
>
> current location in the program can be helpful)"
How can it be helpful? what are the comparative limitations of each approach?
Second, can you try to make sense of the points made by wil... , the next poster?
> --
>
> Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
>
> Working, but not speaking, for JetHead Development, Inc.
>
> "We must do something. This is something. Therefore, we must do this."
>
> -- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c
csiph-web