<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/technology/cnxml/schema/dtd/0.5/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" xmlns:m="http://www.w3.org/1998/Math/MathML" id="new">
  <name>Programming with M-Files: While Loops</name>
  <metadata>
  <md:version>1.2</md:version>
  <md:created>2006/02/23 14:28:48 US/Central</md:created>
  <md:revised>2006/08/22 10:53:05.306 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>M-File</md:keyword>
    <md:keyword>MathScript</md:keyword>
    <md:keyword>MATLAB</md:keyword>
    <md:keyword>Octave</md:keyword>
    <md:keyword>While Loops</md:keyword>
  </md:keywordlist>

  <md:abstract>This is a tutorial on using while loops in m-file scripts.</md:abstract>
</metadata>
  <content>
<section id="SecW">
	  <name>The While Loop</name>
<para id="ParaW">
The <emphasis>while loop</emphasis> is similar to the for loop in that it allows the repeated execution of statements. Unlike the for loop, the number of times that the statements in the body of the loop are executed can depend on variable values that are computed in the loop.  The syntax of the while loop has the following form:
<code type="block">
while expression
    % Command 1
    % Command 2
    % More commands to execute repeatedly until expression is not true
end
</code>
where <code>expression</code> is a logical expression that is either true or false. (Information about logical expressions is available in <cnxn document="m13357">Programming with M-Files: Logical Expressions</cnxn>.)
For example, consider the following while loop:
<code type="block">
n = 1
while n &lt; 3
    n = n+1
end
</code>
This code creates the following output:
<code type="block">
n =

     1

n =

     2

n =

     3
</code>
</para>
<para id="element-194">
Note that in all of this example, the commands inside the while loop are indented relative to the <code>while</code> and <code>end</code> statements. This is not required, but is common practice and makes the code much more readable.
</para>
</section>
  </content>
  
</document>
