# Calculator Programming Tutorial

## Programming Building Blocks I

### Expressions

#### Logical Operators

##### Introduction

Logical operators operate upon Boolean values (true and false) and produce other Boolean values. Boolean values can arise from relational expressions — such as “a < b” — or from other condition tests.

There are three primary logical operators: and, or, and not. The and operator combines two Boolean values and results in true only if both operands are true. The or operator combines two Boolean values and results in true if either operand is true, or if both operands are true. The not operator reverses a Boolean value: true results in false, and false results in true. These values are shown in the following truth table:

A

not A

B

A and B

A or B

False

True

False

False

False

True

False

True

True

False

False

False

True

True

True

True

Any logical function can be built up out of combinations of these elementary operations.

##### Native Logical Operators

All of the CFX-9850G calculator, JavaScript, and Visual Basic for Applications provide these three basic logical operations. The names these platforms have for the operations are as follows:

Operator

Calculator

JavaScript

VBA

and

And

&&

And

or

Or

||

Or

not

Not

!

Not

Note that the operator symbols are doubled in JavaScript for And and Or. The single symbol versions of these operators perform bitwise operations, described in the next page.

The FX-7400G calculator does not directly implement these logical operations. However, the logical operations may be synthesized from operations the calculator does provide, and it is frequently convenient to do this even on platforms that do provide logical operators. This is discussed in the “Programmatic Logical Operators” section below.

In addition to these basic logical operations, Visual Basic for Applications supplies the exclusive or, implies, and equivalence logical operators. These operators (known as Xor, Imp, and Eqv) are equivalent to:

A Xor B = (A or B) and not (A and B)
A Eqv B = not (A xor B)
A Imp B = (not A) or (A and B)

When dealing with logical quantities, Eqv is equivalent to the = comparison operator.

These logical operators allow the programmer to construct predicates, or logical expressions, of astounding complexity. Very complicated expressions can frequently be simplified by applying the simple logical principles:

A or (not A) = True (completeness)
A and (not A) = False (non-contradiction)
A = not (not A) (excluded middle)

(These axioms, incidentally, give rise to what is called an Aristotlean logic. Aristotlean logic has a role similar to that of Euclidean geometry and Newtonian physics: it is the “canonical” variety of its field, it is the variety we use in day to day operations, and there is substantial evidence that it fails to accurately model physical reality under extreme conditions.) Also useful are DeMorgan’s laws:

Not (A and B) = (not A) or (not B)
Not (A or B) = (not A) and (not B)

Although both JavaScript and Visual Basic for Applications have a Boolean (logical) data type, they also permit these operators to be applied to numeric values. The calculator, having only numeric values, also implements these operators on numeric values. In JavaScript and the calculator, a zero value is regarded as false, any non-zero value is regarded as true, and the result of a logical operation is either 0 (to indicate false) or 1 (to indicate true).

In Visual Basic for Applications, however, these operators applied to integer values become bitwise operators, described in the next section. However, integers and boolean values are freely interchangeable: zero values are considered false, non-zero values are considered true, and the integer equivalents of false and true are 0 and -1, respectively. (Note that -1 is a value with all bits set.) All the bitwise operations, applied to integer values that are either 0 or -1, result in integer values that are either 0 or -1. In this manner, the bitwise operations applied to the generated integer values for false and true behave as if they were logical operators applied to boolean values.

##### Programmatic Logical Operators

The various logical operations can be easily implemented on platforms not having them, but having an If statement, such as the FX-7400G calculator. This emulation is also frequently convenient even on platforms that do implement logical operations.

The and operation can be seen to be true only if its two arguments are true. This means an and function could specified as:

```if a
then
b
else
false;
```

Alternately, an action that is to take place only if a and b is true could also be specified as:

```if a
then
if b
then
{ action };
```

It is convenient to write a logical conjunction in this way when the expression b is valid only if expression a is true. For example, it may be that b is a function which would result in a run-time error unless a is true. The JavaScript && operator in fact guarantees that the right operand will be evaluated only if the left operand is true, so this consideration does not apply in JavaScript.

The and function may also be considered as a test that all of a series of a logical expressions are true. Thus, another way of performing an and operation is to count the number of logical expressions that are true, and to accept the and function as true only if the count of true expressions is appropriate:

```count := 0;
if a
then
count := count + 1;
if b
then
count := count + 1;
if count = 2
then
{ action };
```

Several programming platforms — and in particular, all of JavaScript, Visual Basic for Applications, and the calculators — generate numeric values for Boolean expressions. In cases where the value for false is 0 (as it is in all of these platforms), the values of the expressions can be multiplied:

```if a * b ≠ 0
then
{ action };
```

Alternately, the count of true operations may be generated by simply adding the operations:

```if a + b = 2
then
{ action };
```

(In Visual Basic for Applications, a Boolean expression generates -1 for true, rather than 1. The equality comparison would therefore be for -2, not 2.)

An or operation is similar to the and operation, with the exception that the or operation is true if either or both of its arguments are true. The or operation could be specified as:

```if a
then
true
else
b;
```

Alternatively, an action that is to take place if a or b is true could be specified as:

```if a
then
{ action }
else
if b
then
{ action };
```

As the action code needs to be duplicated in this sequence, it is less convenient than the equivalent sequence for the and operation. However, the arithmetic equivalent shares the convenience of those for the and operation:

```if a + b ≠ 0
then
{ action };
```

Generally, replacing logical operators with code depends on one of two strategies: either using a mapping of logical values and operations to arithmetic values and operations, or using the additional state of the current program location to maintain a current value of the logical expression. (The if sequences use this latter technique.)

It is also entirely possible to package one of these implementation techniques into a subprogram, and to call the subprogram when a logical operation is desired. With proper subprogram design, this has the additional advantage of specifying the operation with data — an operation code — rather than with a name. This permits delaying until run time the selection of which operation is to be performed, which may be advantageous in certain circumstances.

The problem of providing programmatic logical operators is in fact an example in this tutorial.

Page last updated 3 March 2002.

# Brian’s Casio Calculator Corner

Home

Programs

Tutorial

Preface

Introduction

Fundamentals

Building Blocks I

Introduction

Data Types

Numbers

Variables

Expressions

Introduction

Arithmetic

Grouping

Relational

Logical

Bitwise

String

Side Effect

Functions

Control Flow I

Control Flow II

Subprograms

Basic I/O

Algorithms

A First Program

Examples

Exercises

Modularization

Data Structures I

Recursion

Program Attributes

Building Blocks II

Algorithm Analysis

Structuring

Data Structures II

Abstract Types

Objects

Problem Analysis

Reference Card

References

Puzzles

Site Information