1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>section
3.8: Goto and Labels
</title>
10 <link href=
"sx6g.html" rev=precedes
>
11 <link href=
"sx7.html" rel=precedes
>
12 <link href=
"sx6.html" rev=subdocument
>
15 <H2>section
3.8: Goto and Labels
</H2>
18 <p>A tremendous amount of impassioned debate surrounds the lowly
19 <TT>goto
</TT> statement, which exists in many languages.
20 Some people say that
<TT>goto
</TT>s are fine;
21 others say that they must
<strong>never
</strong> be used.
22 You should definitely shy away from
<TT>goto
</TT>s,
23 but don't be dogmatic about it;
25 you'll find yourself writing some screwy piece of code where
26 trying to avoid a
<TT>goto
</TT>
27 (by introducing extra tests or Boolean control variables)
28 would only make things worse.
30 </p><p>When you find yourself writing several nested loops
31 in order to search for something,
32 such that you would need to use a
<TT>goto
</TT>
33 to break out of all of them
34 when you do find what you're looking for,
35 it's often an excellent idea to
37 the search code out into a separate function.
39 can make both the ``found'' and ``not found'' cases
41 Here's a slight variation on the example in the middle of page
66,
42 written as a function:
43 <pre> /* return i such that a[i] == b[j] for some j, or -
1 if none */
46 int findequal(int a[], int na, int b[], int nb)
51 for(i =
0; i
< na; i++) {
52 for(j =
0; j
< nb; j++) {
61 </pre>This function can then be called as
62 <pre> i = findequal(a, na, b, nb);
64 /* didn't find any common element */
66 </pre>(The only disadvantage here is that it's trickier to return
67 <TT>i
</TT> and
<TT>j
</TT> if we need them both.)
71 <a href=
"sx6g.html" rev=precedes
>prev
</a>
72 <a href=
"sx7.html" rel=precedes
>next
</a>
73 <a href=
"sx6.html" rev=subdocument
>up
</a>
74 <a href=
"top.html">top
</a>
77 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
78 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
79 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>