<?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: If Statements</name>
  <metadata>
  <md:version>1.3</md:version>
  <md:created>2006/02/10 15:14:38 US/Central</md:created>
  <md:revised>2006/08/22 10:30:49.879 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>if statement</md:keyword>
    <md:keyword>M-File</md:keyword>
    <md:keyword>MathScript</md:keyword>
    <md:keyword>MATLAB</md:keyword>
    <md:keyword>Octave</md:keyword>
  </md:keywordlist>

  <md:abstract>This is a tutorial on using if statements in m-file scripts.</md:abstract>
</metadata>
  <content>
<section id="SecIf">
	  <name>The If Statement</name>
<para id="ParaIf">The <emphasis>if statement</emphasis> is one way to make  the sequence of computations executed by in an m-file script  depend on variable values. The if statement has  several different forms. The simplest form is
<code type="block">
if expression
    % Commands to execute if expression is 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, the following if statement will print "v is negative"  if the variable <code>v</code> is in fact negative:
<code type="block">
if v &lt; 0
    disp('v is negative')
end
</code>
</para>
<para id="element-199">
A more complicated form of the if statement is
<code type="block">
if expression
    % Commands to execute if expression is true
else
    % Commands to execute if expression is false
end
</code>
For example, the following if statement will print "v is negative"  if the variable <code>v</code> is negative and "v is not negative"  if <code>v</code> is not negative:
<code type="block">
if v &lt; 0
    disp('v is negative')
else
    disp('v is not negative')
end
</code>
</para>
<para id="element-101">
The most general form of the if statement is
<code type="block">
if expression1
    % Commands to execute if expression1 is true
elseif expression2
    % Commands to execute if expression2 is true
elseif expression3
    % Commands to execute if expression3 is true
    ...
else
    % Commands to execute if all expressions are false
end
</code>
The following if statement  is an example of this most general statement:
<code type="block">
if v &lt; 0
    disp('v is negative')
elseif v &gt; 0
    disp('v is positive')
else
    disp('v is zero')
end
</code>
</para>

<para id="element-194">Note that in all of the examples in this module, the commands inside the if statement are indented relative to the <code>if</code>, <code>else</code>, <code>elseif</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>
