ibm-information-center/dist/eclipse/plugins/i5OS.ic.cl_5.4.0.1/dofor.htm

219 lines
9.5 KiB
HTML

<!doctype html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Do For (DOFOR)</title>
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body bgcolor="white">
<script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<a name="DOFOR.Top_Of_Page"></a>
<h2>Do For (DOFOR)</h2>
<table width="100%">
<tr>
<td valign="top" align="left"><b>Where allowed to run: </b>
<ul><li>Batch program (*BPGM)</li>
<li>Interactive program (*IPGM)</li>
</ul><b>Threadsafe: </b>Yes
</td>
<td valign="top" align="right">
<a href="#DOFOR.PARAMETERS.TABLE">Parameters</a><br>
<a href="#DOFOR.COMMAND.EXAMPLES">Examples</a><br>
<a href="#DOFOR.ERROR.MESSAGES">Error messages</a></td>
</tr>
</table>
<div> <a name="DOFOR"></a>
<p>The Do For (DOFOR) command processes a group of CL commands zero or more times based on the values specified for the command.
</p>
<p>The loop control CL variable (VAR parameter) is set to the initial value (FROM parameter) and compared to the loop termination value (TO parameter). If the loop increment value (BY parameter) is positive or zero and the control variable is less than or equal the termination value, the commands between the DOFOR and matching ENDDO command are processed. If the loop increment value is negative and the control variable is greater than or equal the termination value, the commands between the DOFOR and matching ENDDO command are processed.
</p>
<p>When control reaches the ENDDO command, the loop control variable is adjusted by the loop increment value and compared to the loop termination value. If the control variable is greater than the termination value (if BY is positive or zero) or less than the termination value (if BY is negative), control goes to the command following the ENDDO command. Otherwise, control goes to the first command following the DOFOR statement (the top of the loop).
</p>
<p><b>Restrictions:</b>
</p>
<ul>
<li>This command is valid only in CL procedures.
</li>
<li>Up to 25 levels of nested DO, DOWHILE, DOUNTIL, and DOFOR commands are allowed.
</li>
</ul>
</div>
<table width="100%">
<tr><td align="right"><a href="#DOFOR.Top_Of_Page">Top</a></td></tr>
</table>
<hr size="2" width="100%">
<div>
<h3><a name="DOFOR.PARAMETERS.TABLE">Parameters</a></h3>
<table border="1" cellpadding="4" cellspacing="0">
<!-- col1="10" col2="15" col3="30" col4="10" -->
<tr>
<th bgcolor="aqua" valign="bottom" align="left">Keyword</th>
<th bgcolor="aqua" valign="bottom" align="left">Description</th>
<th bgcolor="aqua" valign="bottom" align="left">Choices</th>
<th bgcolor="aqua" valign="bottom" align="left">Notes</th>
</tr>
<tr>
<td valign="top"><a href="#DOFOR.VAR"><b>VAR</b></a></td>
<td valign="top">CL variable name</td>
<td valign="top"><i>CL variable name</i></td>
<td valign="top">Required, Positional 1</td>
</tr>
<tr>
<td valign="top"><a href="#DOFOR.FROM"><b>FROM</b></a></td>
<td valign="top">From value</td>
<td valign="top"><i>Integer</i></td>
<td valign="top">Required, Positional 2</td>
</tr>
<tr>
<td valign="top"><a href="#DOFOR.TO"><b>TO</b></a></td>
<td valign="top">To value</td>
<td valign="top"><i>Integer</i></td>
<td valign="top">Required, Positional 3</td>
</tr>
<tr>
<td valign="top"><a href="#DOFOR.BY"><b>BY</b></a></td>
<td valign="top">By value</td>
<td valign="top"><i>Integer</i>, <b><u>1</u></b></td>
<td valign="top">Optional</td>
</tr>
</table>
<table width="100%">
<tr><td align="right"><a href="#DOFOR.Top_Of_Page">Top</a></td></tr>
</table>
</div>
<div> <a name="DOFOR.VAR"></a>
<h3>CL variable name (VAR)</h3>
<p>Specifies the CL variable used to control the DOFOR loop. The variable must be of type *INT or *UINT. The name must start with an ampersand (&amp;).
</p>
<p>This is a required parameter.
</p>
<dl>
<dt><b><i>CL-integer-variable-name</i></b></dt>
<dd>Specify the name of an integer variable to be used as the loop control.
</dd>
</dl>
</div>
<table width="100%">
<tr><td align="right"><a href="#DOFOR.Top_Of_Page">Top</a></td></tr>
</table>
<div> <a name="DOFOR.FROM"></a>
<h3>From value (FROM)</h3>
<p>Specifies the initial value of the CL variable used to control the DOFOR loop. The value must be specified as an integer constant, a CL variable declared as type *INT or *UINT, or an expression which results in an integer value. The initial value is assigned to the loop control CL variable (VAR parameter) only once, prior to processing the group of CL commands between the DOFOR command and the corresponding ENDDO command.
</p>
<p>This is a required parameter.
</p>
<dl>
<dt><b><i>integer</i></b></dt>
<dd>Specify the constant integer value for initializing the VAR parameter.
</dd>
<dt><b><i>CL-integer-variable-name</i></b></dt>
<dd>Specify the name of an integer variable to be used as initial value for the loop.
</dd>
<dt><b><i>integer-expression</i></b></dt>
<dd>Specify an expression whose result will be treated as an integer value.
</dd>
</dl>
</div>
<table width="100%">
<tr><td align="right"><a href="#DOFOR.Top_Of_Page">Top</a></td></tr>
</table>
<div> <a name="DOFOR.TO"></a>
<h3>To value (TO)</h3>
<p>Specifies the final value to compare to the control variable (VAR parameter) to control the DOFOR loop. The value must be specified as an integer constant, a CL variable declared as type *INT or *UINT, or an expression which results in an integer value. The loop control CL variable (VAR parameter) will be compared to this final value before processing the group of CL commands between the DOFOR and corresponding ENDDO statement, and after each loop iteration.
</p>
<ul>
<li>If the BY parameter value is negative, the loop ends when the loop control variable is less than the TO value.
</li>
<li>If the BY parameter value is positive (or zero), the loop ends when the loop control variable is greater than the TO value.
</li>
</ul>
<p>This is a required parameter.
</p>
<dl>
<dt><b><i>integer</i></b></dt>
<dd>Specify the constant value to be used to be used as the terminating value for the loop.
</dd>
<dt><b><i>CL-integer-variable-name</i></b></dt>
<dd>Specify the name of an integer variable to be used as the terminating value for the loop.
</dd>
<dt><b><i>integer-expression</i></b></dt>
<dd>Specify an expression whose result will be treated as an integer value.
</dd>
</dl>
</div>
<table width="100%">
<tr><td align="right"><a href="#DOFOR.Top_Of_Page">Top</a></td></tr>
</table>
<div> <a name="DOFOR.BY"></a>
<h3>By value (BY)</h3>
<p>Specifies the amount to add to the loop control variable (VAR parameter) after each iteration of the loop. The value must be specified as an integer constant; the value can be positive or negative or zero.
</p>
<dl>
<dt><b><u>1</u></b></dt>
<dd>Increments the control variable specified for the <b>CL variable name (VAR)</b> parameter by 1 each time through the loop.
</dd>
<dt><b><i>integer</i></b></dt>
<dd>Specify the constant value to be added to the control variable specified for the VAR parameter.
</dd>
</dl>
</div>
<table width="100%">
<tr><td align="right"><a href="#DOFOR.Top_Of_Page">Top</a></td></tr>
</table>
<hr size="2" width="100%">
<div><h3><a name="DOFOR.COMMAND.EXAMPLES">Examples</a> </h3>
<p><b>Example 1: DOFOR Command Group Fixed Number of Times</b>
</p>
<p>
<pre>
DCL VAR(&amp;INT) TYPE(*INT) LEN(2)
:
DOFOR VAR(&amp;INT) FROM(1) TO(10)
: (group of CL commands)
ENDDO
</pre>
</p>
<p>The group of commands between the DOFOR and ENDDO will be processed 10 times. CL variable &amp;INT will be set to the initial value of 1 and compared to the loop termination value of 10. After each loop iteration, &amp;INT will be incremented by 1 (the default for the BY parameter). After the tenth loop iteration, &amp;INT will have a value of 11 and control will go the command that follows the ENDDO statement.
</p>
<p>
<b>Note: </b>If the value of CL variable &amp;INT is changed within the group of CL commands in the DOFOR loop, the loop could be processed more or less than 10 times.
</p>
<p><b>Example 2: DOFOR Using Variables for FROM and TO</b>
</p>
<p>
<pre>
DCL VAR(&amp;INT) TYPE(*INT) LEN(2)
DCL VAR(&amp;START) TYPE(*INT) LEN(2)
DCL VAR(&amp;END) TYPE(*INT) LEN(2)
:
CHGVAR VAR(&amp;START) VALUE(100)
CHGVAR VAR(&amp;END) VALUE(0)
:
DOFOR VAR(&amp;INT) FROM(&amp;START) TO(&amp;END) BY(-5)
: (group of CL commands)
ENDDO
</pre>
</p>
<p>The group of commands between the DOFOR and ENDDO will be processed 21 times. CL variable &amp;INT will be set to the initial value of 100 and compared to the loop termination value of 0. Because the increment value is negative, the loop is processed until &amp;INT is less than 0. After each loop iteration, &amp;INT will be decremented by 5 and compared to the TO value. After the twenty-first loop iteration, &amp;INT will have a value of -5 and control will go the command that follows the ENDDO statement.
</p>
<p>
<b>Note: </b>If the values of CL variables &amp;INT or &amp;END are changed within the group of CL commands in the DOFOR loop, the loop could be processed more or less than 21 times. Changing the value of CL variable &amp;START inside the loop will not affect the loop behavior since &amp;START is only used to set the loop control variable (&amp;INT) prior to the first loop iteration.
</p>
</div>
<table width="100%">
<tr><td align="right"><a href="#DOFOR.Top_Of_Page">Top</a></td></tr>
</table>
<hr size="2" width="100%">
<div><h3><a name="DOFOR.ERROR.MESSAGES">Error messages</a> </h3>
<p>None
</p>
</div>
<table width="100%">
<tr><td align="right"><a href="#DOFOR.Top_Of_Page">Top</a></td></tr>
</table>
</body>
</html>