Operators and Operands

<< Click to Display Table of Contents >>

Navigation:  Concordance Programming Language Reference >

Operators and Operands

The following topic discusses arithmetic, assignment, bitwise, conditional, and logical operators, as well as their precedence relative to each other.

Arithmetic Operators

The arithmetic operators are +, –, *, /, and mod. Unary minus is evaluated right to left, the other arithmetic expressions are evaluated left to right. Standard arithmetic precedence applies: division and multiplication are evaluated before addition and subtraction.

Operator

Description

Unary minus, negative numbers

*

Multiplication

/

Division

mod

The remainder of the first operand divided by the second

+

Addition

Subtraction

 

Assignment Operators

Assignments in CPL are done with =, the assignment operator. This should not be confused with ==, the comparison operator. The assignment operator assigns a value to a variable. The comparison operator determines if the two values are equal and returns a true or false as a result.

CPL assignments also produce a value as a result of the assignment. You can test the value of the assignment and make the assignment in one statement. This allows you to write more compact programs, but it can become confusing if you are not careful. There are times to take advantage of this feature, and times to avoid it.

Example:

if ((data = open("letter.txt","r")) <> -1)

{

/* ... */

close(data);

}

This program fragment opens a file and stores the file handle to the variable called data. The result of the assignment is the value stored in data. This is tested against -1 to see if the file was successfully opened. This construct is both efficient and common in CPL.

Since the assignment operator has a lower precedence than the comparison operator, the assignment to data from open() must be enclosed in parentheses. If the parentheses were not used, CPL would first compare the value of open() to -1. The result of that comparison would be a true or false. CPL would then store a true or false to data, not the file handle returned by open().

 

Bitwise Operators

CPL provides two operators that allow you to manipulate the individual bits in char, short, and int data types.

Operator

Description

&

Bitwise AND compares each bit of the first operand to the corresponding bit of the second operand. Returns a bit set to one for each bit set to one in both operands.

|

Bitwise OR compares each bit of the first operand to the corresponding bits in the second operand. Returns a bit set to one for each bit set to one in either operand.

 

Conditional Operator

The conditional operator “?:” provides a compact way to execute if-then logic. Consider the example used earlier to describe function parameters:

max(float a, b)

{

if (a < b)

a = b;

return(a);

}

It could be rewritten more compactly, by taking advantage of the conditional expression:

max(float a, b)

{

return(a > b ? a : b);

}

The conditional expression evaluates the expression preceding the question mark. It returns the value preceding the colon if it is true, and the value following the colon if it is false. The syntax for the conditional expression is:

(test-expression) ? TRUE : FALSE

The conditional expression has the second lowest precedence of all operators, just above an assignment. Therefore, the test for the conditional expression should almost always be enclosed in parentheses.

Here’s a bit of sample code that appends an s when the number of reported documents is plural.

string=str(count(db))+" Document"+(count(db)<>1)?"s":"";

writeln(handle,string,len(string));

Including the logic in the assignment creates a more concise program, though it may be somewhat terse and less readily understandable. Consider the extra code it would take to write this statement with if-then logic.

 

Logical Operator

The two logical operators are and and or. They each evaluate two operands, on the left and right, and return true or false values. They are usually used to connect individual relational comparisons into more complex relationships.

Operator

Result

and

Evaluates to true only if both operands are true

or

Evaluates to true if either operands are true

 

Precedence of Operators and Order of Evaluation

Operators are evaluated in order of precedence. Parentheses should be used to override precedence where desired. The operators are listed below from highest precedence to lowest. Operators on the same line have the same precedence.

Operator

Operands Evaluated

. –>

left to right

– (unary minus)

right to left

* / mod

left to right

+ –

left to right

< > <= >=

left to right

== <>

left to right

&

left to right

|

left to right

and

left to right

or

left to right

?:

left to right

=

right to left