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 84 85 86 87 88 89 90 91 92
|
<html lang="en">
<head>
<title>The @code{while} 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_007bswitch_007d-Statement.html#The-_0040code_007bswitch_007d-Statement" title="The @code{switch} Statement">
<link rel="next" href="The-_0040code_007bdo_002duntil_007d-Statement.html#The-_0040code_007bdo_002duntil_007d-Statement" title="The @code{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">
<p>
<a name="The-%3ccode%3ewhile%3c%2fcode%3e-Statement"></a>
<a name="The-_003ccode_003ewhile_003c_002fcode_003e-Statement"></a>
Next: <a rel="next" accesskey="n" href="The-_003ccode_003edo_002duntil_003c_002fcode_003e-Statement.html#The-_003ccode_003edo_002duntil_003c_002fcode_003e-Statement">The <code>do-until</code> Statement</a>,
Previous: <a rel="previous" accesskey="p" href="The-_003ccode_003eswitch_003c_002fcode_003e-Statement.html#The-_003ccode_003eswitch_003c_002fcode_003e-Statement">The <code>switch</code> Statement</a>,
Up: <a rel="up" accesskey="u" href="Statements.html#Statements">Statements</a>
<hr>
</div>
<h3 class="section">10.3 The <code>while</code> Statement</h3>
<p><a name="index-g_t_0040code_007bwhile_007d-statement-564"></a><a name="index-g_t_0040code_007bendwhile_007d-statement-565"></a><a name="index-loop-566"></a><a name="index-body-of-a-loop-567"></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 <= 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>
|