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

244 lines
13 KiB
HTML

<?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="*AND, *OR, and *NOT operators" />
<meta name="abstract" content="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." />
<meta name="description" content="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." />
<meta name="DC.subject" content="*AND operator, *OR operator, *NOT operator, operator, logical, relational expression, calculation, expression, relational, logical expression, arithmetic, performing, character, example, logical expression, parameter, RTNCDE (return code), RTNCDE (return code) parameter, return code (RTNCDE) parameter, return code, summary, return code, BASIC program, RPG IV program, PL/I program, CL procedure, Pascal program" />
<meta name="keywords" content="*AND operator, *OR operator, *NOT operator, operator, logical, relational expression, calculation, expression, relational, logical expression, arithmetic, performing, character, example, logical expression, parameter, RTNCDE (return code), RTNCDE (return code) parameter, return code (RTNCDE) parameter, return code, summary, return code, BASIC program, RPG IV program, PL/I program, CL procedure, Pascal program" />
<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="elsec.htm" />
<meta name="DC.Relation" scheme="URI" content="embif.htm" />
<meta name="DC.Relation" scheme="URI" content="dountilcmd.htm" />
<meta name="DC.Relation" scheme="URI" content="dowhilecmd.htm" />
<meta name="DC.Relation" scheme="URI" content="selectcmd.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="expr" />
<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>*AND, *OR, and *NOT operators</title>
</head>
<body id="expr"><a name="expr"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">*AND, *OR, and *NOT operators</h1>
<div><p>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.</p>
<div class="section"><p>*AND and *OR are the reserved values used to specify the relationship
between operands in a logical expression. The ampersand symbol (&amp;) can
replace the reserved value *AND, and the vertical bar (|) can replace *OR.
The reserved values must be preceded and followed by blanks. The operands
in a logical expression consist of relational expressions or logical variables
or constants separated by logical operators. The *AND operator indicates that
both operands (on either side of the operator) have to be true to produce
a true result. The *OR operator indicates that one or the other of its operands
must be true to produce a true result. </p>
<div class="note"><span class="notetitle">Note:</span> Using the ampersand symbol
or the vertical bar can cause problems because the symbols are not at the
same code point for all code pages. To avoid this, use *AND and *OR instead
of the symbols.</div>
<p>Use operators other than logical operators in expressions
to indicate the actions to perform on the operands in the expression or the
relationship between the operands. There are three kinds of operators other
than logical operators: </p>
<ul><li>Arithmetic (+, -, *, /)</li>
<li>Character (*CAT, ||, *BCAT, |&gt;, *TCAT, |&lt;)</li>
<li>Relational (*EQ, =, *GT, &gt;, *LT, &lt;, *GE, &gt;=, *LE, &lt;=, *NE,
¬=, *NG, ¬&gt;, *NL, ¬&lt;)</li>
</ul>
<p>The following are examples of logical expressions: </p>
<pre>((&amp;C *LT 1) *AND (&amp;TIME *GT 1430))
(&amp;C *LT 1 *AND &amp;TIME *GT 1430)
((&amp;C &lt; 1) &amp; (&amp;TIME&gt;1430))
((&amp;C&lt; 1) &amp; (&amp;TIME&gt;1430))</pre>
<p>In each of these cases, the logical expression consists of
three parts: two operands and one operator (*AND or *OR, or their symbols).
It is the type of operator (*AND or *OR) that characterizes the expression
as logical, not the type of operand. Operands in logical expressions can
be logical variables or other expressions, such as relational expressions.
(Relational expressions are characterized by &amp;gt, &amp;lt, or = symbols
or corresponding reserved values.) For instance, in following example the
entire logical expression is enclosed in parentheses, and both operands are
relational expressions, also enclosed separately in parentheses:</p>
<pre>((&amp;C *LT 1) *AND (&amp;TIME *GT 1430))</pre>
<p>As you can see from the second example of logical expressions,
the operands need not be enclosed in separate parentheses, but it is recommended
for clarity. Parentheses are not needed because *AND and *OR have different
priorities. *AND is always considered before *OR. For operators of the same
priority, parentheses can be used to control the order in which operations
are performed.</p>
<p>A simple relational expression can be written as the
condition in a command:</p>
<pre>IF (&amp;A=&amp;B) THEN(DO)
.
.
.
ENDDO</pre>
<p>The operands in this relational expression could also be constants.</p>
<p>If
you wish to specify more than one condition, you can use a logical expression
with relational expressions as operands:</p>
<pre>IF ((&amp;A=&amp;B) *AND (&amp;C=&amp;D)) THEN(DO)
.
.
.
ENDDO</pre>
<p>The series of dependent IF commands cited as an example in <a href="embif.htm#embif">Embedded IF commands</a> could be coded:</p>
<pre>PGM
DCL &amp;RESP *DEC 1
DCL &amp;A *DEC 1
DCL &amp;B *CHAR 2
IF ((&amp;RESP=1) *AND (&amp;A=5) *AND (&amp;B=NO)) THEN(DO)
.
.
.
ENDDO
CHGVAR &amp;A VALUE(8)
CALLPRC PROC(DAILY)
ENDPGM</pre>
<p>Here the logical operators are again used between relational
expressions.</p>
<p>Because a logical expression can also have other logical
expressions as operands, quite complex logic is possible:</p>
<pre>IF (((&amp;A=&amp;B) *OR (&amp;A=&amp;C)) *AND ((&amp;C=1) *OR (&amp;D='0'))) THEN(DO)</pre>
<p>In this case, &amp;D is defined as a logical variable.</p>
<p>The
result of the evaluation of any relational or logical expression is a '1'
or '0' (true or false). The dependent command is processed only if the complete
expression is evaluated as true ('1'). The following command is interpreted
in these terms: </p>
<pre>IF ((&amp;A = &amp;B) *AND (&amp;C = &amp;D)) THEN(DO)
((<em>true'1'</em>) *AND (<em>not true'0'</em>))
(<em>not true '0'</em>)</pre>
<p>The expression is finally evaluated as not true ('0'), and,
therefore, the DO is not processed. For an explanation of how this evaluation
was reached, see the matrices later in this section.</p>
<p>This same process
is used to evaluate a logical expression using logical variables, as in this
example:</p>
<pre>PGM
DCL &amp;A *LGL
DCL &amp;B *LGL
IF (&amp;A *OR &amp;B) THEN(CALL PGM(PGMA))
.
.
.
ENDPGM</pre>
<p>Here the conditional expression is evaluated to see if the
value of &amp;A or of &amp;B is equal to '1' (true). If either is true, the
whole expression is true, and PGMA is called.</p>
<p>The final evaluation arrived
at for all these examples of logical expressions is based on standard matrices
comparing two values (referred to here as &amp;A and &amp;B) under an *OR
or *AND operator.</p>
<p>Use the following matrix when using *OR with logical
variables or constants: </p>
<dl><dt class="dlterm">If &amp;A is:</dt>
<dd>'0' '0' '1' '1'</dd>
<dt class="dlterm">and &amp;B is:</dt>
<dd>'0' '1' '0' '1'</dd>
<dt class="dlterm">the OR expression is:</dt>
<dd>'0' '1' '1' '1'</dd>
</dl>
<p>In short, for multiple OR operators with logical variables
or constants, the expression is false ('0') if all values are false. The
expression is true ('1') if any values are true.</p>
<pre>PGM
DCL &amp;A *LGL VALUE('0')
DCL &amp;B *LGL VALUE('1')
DCL &amp;C *LGL VALUE('1')
IF (&amp;A *OR &amp;B *OR &amp;C) THEN(CALL PGMA)
.
.
.
ENDPGM</pre>
<p>Here the values are not all false; therefore, the expression
is true, and PGMA is called.</p>
<p>Use the following matrix when evaluating
a logical expression with *AND with logical variables or constants:</p>
<dl><dt class="dlterm">If &amp;A is:</dt>
<dd>'0' '0' '1' '1'</dd>
<dt class="dlterm">and &amp;B is:</dt>
<dd>'0' '1' '0' '1'</dd>
<dt class="dlterm">the ANDed expression is:</dt>
<dd>'0' '0' '0' '1'</dd>
</dl>
<p>For multiple AND operators with logical variables or constants,
the expression is false ('0') when any value is false, and true when they
are all true. </p>
<pre>PGM
DCL &amp;A *LGL VALUE('0')
DCL &amp;B *LGL VALUE('1')
DCL &amp;C *LGL VALUE('1')
IF (&amp;A *AND &amp;B *AND &amp;C) THEN(CALL PGMA)
.
.
.
ENDPGM</pre>
<p>Here the values are not all true; therefore, the expression
is false, and PGMA is not called.</p>
<p>These logical operators can only be
used <em>within</em> an expression when the operands represent a logical value,
as in the preceding examples. It is <em>incorrect</em> to attempt to use OR
or AND for variables that are not logical. For instance: </p>
<pre>PGM
DCL &amp;A *CHAR 3
DCL &amp;B *CHAR 3
DCL &amp;C *CHAR 3
Incorrect: IF (&amp;A *OR &amp;B *OR &amp;C = YES) THEN...</pre>
<p>The correct coding for this would be:</p>
<pre>IF ((&amp;A=YES) *OR (&amp;B=YES) *OR (&amp;C=YES)) THEN...</pre>
<p>In this case, the ORing occurs between relational expressions.</p>
<p>The
logical operator *NOT (or ¬) is used to negate logical variables or constants.
Any *NOT operators must be evaluated before the *AND or *OR operators are
evaluated. Any values that follow *NOT operators must be evaluated before
the logical relationship between the operands is evaluated. </p>
<pre>PGM
DCL &amp;A *LGL '1'
DCL &amp;B *LGL '0'
IF (&amp;A *AND *NOT &amp;B) THEN(CALL PGMA)</pre>
<p>In this example, the values are all true; therefore, the expression
is true, and PGMA is called. </p>
<pre>PGM
DCL &amp;A *LGL
DCL &amp;B *CHAR 3 VALUE('ABC')
DCL &amp;C *CHAR 3 VALUE('XYZ')
CHGVAR &amp;A VALUE(&amp;B *EQ &amp;C)
IF (&amp;A) THEN(CALLPRC PROCA)</pre>
<p>In this example, the value is false, therefore, PROCA is not
called.</p>
</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="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><a href="dountilcmd.htm" title="The Do Until (DOUNTIL) command processes a group of CL commands one or more times.">DOUNTIL command</a></div>
<div><a href="dowhilecmd.htm" title="The DOWHILE command lets you process a group of commands zero or more times while the value of a logical expression is true.">DOWHILE command</a></div>
<div><a href="selectcmd.htm" title="The Select (SELECT) command is used to identify one or more conditions and an associated group of commands to process when that condition is true.">SELECT command and SELECT groups</a></div>
</div>
</div>
</body>
</html>