Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > acceptable use of goto?

Reply
Thread Tools

acceptable use of goto?

 
 
Richard Heathfield
Guest
Posts: n/a
 
      03-22-2008
Bartc said:

<snip>

> A lot of work to eliminate the occasional goto as in the following
> (uncompiled) code:
>
> for (i=1; i<=N; +=i)


Presumably ++i ?

> {
> if (x==2) break;
>
> switch (x)
> {
> case 2:
> goto mylabel;
> };
>
> };
> mylabel:


for(i = 1; i <= N && x != 2; ++i)
{
}

would do it.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
Ed Prochak
Guest
Posts: n/a
 
      03-22-2008
On Mar 22, 6:45 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> Bartc said:
>
> <snip>
>
> > A lot of work to eliminate the occasional goto as in the following
> > (uncompiled) code:

>
> > for (i=1; i<=N; +=i)

>
> Presumably ++i ?
>
> > {
> > if (x==2) break;

>
> > switch (x)
> > {
> > case 2:
> > goto mylabel;
> > };

>
> > };
> > mylabel:

>
> for(i = 1; i <= N && x != 2; ++i)
> {
>
> }
>
> would do it.
>
> <snip>
>
> --
> Richard Heathfield <http://www.cpax.org.uk>


AH, but that fails for the OP, since that requires completing a full
pass thru the loop. But it is useful to point out the feature of
multiple conditions. Just don't abuse it.
That seven page while loop I mentioned previously also had about 5
lines of conditions, about 25contitions all together! It really
should have been two separate programs.

Enjoy.
Ed
 
Reply With Quote
 
 
 
 
Bartc
Guest
Posts: n/a
 
      03-22-2008

"Richard Heathfield" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bartc said:
>
> <snip>
>
>> A lot of work to eliminate the occasional goto as in the following
>> (uncompiled) code:
>>
>> for (i=1; i<=N; +=i)

>
> Presumably ++i ?


Er, yes; I did say it was uncompiled.

>> {
>> if (x==2) break;
>>
>> switch (x)
>> {
>> case 2:
>> goto mylabel;
>> };
>>
>> };
>> mylabel:

>
> for(i = 1; i <= N && x != 2; ++i)
> {
> }
>
> would do it.


For that particular fragment, yes. But in practice I may want to exit in the
middle of the loop.

It just seems wrong that break can be used in a for-loop body, by itself or
in an if statement, but not in a switch statement or a nested for- or
while-loop.

The following is an extract from a piece of code I posted last week. The
goto was left in inadvertently. I wanted to exit the outer while loop from
inside the switch statement.

This example is easy to fix, but the point is I was momentarily thrown
because I couldn't use break there (eg. break 2) -- I had to stop and think
of a workaround. The goto was an instant fix. But I should not have to fight
the language.

while(1)
{...
switch (c)
{
......
default:
goto xyzzy;
};
};
xyzzy:


--
Bart




 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-22-2008
Bartc said:

<snip>
>
> [...] But in practice I may want to exit in
> the middle of the loop.


Understood. At least, kind of understood. I know that such a desire is
relatively commonplace, but it isn't something I have ever felt an urge to
do in C. Bizarrely, it *is* something I felt an urge to do in BASIC, and
goto was the way I solved that problem. But at the same time that I was
learning C, I was solidly won over to the idea of one-entry-one-exit for
every control structure (functions, loops, switches, and ifs). So at the
very moment that I finally got my hands on a language that supported
unstructured loops (via break), I stopped wanting to use them!

> It just seems wrong that break can be used in a for-loop body, by itself
> or in an if statement, but not in a switch statement or a nested for- or
> while-loop.


Yes, break shouldn't be overloaded like that. In fact, break shouldn't be
needed in a switch at all! Rather, there should be a keyword such as
'fallthrough' (okay, maybe there are better names for it), leaving break
purely for breaking out of loops (for those who insist on doing such
things).

> The following is an extract from a piece of code I posted last week. The
> goto was left in inadvertently. I wanted to exit the outer while loop
> from inside the switch statement.


