* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx6h.html
blobbba65cb2489e14150f36f816e6067fb8e0d8d3e9
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
15 <H2>section 3.8: Goto and Labels</H2>
17 pages 65-66
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;
24 some day,
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.
29 </p><p></p><p>page 66
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
36 move
37 the search code out into a separate function.
38 Doing so
39 can make both the ``found'' and ``not found'' cases
40 easier to handle.
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 */
44 <br>
45 <br>
46 int findequal(int a[], int na, int b[], int nb)
48 int i, j;
49 <br>
50 <br>
51 for(i = 0; i &lt; na; i++) {
52 for(j = 0; j &lt; nb; j++) {
53 if(a[i] == b[j])
54 return i;
57 <br>
58 <br>
59 return -1;
61 </pre>This function can then be called as
62 <pre> i = findequal(a, na, b, nb);
63 if(i == -1)
64 /* didn't find any common element */
65 else /* got one */
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.)
68 </p><hr>
69 <p>
70 Read sequentially:
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>
75 </p>
76 <p>
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>
80 </p>
81 </body>
82 </html>