Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 3e60ff9d4d6f58c8fbd17208f14089fa > files > 420

octave-doc-3.2.3-3mdv2010.0.i586.rpm

<html lang="en">
<head>
<title>The @code{for} Statement - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<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-_0040code_007bdo_002duntil_007d-Statement.html#The-_0040code_007bdo_002duntil_007d-Statement" title="The @code{do-until} Statement">
<link rel="next" href="The-_0040code_007bbreak_007d-Statement.html#The-_0040code_007bbreak_007d-Statement" title="The @code{break} 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-%3ccode%3efor%3c%2fcode%3e-Statement"></a>
<a name="The-_003ccode_003efor_003c_002fcode_003e-Statement"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="The-_003ccode_003ebreak_003c_002fcode_003e-Statement.html#The-_003ccode_003ebreak_003c_002fcode_003e-Statement">The &lt;code&gt;break&lt;/code&gt; Statement</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="The-_003ccode_003edo_002duntil_003c_002fcode_003e-Statement.html#The-_003ccode_003edo_002duntil_003c_002fcode_003e-Statement">The &lt;code&gt;do-until&lt;/code&gt; Statement</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Statements.html#Statements">Statements</a>
<hr>
</div>

<h3 class="section">10.5 The <code>for</code> Statement</h3>

<p><a name="index-g_t_0040code_007bfor_007d-statement-569"></a><a name="index-g_t_0040code_007bendfor_007d-statement-570"></a>
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:

<pre class="example">     for <var>var</var> = <var>expression</var>
       <var>body</var>
     endfor
</pre>
   <p class="noindent">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">Looping Over Structure Elements</a>, below.

   <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>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:

<pre class="example">     fib = ones (1, 10);
     for i = 3:10
       fib (i) = fib (i-1) + fib (i-2);
     endfor
</pre>
   <p class="noindent">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>Within Octave is it also possible to iterate over matrices or cell arrays
using the <code>for</code> statement.  For example consider

<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>
   <p class="noindent">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
multidimensional arrays.  For example

<pre class="example">     a = [1,3;2,4]; b = cat(3, a, 2*a);
     for i = c
       i
     endfor
</pre>
   <p class="noindent">In the above case, the multidimensional 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>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.

<ul class="menu">
<li><a accesskey="1" href="Looping-Over-Structure-Elements.html#Looping-Over-Structure-Elements">Looping Over Structure Elements</a>
</ul>

   </body></html>