I'd like to say I sympathise, but it's the wrong word here, because I never
want to do that, so sym- doesn't really work. But I can understand your
frustration with the lack of a language feature to fulfil an apparently
simple and obvious desire (my own pet annoyance is the absence of
#ifntypedef!).

> This example is easy to fix, but the point is I was momentarily thrown
> because I couldn't use break there (eg. break 2) -- I had to stop and
> think of a workaround. The goto was an instant fix. But I should not have
> to fight the language.


I don't have to, because I automatically structure my code in such a way
that it is never necessary. On the other hand, my coding style is not one
that seems particularly popular around here!

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-23-2008
Richard Heathfield <(E-Mail Removed)> writes:

> (my own pet annoyance is the absence of
> #ifntypedef!).


They say it is not a new C standard unless static is given a new
meaning, so I think you should propose:

static typedef long number;

to do the typedef only if number is not already a type synonym. It
would do part of what you want.

--
Ben.
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      03-23-2008

"Richard Heathfield" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bartc said:


>> It just seems wrong that break can be used in a for-loop body ...
>> but not in a switch statement or a nested for- or hile-loop


>...leaving break purely for breaking out of loops (for those who insist
> on doing such things).


I could go on at length about why break (and extensions of it) is desirable
and makes code easier to read. But I won't. Just trust me.

>... But I can understand your
> frustration with the lack of a language feature to fulfil an apparently
> simple and obvious desire (my own pet annoyance is the absence of
> #ifntypedef!).


I don't think #ifntypedef is so simple and obvious. I could live without it!

And I think it may not be practical, because these # ops I understand are
processed before any analysis of the underlying C source so will not know
anything about existing typedefs.

--
Bart


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-23-2008
Bartc said:

>
> "Richard Heathfield" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Bartc said:

>
>>> It just seems wrong that break can be used in a for-loop body ...
>>> but not in a switch statement or a nested for- or hile-loop

>
>>...leaving break purely for breaking out of loops (for those who insist
>> on doing such things).

>
> I could go on at length about why break (and extensions of it) is
> desirable and makes code easier to read. But I won't. Just trust me.


And I could go on at length about why it isn't and doesn't. But I will
extend to you the same courtesy that you extended to me.

>>... But I can understand your
>> frustration with the lack of a language feature to fulfil an apparently
>> simple and obvious desire (my own pet annoyance is the absence of
>> #ifntypedef!).

>
> I don't think #ifntypedef is so simple and obvious. I could live without
> it!


Well, I have to live without it, obviously.

>
> And I think it may not be practical, because these # ops I understand are
> processed before any analysis of the underlying C source so will not know
> anything about existing typedefs.


Right. I understand why I can't have it, but that doesn't stop me wanting
it. The "static" suggestion elsethread is intriguing!

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
João Jerónimo
Guest
Posts: n/a
 
      03-25-2008
Richard Heathfield wrote:
> <snip>
>> [...] But in practice I may want to exit in
>> the middle of the loop.

>
> Understood. At least, kind of understood. I know that such a desire is
> relatively commonplace, but it isn't something I have ever felt an urge to
> do in C. Bizarrely, it *is* something I felt an urge to do in BASIC, and
> goto was the way I solved that problem. But at the same time that I was
> learning C, I was solidly won over to the idea of one-entry-one-exit for
> every control structure (functions, loops, switches, and ifs). So at the
> very moment that I finally got my hands on a language that supported
> unstructured loops (via break), I stopped wanting to use them!


If I declare an integer array with:
int myarray[] = {5, 6, 3, 4, 8, 7, 2, 3, 4, 5, 6, 0};
And want to write a routine to print it like this:
(5, 6, 3, 4, 8, 7, 2, 3, 4, 5, 6, 0)
, my best hint would be (untested):

--------------------------------------------------
void print_int_array( int *array, int arraylen )
{
assert( arraylen >= 1 );

int *arrayend = array+arraylen;

printf("(");
for(; {
printf("%d", *array);
array++;
if ( array == arrayend )
break;
printf(", ");
}
printf(")");
}
--------------------------------------------------

Now, please point me out a better solution.
(This is not strictly the case of a loop with multiple exits, but helps to
ilustrate the idea...)

JJ
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-25-2008
Joo Jernimo said:

<snip>

> If I declare an integer array with:
> int myarray[] = {5, 6, 3, 4, 8, 7, 2, 3, 4, 5, 6, 0};
> And want to write a routine to print it like this:
> (5, 6, 3, 4, 8, 7, 2, 3, 4, 5, 6, 0)
> , my best hint would be (untested):
>
> --------------------------------------------------
> void print_int_array( int *array, int arraylen )
> {
> assert( arraylen >= 1 );
>
> int *arrayend = array+arraylen;
>
> printf("(");
> for(; {
> printf("%d", *array);
> array++;
> if ( array == arrayend )
> break;
> printf(", ");
> }
> printf(")");
> }
> --------------------------------------------------
>
> Now, please point me out a better solution.


void print_int_array(int *array, size_t arraylen)
{
assert(arraylen >= 1);
printf("(%d", *array++);
while(--arraylen)
{
printf(", %d", *array++);
}
printf(")");
}

I accept that my solution duplicates the %d line, but the payoff is that,
overall, the code is shorter and simpler.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Morris Dovey
Guest
Posts: n/a
 
      03-25-2008
Richard Heathfield wrote:
>
> Joo Jernimo said:
>
> <snip>
>
> > If I declare an integer array with:
> > int myarray[] = {5, 6, 3, 4, 8, 7, 2, 3, 4, 5, 6, 0};
> > And want to write a routine to print it like this:
> > (5, 6, 3, 4, 8, 7, 2, 3, 4, 5, 6, 0)
> > , my best hint would be (untested):
> >
> > --------------------------------------------------
> > void print_int_array( int *array, int arraylen )
> > {
> > assert( arraylen >= 1 );
> >
> > int *arrayend = array+arraylen;
> >
> > printf("(");
> > for(; {
> > printf("%d", *array);
> > array++;
> > if ( array == arrayend )
> > break;
> > printf(", ");
> > }
> > printf(")");
> > }
> > --------------------------------------------------
> >
> > Now, please point me out a better solution.

>
> void print_int_array(int *array, size_t arraylen)
> {
> assert(arraylen >= 1);
> printf("(%d", *array++);
> while(--arraylen)
> {
> printf(", %d", *array++);
> }
> printf(")");
> }


void print_int_array(int *array, int arraylen)
{ assert(arraylen >= 1);
do printf(" %d",*array++);
while (--arraylen);
}

Is there a prize?

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Acceptable Load on WAN Link ciscoham Cisco 3 10-20-2005 04:28 PM
MCAD/MCSD certificate testing: whats an acceptable exam score rate .Net Sports ASP .Net 2 05-03-2005 05:31 PM
acceptable way to program steve Java 34 02-20-2005 06:37 PM
Is this trick with reset acceptable? valentin tihomirov VHDL 6 04-14-2004 05:52 PM
An "acceptable" use of break Chris Potter C Programming 26 11-07-2003 09:21 PM



Advertisments