Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 504

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>The break Statement - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Statements.html#Statements" title="Statements">
<link rel="prev" href="The-for-Statement.html#The-for-Statement" title="The for Statement">
<link rel="next" href="The-continue-Statement.html#The-continue-Statement" title="The 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">
<a name="The-break-Statement"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="The-continue-Statement.html#The-continue-Statement">The continue Statement</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="The-for-Statement.html#The-for-Statement">The for Statement</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Statements.html#Statements">Statements</a>
<hr>
</div>

<h3 class="section">10.6 The break Statement</h3>

<p><a name="index-g_t_0040code_007bbreak_007d-statement-707"></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 &lt;= 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 &gt; num)
         printf ("%d is prime\n", num);
         break;
       endif
     endwhile
</pre>
   </body></html>