<?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="Embedded IF commands" /> <meta name="abstract" content="An IF command can be embedded in another IF command." /> <meta name="description" content="An IF command can be embedded in another IF command." /> <meta name="DC.subject" content="embedded IF (If) command, IF (If) command, embedded, command, CL" /> <meta name="keywords" content="embedded IF (If) command, IF (If) command, embedded, command, CL" /> <meta name="DC.Relation" scheme="URI" content="contp.htm" /> <meta name="DC.Relation" scheme="URI" content="ifcmd.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="expr.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="embif" /> <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>Embedded IF commands</title> </head> <body id="embif"><a name="embif"><!-- --></a> <!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script> <h1 class="topictitle1">Embedded IF commands</h1> <div><p>An IF command can be embedded in another IF command. </p> <div class="section"><p>This would occur when the command to be processed under a true evaluation (the CL command placed on the THEN parameter) is itself another IF command:</p> <pre>IF (&A=&B) THEN(IF (&C=&D) THEN(GOTO END)) GOTO START</pre> <p>This can be useful when several conditions must be satisfied before a certain command or group of commands is run. In the preceding example, if the first expression is true, the system then reads the first THEN parameter; within that, if the <samp class="codeph">&C=&D</samp> expression is evaluated as true, the system processes the command in the second THEN parameter, GOTO END. Both expressions must be true to process the GOTO END command. If one or the other is false, the GOTO START command is run. Note the use of parentheses to organize expressions and commands.</p> <p>Up to 25 levels of such embedding are permitted in CL programming.</p> <p>As the levels of embedding increase and logic grows more complex, you may wish to enter the code in free-form design to clarify relationships: </p> <pre>PGM DCL &A *DEC 1 DCL &B *CHAR 2 DCL &RESP *DEC 1 IF (&RESP=1) + IF (&A=5) + IF (&B=NO) THEN(DO) . . . ENDDO CHGVAR &A VALUE(8) CALL PGM(DAILY) ENDPGM</pre> <p>The preceding IF series is handled as one embedded command. Whenever any one of the IF conditions fails, processing branches to the remainder of the code (<span class="cmdname">Change Variable (CHGVAR)</span> and subsequent commands). If the purpose of this code is to accumulate a series of conditions, all of which must be true for the Do group to process, it could be more easily coded using *AND with several expressions in one command.</p> <p>In some cases, however, the branch must be different depending on which condition fails. You can accomplish this by adding an ELSE command for each embedded IF command: </p> <pre>PGM DCL &A ... DCL &B ... DCL &RESP ... IF (&RESP=1) + IF (&A=5) + IF (&B=NO) THEN(DO) . . . SNDPGMMSG ... . . . ENDDO ELSE CALLPRC PROCA ELSE CALLPRC PROCB CHGVAR &A 8 CALLPRC PROC(DAILY) ENDPGM</pre> <p>Here, if all conditions are true, the <span class="cmdname">Send Program Message (SNDPGMMSG)</span> command is processed, followed by the <span class="cmdname">Change Variable (CHGVAR)</span> command. If the first and second conditions (<samp class="codeph">&RESP=1</samp> and <samp class="codeph">&A=5</samp>) are true, but the third (<samp class="codeph">&B=NO</samp>) is false, PROCA is called; when PROCA returns, the CHGVAR command is processed. If the second conditions fails, PROCB is called (<samp class="codeph">&B=NO</samp> is not tested), followed by the CHGVAR command. Finally, if &RESP does not equal 1, the CHGVAR command is immediately processed. The ELSE command has been used to provide a different branch for each test.</p> <div class="note"><span class="notetitle">Note:</span> The following three examples are correct syntactical equivalents to the embedded IF command in the preceding example:</div> <pre>IF (&RESP=1) THEN(IF (&A=5) THEN(IF (&B=NO) THEN(DO))) IF (&RESP=1) THEN + (IF (&A=5) THEN + (IF (&B=NO) THEN(DO))) IF (&RESP=1) + (IF (&A=5) + (IF (&B=NO) THEN(DO)))</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="ifcmd.htm" title="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.">IF command</a></div> <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> <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>