Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 512

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

<html lang="en">
<head>
<title>The while 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-switch-Statement.html#The-switch-Statement" title="The switch Statement">
<link rel="next" href="The-do_002duntil-Statement.html#The-do_002duntil-Statement" title="The do-until 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-while-Statement"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="The-do_002duntil-Statement.html#The-do_002duntil-Statement">The do-until Statement</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="The-switch-Statement.html#The-switch-Statement">The switch Statement</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Statements.html#Statements">Statements</a>
<hr>
</div>

<h3 class="section">10.3 The while Statement</h3>

<p><a name="index-g_t_0040code_007bwhile_007d-statement-698"></a><a name="index-g_t_0040code_007bendwhile_007d-statement-699"></a><a name="index-loop-700"></a><a name="index-body-of-a-loop-701"></a>
In programming, a <dfn>loop</dfn> means a part of a program that is (or at least can
be) executed two or more times in succession.

   <p>The <code>while</code> statement is the simplest looping statement in Octave. 
It repeatedly executes a statement as long as a condition is true.  As
with the condition in an <code>if</code> statement, the condition in a
<code>while</code> statement is considered true if its value is non-zero, and
false if its value is zero.  If the value of the conditional expression
in a <code>while</code> statement is a vector or a matrix, it is considered
true only if it is non-empty and <em>all</em> of the elements are non-zero.

   <p>Octave's <code>while</code> statement looks like this:

<pre class="example">     while (<var>condition</var>)
       <var>body</var>
     endwhile
</pre>
   <p class="noindent">Here <var>body</var> is a statement or list of statements that we call the
<dfn>body</dfn> of the loop, and <var>condition</var> is an expression that
controls how long the loop keeps running.

   <p>The first thing the <code>while</code> statement does is test <var>condition</var>. 
If <var>condition</var> is true, it executes the statement <var>body</var>.  After
<var>body</var> has been executed, <var>condition</var> is tested again, and if it
is still true, <var>body</var> is executed again.  This process repeats until
<var>condition</var> is no longer true.  If <var>condition</var> is initially
false, the body of the loop is never executed.

   <p>This example creates a variable <code>fib</code> that contains the first ten
elements of the Fibonacci sequence.

<pre class="example">     fib = ones (1, 10);
     i = 3;
     while (i &lt;= 10)
       fib (i) = fib (i-1) + fib (i-2);
       i++;
     endwhile
</pre>
   <p class="noindent">Here the body of the loop contains two statements.

   <p>The loop works like this: first, the value of <code>i</code> is set to 3. 
Then, the <code>while</code> tests whether <code>i</code> is less than or equal to
10.  This is the case when <code>i</code> equals 3, so the value of the
<code>i</code>-th element of <code>fib</code> is set to the sum of the previous two
values in the sequence.  Then the <code>i++</code> increments the value of
<code>i</code> and the loop repeats.  The loop terminates when <code>i</code>
reaches 11.

   <p>A newline is not required between the condition and the
body; but using one makes the program clearer unless the body is very
simple.

   </body></html>