Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Goto? 1

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
0
0
US
I would just like to have some opinions on this as
coding style.
Is this bad style, why or why not?
All opinions appreciated.

int CommitEntry(FILE *f, char *Ed[], int n) {
int y = 0, quit;

if (f) {
fprintf(f,&quot;%s\n&quot;,&quot;<br><blockquote><br>&quot;);
start:
fprintf(f,&quot;%s\n&quot;,Ed[y++]);
quit = y == n ? 1 : 0;
if (quit) {
fprintf(f,&quot;%s\n&quot;,&quot;</blockquote><br><br>&quot;);
fflush(f);
fclose(f);
exit(0);
}
goto start;
}
return 1;
}
 
your code should be changed to folowwing to get readable code:

Code:
int CommitEntry(FILE *f, char *Ed[], int n) {
   int y = 0, quit;

   if (f != 0) {
      fprintf(f,&quot;%s\n&quot;,&quot;<br><blockquote><br>&quot;);
      do {
         fprintf(f,&quot;%s\n&quot;,Ed[y++]);
         if ((quit = (y == n)) != 0) {
            fprintf(f,&quot;%s\n&quot;,&quot;</blockquote><br><br>&quot;);
            fflush(f); /* not necessary needed. because the next statement'll flush it. */
            fclose(f);
            exit(0);
         }
      } while (1)
   }
   return 1;
}
 
hi ..

i like what denniscpp has explained here. you can do almost everything with for, while, if/else and other compound statements.

GOTOs are something that you can almost always do without in your code. One use of GOTO with some sort of legitimacy is in your code that has a very long loop, for example a while(1) is too extended.

GOTO can only branch to labels within the same function.

br:
 
Thanks to you both..

I wasn't exactly asking about jumping
out of functions with *jmp style instructions and creating spaghetti code
but about the overall style, which dennis
provide admirably.

jmnaqi..
You say that a while (1)
is too extended...
Say for instance you are reading
from files passed internally, and sequentially...calling break after the last eof in last file of 'n' is found...
I can find lots of uses for while (1)..
Where would you use a goto instead of
while?

OT:
Does anybody know where I can find some code examples for a decent, but small editor written in C?
 
hi marsd ..

i wasn't pin pointng to your code.
but i say, using a GOTO can be deemed legitimate if you have a very very long while loop - akin to a while(1).

for example:

channel = channel & 07;
channel = channel << 3;
...
while (*adc & 0x01 == _io13)) {
if ((bit14 | channel) != 1)
goto emergency;
...
...
...
}
emergency:
/* input register is invalid */
/* do something here */


in the above example, assume that the while loop is an extended loop. you don't want to really to continue any longer since the input register is corrupted already.

nothing wrong in using goto because it is part of C :). but, a goto is a JUMP equivalent and this makes a program much slower.

br: nagi
 
hi,
jmnagi , in this last case a &quot;break&quot; does the action.

In C we have break,continue,and return statements that
we don't have in PASCAL or other.

To avoid &quot;spaghetti-code&quot;, I suggest to use goto only
when you have to &quot;branch out&quot; of 2 or more loops nested.

bye
 
Yes. A break will do here. But that is what i think jmnagi is trying to explain. We see a lot of ..., ..., ... in his code. May be they are nested/multiple loops ?

Anyway, I never ever had to use gotos in my code. Was told by my tutor a few years back to avoid gotos and like a monkey I just follow it :)

J'étais intelligent soutenu - l'éducation m'a rendu stupide.
 
hi,
microshaft, my post had 3 &quot;paragraph&quot;, the break was in the 1st. The use/abuse of goto was in 3rd.

Also my teacher 20y ago told us to don't use GOTO in PASCAL.
Obviously during university, never wrote GOTO word in a job.

Going to work, finishing to &quot;monkey&quot; professor, when I learn
to walk by my feet, I have learned that 1 GOTO every 20 program, can make the program &quot;readble&quot; more that ten
if nested to dont enter in no more-util statements.

Also in PASCAL, where Wirth put goto only as compatibility,
many columnists spoked about GOTO useful: in PASCAL you
cannot &quot;return&quot; in the middle of a routine: this rets at END. To avoid inutil if, also in PASCAL a GOTO END is good.

bye
 
So the rough consensus is that goto isn't always necessary
and don't use it unless more accepted and efficient(?) controls won't work concisely.
Is this right?

Thanks all.
 
how about construct each loop in their own function, such as:

Code:
void funct()
{
   while (...)
      funct1();
}

void funct1()
{
   while (...)
      funct2();
}

void funct2()
{
   while (...)
      ...
}
 
Status
Not open for further replies.

Similar threads

Replies
3
Views
11

Part and Inventory Search

Sponsor

Back
Top