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 | 11 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 2 of 2 — ← Prev page 1 [2]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2014-04-03 08:09 -0700 |
| Message-ID | <lneh1ee9gg.fsf@nuthaus.mib.org> |
| In reply to | #42510 |
rivkaumiller@gmail.com writes:
> On Wednesday, April 2, 2014 7:24:15 PM UTC-7, Keith Thompson wrote:
>> riv...@gmail.com writes:
[...]
>> 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?
It can make debugging easier. You can print the value of the state
variable on each iteration of the loop, either by setting a single
breakpoint or by adding a single printf() call. If you want to trace
every state transition in an FSM implemented with gotos, you have to add
code to every state.
--
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 | JohnF <john@please.see.sig.for.email.com> |
|---|---|
| Date | 2014-04-03 06:36 +0000 |
| Message-ID | <lhivi2$gku$1@reader1.panix.com> |
| In reply to | #42504 |
Keith Thompson <kst-u@mib.org> wrote:
> rivkaumiller@gmail.com writes:
> [snip]
>> can I use a switch-case's case-colon as a
>> label for goto or not?
> [snip]
>
> No, you can't.
> You can always add labels as needed:
> switch (...) {
> case 0:
> L0: /* ... */ break;
> case 1:
> L1: /* ... */ break;
> /* ... */
> default:
> Default: /* ... */ break;
> } /* --- end-of-switch() --- */
> /* ... */
> goto L0;
> goto L1;
> goto Default;
> But I'd really advise you to rethink your design.
Yeah, that's for sure. But just curious -- if one
of those goto's takes execution from outside the
switch's {...} to inside it, will the subsequent
break understand the scope, i.e., where the program
counter should go next? And is that behavior mandated
by standard?
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2014-04-03 07:48 -0400 |
| Message-ID | <lhjhqm$jjv$1@dont-email.me> |
| In reply to | #42512 |
On 04/03/2014 02:36 AM, JohnF wrote:
> Keith Thompson <kst-u@mib.org> wrote:
...
>> You can always add labels as needed:
>> switch (...) {
>> case 0:
>> L0: /* ... */ break;
>> case 1:
>> L1: /* ... */ break;
>> /* ... */
>> default:
>> Default: /* ... */ break;
>> } /* --- end-of-switch() --- */
>> /* ... */
>> goto L0;
>> goto L1;
>> goto Default;
>> But I'd really advise you to rethink your design.
>
> Yeah, that's for sure. But just curious -- if one
> of those goto's takes execution from outside the
> switch's {...} to inside it, will the subsequent
> break understand the scope, i.e., where the program
> counter should go next? And is that behavior mandated
> by standard?
"A break statement terminates execution of the smallest enclosing switch
or iteration statement." (6.8.6.3p2) It doesn't matter how execution of
the program reached the break statement, the only thing that matters is
the location of the break statement.
--
James Kuyper
[toc] | [prev] | [next] | [standalone]
| From | JohnF <john@please.see.sig.for.email.com> |
|---|---|
| Date | 2014-04-03 12:21 +0000 |
| Message-ID | <lhjjof$rv9$1@reader1.panix.com> |
| In reply to | #42532 |
James Kuyper <jameskuyper@verizon.net> wrote:
> On 04/03/2014 02:36 AM, JohnF wrote:
>> Keith Thompson <kst-u@mib.org> wrote:
> ...
>>> You can always add labels as needed:
>>> switch (...) {
>>> case 0:
>>> L0: /* ... */ break;
>>> case 1:
>>> L1: /* ... */ break;
>>> /* ... */
>>> default:
>>> Default: /* ... */ break;
>>> } /* --- end-of-switch() --- */
>>> /* ... */
>>> goto L0;
>>> goto L1;
>>> goto Default;
>>> But I'd really advise you to rethink your design.
>>
>> Yeah, that's for sure. But just curious -- if one
>> of those goto's takes execution from outside the
>> switch's {...} to inside it, will the subsequent
>> break understand the scope, i.e., where the program
>> counter should go next? And is that behavior mandated
>> by standard?
>
> "A break statement terminates execution of the smallest enclosing switch
> or iteration statement." (6.8.6.3p2) It doesn't matter how execution of
> the program reached the break statement, the only thing that matters is
> the location of the break statement.
Thanks, James. I hadn't been aware of that before.
But after posting, I realized I hadn't exactly asked
the complete question that bothered me. Consider the
following snippet,
char label[100] = "printed during loop";
int nloop = 0;
while ( 1 ) {
int i = 0;
entry_pt:
i++;
printf("%s: i=%d\n",label,i);
if ( i >= 10 ) break;
} /* --- continue --- */
if ( ++nloop < 2 ) {
strcpy(label,"printed after goto");
goto entry_pt; }
Okay, so you know the question: what's the
value of i "printed after goto"? And what's the
general rule about that? And how portable is it?
Thanks,
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2014-04-03 10:16 -0400 |
| Message-ID | <lhjqfs$ikr$1@dont-email.me> |
| In reply to | #42533 |
On 04/03/2014 08:21 AM, JohnF wrote:
> James Kuyper <jameskuyper@verizon.net> wrote:
...
>> "A break statement terminates execution of the smallest enclosing switch
>> or iteration statement." (6.8.6.3p2) It doesn't matter how execution of
>> the program reached the break statement, the only thing that matters is
>> the location of the break statement.
>
> Thanks, James. I hadn't been aware of that before.
> But after posting, I realized I hadn't exactly asked
> the complete question that bothered me. Consider the
> following snippet,
> char label[100] = "printed during loop";
> int nloop = 0;
> while ( 1 ) {
> int i = 0;
> entry_pt:
> i++;
> printf("%s: i=%d\n",label,i);
> if ( i >= 10 ) break;
> } /* --- continue --- */
> if ( ++nloop < 2 ) {
> strcpy(label,"printed after goto");
> goto entry_pt; }
> Okay, so you know the question: what's the
> value of i "printed after goto"? And what's the
> general rule about that? And how portable is it?
> Thanks,
The goto statement enters the block containing the definition of 'i', so
a new instance of i is created with an indeterminate value. The goto
skips the initialization of i, which therefore doesn't occur. At least,
that's how I interpret 6.2.4p6. If the indeterminate value of i is
either a trap representation or INT_MAX, the i++ expression renders the
behavior of the entire program undefined. Otherwise, the value of 'i' is
unspecified, but valid, after the jump. In that case, the increment
occurs normally, and INT_MIN < i && i <= INT_MAX at the time of the
printf() call.
If you want i to have a well-defined value after the goto, move the
label before the declaration of i, or make i static.
--
James Kuyper
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2014-04-03 08:12 -0700 |
| Message-ID | <lna9c2e9b5.fsf@nuthaus.mib.org> |
| In reply to | #42534 |
James Kuyper <jameskuyper@verizon.net> writes:
[...]
> If you want i to have a well-defined value after the goto, move the
> label before the declaration of i, or make i static.
Though C has permitted mixed declarations and statements since C99, it
still doesn't permit labels on declarations. This:
label: int i = 0;
is a syntax error. (I just ran into this the other day.)
You can just add a null statement:
label:; int i = 0;
--
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 | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2014-04-03 12:44 -0400 |
| Message-ID | <533D8FDB.30202@verizon.net> |
| In reply to | #42539 |
On 04/03/2014 11:12 AM, Keith Thompson wrote: > James Kuyper <jameskuyper@verizon.net> writes: > [...] >> If you want i to have a well-defined value after the goto, move the >> label before the declaration of i, or make i static. > > Though C has permitted mixed declarations and statements since C99, it > still doesn't permit labels on declarations. This: > > label: int i = 0; > > is a syntax error. (I just ran into this the other day.) > > You can just add a null statement: > > label:; int i = 0; That's what I actually meant, though I didn't say it quite right.
[toc] | [prev] | [next] | [standalone]
| From | JohnF <john@please.see.sig.for.email.com> |
|---|---|
| Date | 2014-04-04 07:06 +0000 |
| Message-ID | <lhllm9$fqa$1@reader1.panix.com> |
| In reply to | #42534 |
James Kuyper <jameskuyper@verizon.net> wrote:
> On 04/03/2014 08:21 AM, JohnF wrote:
>> James Kuyper <jameskuyper@verizon.net> wrote:
> ...
>>> "A break statement terminates execution of the smallest enclosing switch
>>> or iteration statement." (6.8.6.3p2) It doesn't matter how execution of
>>> the program reached the break statement, the only thing that matters is
>>> the location of the break statement.
>>
>> Thanks, James. I hadn't been aware of that before.
>> But after posting, I realized I hadn't exactly asked
>> the complete question that bothered me. Consider the
>> following snippet,
>> char label[100] = "printed during loop";
>> int nloop = 0;
>> while ( 1 ) {
>> int i = 0;
>> entry_pt:
>> i++;
>> printf("%s: i=%d\n",label,i);
>> if ( i >= 10 ) break;
>> } /* --- continue --- */
>> if ( ++nloop < 2 ) {
>> strcpy(label,"printed after goto");
>> goto entry_pt; }
>> Okay, so you know the question: what's the
>> value of i "printed after goto"? And what's the
>> general rule about that? And how portable is it?
>> Thanks,
>
> The goto statement enters the block containing the definition of 'i', so
> a new instance of i is created with an indeterminate value. The goto
> skips the initialization of i, which therefore doesn't occur. At least,
> that's how I interpret 6.2.4p6. If the indeterminate value of i is
> either a trap representation or INT_MAX, the i++ expression renders the
> behavior of the entire program undefined. Otherwise, the value of 'i' is
> unspecified, but valid, after the jump. In that case, the increment
> occurs normally, and INT_MIN < i && i <= INT_MAX at the time of the
> printf() call.
>
> If you want i to have a well-defined value after the goto, move the
> label before the declaration of i, or make i static.
Thanks for additional clarification, James (and for not
pointing out how my i++ was pretty darned useless:).
That seems to just emphasize the earlier judgement that
this kind of branch is more than likely a bad thing.
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2014-04-14 16:38 -0700 |
| Message-ID | <kfnbnw3eb21.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #42534 |
James Kuyper <jameskuyper@verizon.net> writes:
> On 04/03/2014 08:21 AM, JohnF wrote:
>> James Kuyper <jameskuyper@verizon.net> wrote:
> ...
>>> "A break statement terminates execution of the smallest enclosing
>>> switch or iteration statement." (6.8.6.3p2) It doesn't matter how
>>> execution of the program reached the break statement, the only
>>> thing that matters is the location of the break statement.
>>
>> Thanks, James. I hadn't been aware of that before.
>> But after posting, I realized I hadn't exactly asked
>> the complete question that bothered me. Consider the
>> following snippet,
>> char label[100] = "printed during loop";
>> int nloop = 0;
>> while ( 1 ) {
>> int i = 0;
>> entry_pt:
>> i++;
>> printf("%s: i=%d\n",label,i);
>> if ( i >= 10 ) break;
>> } /* --- continue --- */
>> if ( ++nloop < 2 ) {
>> strcpy(label,"printed after goto");
>> goto entry_pt; }
>> Okay, so you know the question: what's the
>> value of i "printed after goto"? And what's the
>> general rule about that? And how portable is it?
>> Thanks,
>
> The goto statement enters the block containing the definition of
> 'i', so a new instance of i is created with an indeterminate
> value. The goto skips the initialization of i, which therefore
> doesn't occur. At least, that's how I interpret 6.2.4p6. If the
> indeterminate value of i is either a trap representation or
> INT_MAX, the i++ expression renders the behavior of the entire
> program undefined. Otherwise, the value of 'i' is unspecified,
> but valid, after the jump. In that case, the increment occurs
> normally, and INT_MIN < i && i <= INT_MAX at the time of the
> printf() call.
This analysis is right for C90 and C99. In C11, the access
is i is simply undefined behavior, under 6.3.2.1 p2.
[toc] | [prev] | [next] | [standalone]
| From | rivkaumiller@gmail.com |
|---|---|
| Date | 2014-04-03 00:18 -0700 |
| Message-ID | <e7b40253-329f-4e90-917f-851ea49cb138@googlegroups.com> |
| In reply to | #42502 |
On Wednesday, April 2, 2014 11:31:03 PM UTC-7, Stefan Ram wrote:
> riv...@gmail.com writes:
>
> >Can I jump to a case inside a switch using goto?
>
> #include <stdio.h>
> int main()
> { int pc = 3;
> #define GOTO(x) pc=x;break
>
> while( 1 )switch( pc )
> { case 2: puts( "2:" ); goto out;
> case 3: puts( "3:" ); GOTO( 2 ); }
> out:; }
> >Can I jump to a case inside a switch using goto?
but you are going through the while loop head to do the GOTO.
In my case, there is a getchar() there so an input stream has been consumed. You are not allowed to go through the loop head.
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2014-04-03 09:49 +0100 |
| Message-ID | <0h9%u.238865$Dj1.181538@fx06.am4> |
| In reply to | #42502 |
<rivkaumiller@gmail.com> wrote in message news:1af492a6-6797-401a-af65-afe48c7d83fc@googlegroups.com... > 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: ? Another way to share code between cases is to put it in functions. So if the code for unique is in dosomething3(), then from rose,pink you just call dosomething3(). No need for a goto. -- Bartc
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c
csiph-web