<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="None">
  <name>For Loops</name>
  <metadata>
  <md:version>1.1</md:version>
  <md:created>2004/06/17 15:40:39 GMT-5</md:created>
  <md:revised>2004/07/16 15:04:32.757 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="NationalInstruments">
      <md:firstname>National</md:firstname>
      
      <md:surname>Instruments</md:surname>
      <md:email>certification@ni.com</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="lizzardg">
      <md:firstname>Elizabeth</md:firstname>
      
      <md:surname>Gregory</md:surname>
      <md:email>lizzardg@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="mshiralkar05">
      <md:firstname>Malan</md:firstname>
      
      <md:surname>Shiralkar</md:surname>
      <md:email>mshiralkar05@hotmail.com</md:email>
    </md:maintainer>
    <md:maintainer id="harika">
      <md:firstname>Harika</md:firstname>
      
      <md:surname>Basana</md:surname>
      <md:email>ilsai@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="NationalInstruments">
      <md:firstname>National</md:firstname>
      
      <md:surname>Instruments</md:surname>
      <md:email>certification@ni.com</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  

  <md:abstract/>
</metadata>

  <content>
    <para id="intro">
      A <code>For Loop</code> executes a subdiagram a set number of
      times. <cnxn target="shrek2"/> shows a <code>For Loop</code> in
      LabVIEW, a flow chart equivalent of the <code>For Loop</code>
      functionality, and a pseudo code example of the functionality of
      the <code>For Loop</code>.
      <figure id="shrek2">
	<media type="image/src" src="ForLoopTheory.png"/>
      </figure>
    </para>   

    <para id="intro1">
      <media id="m1" type="image/png" src="count.png"/> 

      The <code>For Loop</code> is located on the <code>
	Functions&gt;&gt;All Functions&gt;&gt;Structures </code> palette. You also
      can place a <code>While Loop</code> on the block diagram,
      right-click the border of the <code>While Loop</code>, and
      select <code>Replace with For Loop</code> from the shortcut menu
      to change a <code>While Loop</code> to a <code>For
	Loop</code>. The value in the <code>count</code> terminal (an
      input terminal), shown in <cnxn target="m1"/>, indicates how
      many times to repeat the subdiagram.
    </para>
    
    <para id="intro2">
      <media id="m2" type="image/png" src="iteration.png"/> 

      The <code>iteration</code> terminal (an output terminal), shown
      in <cnxn target="m2"/>, contains the number of completed
      iterations. The iteration count always starts at zero. During
      the first iteration, the iteration terminal returns
      <code>0</code>.
    </para>
    
    <para id="intro3">
      The <code>For Loop</code> differs from the <code>While
	Loop</code> in that the <code>For Loop</code> executes a set
      number of times. A <code>While Loop</code> stops executing the
      subdiagram only if the value at the conditional terminal exists.
    </para>

    <para id="intro4">
      The <code>For Loop</code> in <cnxn target="troy"/> generates a
      random number every second for <m:math><m:cn>100</m:cn></m:math>
      seconds and displays the random numbers in a numeric indicator.
      <figure id="troy">
	<media type="image/src" src="forloopex.png"/>
      </figure>
    </para>

    <section>
      <name>Wait Functions</name>

      <para id="ahjh">
	<media id="m3" type="image/png" src="waitms.png"/> 
	The <code>Wait Until Next ms Multiple</code> function, shown
	in <cnxn target="m3"/>, monitors a millisecond counter and
	waits until the millisecond counter reaches a multiple of the
	amount you specify. Use this function to synchronize
	activities. Place this function within a loop to control the
	loop execution rate.
      </para>

      <para id="ah">
	<media id="m4" type="image/png" src="wait.png"/>
	The <code>Wait (ms)</code> function, shown in <cnxn target="m4"/>, adds the wait time to the code execution
	time. This can cause a problem if code execution time is
	variable.

	<note>
	  The <code>Time Delay</code> Express VI, located on the
	  <code>Functions&gt;&gt;Execution Control</code> palette, behaves
	  similar to the <code>Wait (ms)</code> function with the
	  addition of built-in error clusters. Refer to <cnxn document="m12231">Clusters</cnxn> for more information about error
	  clusters.
	</note>
      </para>
    </section>

    <section>
      <name>Wait Functions</name>
      <para id="ah1">
	LabVIEW can represent numeric data types as signed or unsigned
	integers (8-bit, 16-bit, or 32-bit), floating-point numeric
	values (single-, double-, or extended-precision), or complex
	numeric values (single-, double-, or extended-precision). When
	you wire two or more numeric inputs of different
	representations to a function, the function usually returns
	output in the larger or wider format. The functions coerce the
	smaller representations to the widest representation before
	execution, and LabVIEW places a coercion dot on the terminal
	where the conversion takes place.
      </para>

      <para id="ah2">
	For example, the <code>For Loop</code> count terminal is a
	32-bit signed integer. If you wire a double-precision,
	floating-point numeric to the count terminal, LabVIEW converts
	the numeric to a 32-bit signed integer. A coercion dot appears
	on the count terminal of the first <code>For Loop</code>, as
	shown in <cnxn target="saved"/>.
	<figure id="saved">
	  <media type="image/src" src="coercion.png"/>
	</figure>
      </para>

      <para id="ah3">
	If you wire two different numeric data types to a numeric
	function that expects the inputs to be the same data type,
	LabVIEW converts one of the terminals to the same
	representation as the other terminal. LabVIEW chooses the
	representation that uses more bits. If the number of bits is
	the same, LabVIEW chooses unsigned over signed.
      </para>

      <para id="ah4">
	In the example in <cnxn target="brucealmgty"/>, a 32-bit
	signed integer (I32) and a double-precision, floating-point
	numeric value (DBL) are wired to the <code>Divide</code>
	function. The 32-bit signed integer is coerced since it uses
	fewer bits than the double-precision, floating-point numeric
	value.
	<figure id="brucealmgty">
	  <media type="image/src" src="funcconvert.png"/>
	</figure>
      </para>

      <para id="ah5">
	To change the representation of a numeric object, right-click
	the object and select <code>Representation</code> from the
	shortcut menu. Select the data type that best represents the
	<code>data.ut</code> data types.
      </para>

      <para id="ah6">
	When LabVIEW converts double-precision, floating-point numeric
	values to integers, it rounds to the nearest integer. LabVIEW
	rounds <code>x.5</code> to the nearest even integer. For
	example, LabVIEW rounds <m:math><m:cn>2.5</m:cn></m:math> to
	<m:math><m:cn>2</m:cn></m:math> and
	<m:math><m:cn>3.5</m:cn></m:math> to
	<m:math><m:cn>4</m:cn></m:math>.
      </para>

      <para id="ah7">
	Refer to the Data Types section of
	Introduction to LabVIEW, of this
	manual or to the LabVIEW Help for more information about data
	types.
      </para>
    </section>
  </content>
</document>
