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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>The for Statement (GNU Octave (version 6.2.0))</title>
<meta name="description" content="The for Statement (GNU Octave (version 6.2.0))">
<meta name="keywords" content="The for Statement (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Statements.html" rel="up" title="Statements">
<link href="Looping-Over-Structure-Elements.html" rel="next" title="Looping Over Structure Elements">
<link href="The-do_002duntil-Statement.html" rel="prev" title="The do-until Statement">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<span id="The-for-Statement"></span><div class="header">
<p>
Next: <a href="The-break-Statement.html" accesskey="n" rel="next">The break Statement</a>, Previous: <a href="The-do_002duntil-Statement.html" accesskey="p" rel="prev">The do-until Statement</a>, Up: <a href="Statements.html" accesskey="u" rel="up">Statements</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="The-for-Statement-1"></span><h3 class="section">10.5 The for Statement</h3>
<span id="index-for-statement"></span>
<span id="index-endfor-statement"></span>
<p>The <code>for</code> statement makes it more convenient to count iterations of a
loop. The general form of the <code>for</code> statement looks like this:
</p>
<div class="example">
<pre class="example">for <var>var</var> = <var>expression</var>
<var>body</var>
endfor
</pre></div>
<p>where <var>body</var> stands for any statement or list of statements,
<var>expression</var> is any valid expression, and <var>var</var> may take several
forms. Usually it is a simple variable name or an indexed variable. If
the value of <var>expression</var> is a structure, <var>var</var> may also be a
vector with two elements. See <a href="Looping-Over-Structure-Elements.html">Looping Over Structure Elements</a>, below.
</p>
<p>The assignment expression in the <code>for</code> statement works a bit
differently than Octave’s normal assignment statement. Instead of
assigning the complete result of the expression, it assigns each column
of the expression to <var>var</var> in turn. If <var>expression</var> is a range,
a row vector, or a scalar, the value of <var>var</var> will be a scalar each
time the loop body is executed. If <var>var</var> is a column vector or a
matrix, <var>var</var> will be a column vector each time the loop body is
executed.
</p>
<p>The following example shows another way to create a vector containing
the first ten elements of the Fibonacci sequence, this time using the
<code>for</code> statement:
</p>
<div class="example">
<pre class="example">fib = ones (1, 10);
for i = 3:10
fib(i) = fib(i-1) + fib(i-2);
endfor
</pre></div>
<p>This code works by first evaluating the expression <code>3:10</code>, to
produce a range of values from 3 to 10 inclusive. Then the variable
<code>i</code> is assigned the first element of the range and the body of the
loop is executed once. When the end of the loop body is reached, the
next value in the range is assigned to the variable <code>i</code>, and the
loop body is executed again. This process continues until there are no
more elements to assign.
</p>
<p>Within Octave is it also possible to iterate over matrices or cell arrays
using the <code>for</code> statement. For example consider
</p>
<div class="example">
<pre class="example">disp ("Loop over a matrix")
for i = [1,3;2,4]
i
endfor
disp ("Loop over a cell array")
for i = {1,"two";"three",4}
i
endfor
</pre></div>
<p>In this case the variable <code>i</code> takes on the value of the columns of
the matrix or cell matrix. So the first loop iterates twice, producing
two column vectors <code>[1;2]</code>, followed by <code>[3;4]</code>, and likewise
for the loop over the cell array. This can be extended to loops over
multi-dimensional arrays. For example:
</p>
<div class="example">
<pre class="example">a = [1,3;2,4]; c = cat (3, a, 2*a);
for i = c
i
endfor
</pre></div>
<p>In the above case, the multi-dimensional matrix <var>c</var> is reshaped to a
two-dimensional matrix as <code>reshape (c, rows (c), prod (size (c)(2:end)))</code>
and then the same behavior as a loop over a two-dimensional matrix is produced.
</p>
<p>Although it is possible to rewrite all <code>for</code> loops as <code>while</code>
loops, the Octave language has both statements because often a
<code>for</code> loop is both less work to type and more natural to think of.
Counting the number of iterations is very common in loops and it can be
easier to think of this counting as part of looping rather than as
something to do inside the loop.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="Looping-Over-Structure-Elements.html" accesskey="1">Looping Over Structure Elements</a></td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<div class="header">
<p>
Next: <a href="The-break-Statement.html" accesskey="n" rel="next">The break Statement</a>, Previous: <a href="The-do_002duntil-Statement.html" accesskey="p" rel="prev">The do-until Statement</a>, Up: <a href="Statements.html" accesskey="u" rel="up">Statements</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|