ibm-information-center/dist/eclipse/plugins/i5OS.ic.rbam6_5.4.0.1/ifcmd.htm

144 lines
7.9 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="IF command" />
<meta name="abstract" content="The IF command is used to state a condition that, if true, specifies some statement or group of statements in the procedure to be run." />
<meta name="description" content="The IF command is used to state a condition that, if true, specifies some statement or group of statements in the procedure to be run." />
<meta name="DC.subject" content="command, CL, IF (If) command, example" />
<meta name="keywords" content="command, CL, IF (If) command, example" />
<meta name="DC.Relation" scheme="URI" content="contp.htm" />
<meta name="DC.Relation" scheme="URI" content="expr.htm" />
<meta name="DC.Relation" scheme="URI" content="../clfinder/finder.htm" />
<meta name="DC.Relation" scheme="URI" content="../cl/if.htm" />
<meta name="DC.Relation" scheme="URI" content="docmd.htm" />
<meta name="DC.Relation" scheme="URI" content="elsec.htm" />
<meta name="DC.Relation" scheme="URI" content="embif.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="ifcmd" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>IF command</title>
</head>
<body id="ifcmd"><a name="ifcmd"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">IF command</h1>
<div><p>The IF command is used to state a condition that, if true, specifies
some statement or group of statements in the procedure to be run. </p>
<div class="section"><p> The ELSE command can be used with the IF command to specify a
statement or group of statements to be run if the condition expressed by the
IF command is false.</p>
<p>The command includes an expression, which is tested
(true or false), and a THEN parameter that specifies the action to be taken
if the expression is true. The IF command is formatted as follows:</p>
<pre>IF COND(logical-expression) THEN(CL-command)</pre>
<p>The logical expression on the COND parameter may be a single
logical variable or constant, or it must describe a relationship between two
or more operands; the expression is then evaluated as true or false.</p>
<p>If
the condition described by the logical expression is evaluated as true, the
procedure processes the CL command on the THEN parameter. This may be a single
command, or a group of commands. If the condition is not true, the procedure
runs the next sequential command.</p>
<p>Both COND and THEN are keywords on
the command, and they can be omitted for positional entry. The following
are syntactically correct uses of this command:</p>
<pre>IF COND(&amp;RESP=1) THEN(CALL CUS210)
IF (&amp;A *EQ &amp;B) THEN(GOTO LABEL)
IF (&amp;A=&amp;B) GOTO LABEL</pre>
<p>Blanks are required between the command name (IF) and the keyword
(COND) or value (&amp;A). No blanks are permitted between the keyword, if
specified, and the left parenthesis enclosing the value.</p>
<p>The following
is an example of conditional processing with an IF command. Processing branches
in different ways depending on the evaluation of the logical expression in
the IF commands. Assume, for instance, that at the start of the following
code, the value of &amp;A is 2 and the value of &amp;C is 4. </p>
<pre> IF (&amp;A=2) THEN(GOTO FINAL)
IF (&amp;A=3) THEN(CHGVAR &amp;C 5)
.
.
.
FINAL: IF (&amp;C=5) CALL PROGA
ENDPGM</pre>
<p>In this case, the procedure processes the first IF command
before branching to FINAL, skipping the intermediate code. It does not return
to the second IF command. At FINAL, because the test for <samp class="codeph">&amp;C=5</samp> fails,
PROGA is not called. The procedure then processes the next command, ENDPGM,
which signals the end of the procedure, and returns control to the calling
procedure.</p>
<p>Processing logic would be different if, using the same code,
the initial values of the variables were different. For instance, if at the
beginning of this code the value of &amp;A is 3 and the value of &amp;C is
4, the first IF statement is evaluated as false. Instead of processing the
GOTO FINAL command, the procedure ignores the first IF statement and moves
on to the next one. The second IF statement is evaluated as true, and the
value of &amp;C is changed to 5. Subsequent statements, not shown here, are
also processed consecutively. When processing reaches the last IF statement,
the condition <samp class="codeph">&amp;C=5</samp> is evaluated as true, and PROGA is
called.</p>
<p>A series of consecutive IF statements are run independently.
For instance:</p>
<pre> PGM /* IFFY */
DCL &amp;A..
DCL &amp;B..
DCL &amp;C..
DCL &amp;D..
DCL &amp;AREA *CHAR LEN(5) VALUE(YESNO)
DCL &amp;RESP..
IF (&amp;A=&amp;B) THEN(GOTO END) /* IF #1 */
IF (&amp;C=&amp;D) THEN(CALL PGMA) /* IF #2 */
IF (&amp;RESP=1) THEN(CHGVAR &amp;C 2) /* IF #3 */
IF (%SUBSTRING(&amp;AREA 1 3) *EQ YES) THEN(CALL PGMB) /* IF #4 */
CHGVAR &amp;B &amp;C
.
.
.
END: ENDPGM</pre>
<p>If, in this example, &amp;A is not equal to &amp;B, the next
statement is run. If &amp;C is equal to &amp;D, PGMA is called. When PGMA
returns, the third IF statement is considered, and so on. An embedded command
is a command that is completely enclosed in the parameter of another command.</p>
<p>In
the following examples, the <span class="cmdname">Change Variable (CHGVAR)</span> command
and the DO command are embedded:</p>
<pre>IF (&amp;A *EQ &amp;B) THEN(CHGVAR &amp;A (&amp;A+1))
IF (&amp;B *EQ &amp;C) THEN(DO)
.
.
.
ENDDO</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="contp.htm" title="You can use commands to change the flow of logic within your CL procedure.">Control processing within a CL procedure</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="expr.htm" title="The logical operators *AND and *OR specify the relationship between operands in a logical expression. The logical operator *NOT is used to negate logical variables or constants.">*AND, *OR, and *NOT operators</a></div>
<div><a href="docmd.htm" title="The DO command allows you to process a group of commands together.">DO command and DO groups</a></div>
<div><a href="elsec.htm" title="The ELSE command is a way of specifying alternative processing if the condition on the associated IF command is false.">ELSE command</a></div>
<div><a href="embif.htm" title="An IF command can be embedded in another IF command.">Embedded IF commands</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../clfinder/finder.htm">CL command finder</a></div>
<div><a href="../cl/if.htm">If (IF) command</a></div>
</div>
</div>
</body>
</html>