1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
<html lang="en">
<head>
<title>The @code{break} Statement - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Statements.html#Statements" title="Statements">
<link rel="prev" href="The-_0040code_007bfor_007d-Statement.html#The-_0040code_007bfor_007d-Statement" title="The @code{for} Statement">
<link rel="next" href="The-_0040code_007bcontinue_007d-Statement.html#The-_0040code_007bcontinue_007d-Statement" title="The @code{continue} Statement">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="The-%3ccode%3ebreak%3c%2fcode%3e-Statement"></a>
<a name="The-_003ccode_003ebreak_003c_002fcode_003e-Statement"></a>
Next: <a rel="next" accesskey="n" href="The-_003ccode_003econtinue_003c_002fcode_003e-Statement.html#The-_003ccode_003econtinue_003c_002fcode_003e-Statement">The <code>continue</code> Statement</a>,
Previous: <a rel="previous" accesskey="p" href="The-_003ccode_003efor_003c_002fcode_003e-Statement.html#The-_003ccode_003efor_003c_002fcode_003e-Statement">The <code>for</code> Statement</a>,
Up: <a rel="up" accesskey="u" href="Statements.html#Statements">Statements</a>
<hr>
</div>
<h3 class="section">10.6 The <code>break</code> Statement</h3>
<p><a name="index-g_t_0040code_007bbreak_007d-statement-573"></a>
The <code>break</code> statement jumps out of the innermost <code>for</code> or
<code>while</code> loop that encloses it. The <code>break</code> statement may only
be used within the body of a loop. The following example finds the
smallest divisor of a given integer, and also identifies prime numbers:
<pre class="example"> num = 103;
div = 2;
while (div*div <= num)
if (rem (num, div) == 0)
break;
endif
div++;
endwhile
if (rem (num, div) == 0)
printf ("Smallest divisor of %d is %d\n", num, div)
else
printf ("%d is prime\n", num);
endif
</pre>
<p>When the remainder is zero in the first <code>while</code> statement, Octave
immediately <dfn>breaks out</dfn> of the loop. This means that Octave
proceeds immediately to the statement following the loop and continues
processing. (This is very different from the <code>exit</code> statement
which stops the entire Octave program.)
<p>Here is another program equivalent to the previous one. It illustrates
how the <var>condition</var> of a <code>while</code> statement could just as well
be replaced with a <code>break</code> inside an <code>if</code>:
<pre class="example"> num = 103;
div = 2;
while (1)
if (rem (num, div) == 0)
printf ("Smallest divisor of %d is %d\n", num, div);
break;
endif
div++;
if (div*div > num)
printf ("%d is prime\n", num);
break;
endif
endwhile
</pre>
</body></html>
|