* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx6g.html
blobfe54cadf9711dae209f35888122d5dcc60392c7e
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.7: Break and Continue</title>
10 <link href="sx6f.html" rev=precedes>
11 <link href="sx6h.html" rel=precedes>
12 <link href="sx6.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 3.7: Break and Continue</H2>
17 pages 64-65
18 <p>Note that a <TT>break</TT> inside a <TT>switch</TT> inside a loop
19 causes a break out of the <TT>switch</TT>,
20 while a <TT>break</TT> inside a loop inside a <TT>switch</TT>
21 causes a break out of the loop.
22 </p><p>Neither <TT>break</TT> nor <TT>continue</TT> has any effect on a
23 brace-enclosed block of statements following an <TT>if</TT>.
24 <TT>break</TT> causes a break out of the innermost <TT>switch</TT> or loop,
25 and <TT>continue</TT> forces the next iteration of the innermost loop.
26 </p><p>There is no way of forcing a <TT>break</TT> or <TT>continue</TT> to
27 act on an outer loop.
28 </p><p>Another example of where <TT>continue</TT> is useful is when
29 processing data files.
30 It's often useful to allow comments in data files;
31 one convention is that a line beginning with a <TT>#</TT> character
32 is a comment,
33 and should be ignored by any program reading the file.
34 This can be coded with something like
35 <pre> while(getline(line, MAXLINE) &gt; 0) {
36 if(line[0] == '#')
37 continue;
38 <br>
39 <br>
40 /* process data file line */
42 </pre>The alternative,
43 without a <TT>continue</TT>,
44 would be
45 <pre> while(getline(line, MAXLINE) &gt; 0) {
46 if(line[0] != '#') {
47 /* process data file line */
50 </pre>but now the processing of normal data file lines has been made
51 subordinate to comment lines.
52 (Also, as the authors note,
53 it pushes most of the body of the loop over by another tab stop.)
54 Since comments are exceptional,
55 it's nice to test for them,
56 get them out of the way,
57 and go on about our business,
58 which the code using <TT>continue</TT> nicely expresses.
59 </p><hr>
60 <p>
61 Read sequentially:
62 <a href="sx6f.html" rev=precedes>prev</a>
63 <a href="sx6h.html" rel=precedes>next</a>
64 <a href="sx6.html" rev=subdocument>up</a>
65 <a href="top.html">top</a>
66 </p>
67 <p>
68 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
69 // <a href="copyright.html">Copyright</a> 1995, 1996
70 // <a href="mailto:scs@eskimo.com">mail feedback</a>
71 </p>
72 </body>
73 </html>