244 lines
13 KiB
HTML
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 (&) 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, |>, *TCAT, |<)</li>
|
||
|
<li>Relational (*EQ, =, *GT, >, *LT, <, *GE, >=, *LE, <=, *NE,
|
||
|
¬=, *NG, ¬>, *NL, ¬<)</li>
|
||
|
</ul>
|
||
|
<p>The following are examples of logical expressions: </p>
|
||
|
<pre>((&C *LT 1) *AND (&TIME *GT 1430))
|
||
|
(&C *LT 1 *AND &TIME *GT 1430)
|
||
|
((&C < 1) & (&TIME>1430))
|
||
|
((&C< 1) & (&TIME>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 &gt, &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>((&C *LT 1) *AND (&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 (&A=&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 ((&A=&B) *AND (&C=&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 &RESP *DEC 1
|
||
|
DCL &A *DEC 1
|
||
|
DCL &B *CHAR 2
|
||
|
IF ((&RESP=1) *AND (&A=5) *AND (&B=NO)) THEN(DO)
|
||
|
.
|
||
|
.
|
||
|
.
|
||
|
ENDDO
|
||
|
CHGVAR &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 (((&A=&B) *OR (&A=&C)) *AND ((&C=1) *OR (&D='0'))) THEN(DO)</pre>
|
||
|
<p>In this case, &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 ((&A = &B) *AND (&C = &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 &A *LGL
|
||
|
DCL &B *LGL
|
||
|
IF (&A *OR &B) THEN(CALL PGM(PGMA))
|
||
|
.
|
||
|
.
|
||
|
.
|
||
|
ENDPGM</pre>
|
||
|
<p>Here the conditional expression is evaluated to see if the
|
||
|
value of &A or of &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 &A and &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 &A is:</dt>
|
||
|
<dd>'0' '0' '1' '1'</dd>
|
||
|
<dt class="dlterm">and &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 &A *LGL VALUE('0')
|
||
|
DCL &B *LGL VALUE('1')
|
||
|
DCL &C *LGL VALUE('1')
|
||
|
IF (&A *OR &B *OR &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 &A is:</dt>
|
||
|
<dd>'0' '0' '1' '1'</dd>
|
||
|
<dt class="dlterm">and &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 &A *LGL VALUE('0')
|
||
|
DCL &B *LGL VALUE('1')
|
||
|
DCL &C *LGL VALUE('1')
|
||
|
IF (&A *AND &B *AND &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 &A *CHAR 3
|
||
|
DCL &B *CHAR 3
|
||
|
DCL &C *CHAR 3
|
||
|
|
||
|
Incorrect: IF (&A *OR &B *OR &C = YES) THEN...</pre>
|
||
|
<p>The correct coding for this would be:</p>
|
||
|
<pre>IF ((&A=YES) *OR (&B=YES) *OR (&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 &A *LGL '1'
|
||
|
DCL &B *LGL '0'
|
||
|
IF (&A *AND *NOT &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 &A *LGL
|
||
|
DCL &B *CHAR 3 VALUE('ABC')
|
||
|
DCL &C *CHAR 3 VALUE('XYZ')
|
||
|
CHGVAR &A VALUE(&B *EQ &C)
|
||
|
IF (&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>
|