Calculator Programming Tutorial

Programming Building Blocks I

Expressions

Arithmetic Operators

Arithmetic operators operate upon numbers and produce numbers as their results. Common operations include addition, subtraction, multiplication, and division. Less common operations include negation, incrementing, decrementing, exponentiation, logarithms, bitwise operators, trigonometric functions, and so forth.

Operators are classified as unary or binary as they take one or two operands. (JavaScript has a ternary operator, one taking three operands, as well.) The only common unary operator is negation. There are several common arithmetic binary operators.

The common arithmetic operators are:

Operator

Meaning

-

Negation: the value of the right operand is negated.

×

Multiplication: the values of the left and right operands are multiplied by one another.

÷

Division: the value of the left operand is divided by the value of the right operand.

+

Addition: the values of the left and right operands are added together.

-

Subtraction: the value of the right operand is subtracted from the value of the left operand.

There are two items of note about the above table. Firstly, the negation and subtraction operators are typographically similar to one another; and secondly, the operators are listed roughly in the order in which they are performed.

Negation

Negation, or multiplication by –1, is the only common unary arithmetic operator. Subtraction generally shares the same symbol, a hyphen, but is a binary arithmetic operator. In Visual Basic for Applications and JavaScript, the hyphen is used for both operators, and the language processor differentiates between them by context. In the calculator, the operators are in fact different: subtraction uses the key labeled -, and unary negation uses the key labeled (-).

Both Visual Basic for Applications and JavaScript define + as a unary operator, which has no effect: the value of its result is the value of the expression to which it is applied. The Visual Basic for Applications program editor, in fact, deletes the unary + operator from expressions in which it appears. Because this operator has no effect, it is uncommon to see it used.

Multiplication and Division

As we all learned in grammar school, multiplication division are regarded as “more important” than addition and subtraction, and happen first. So an expression such as:

3 × 5 + 4

means (3 × 5) + 4, or 15 + 4, or 19. The expression does not mean 3 × (5 + 4), or 3 × 9, or 27. This is formalized in computer languages by the notion of operator precedence. An operator with higher precedence is performed before an operator with lower precedence, in the absence of grouping operators.

In mathematics, the multiplication operator is frequently omitted. The expression 5sin-1 3y, for example, has two implied multiplications: multiplication of y by 3, and multiplication of sin-1 3y by 5. When multiplication is explicitly shown, the operator is generally either an x-shaped cross, ×, or a raised dot, •. Most computer languages, however, require that multiplication be explicitly shown. Moreover, most computer languages are coded in the ASCII character set, which does not have either a cross or a dot as a character. In JavaScript and Visual Basic for Applications, multiplication is shown by the character asterisk (*). Thus the expression 5sin-1 3y would be written in JavaScript as 5 * Math.asin (3 * y), where Math.asin() is the arc sine routine.

The calculator, however, uses notation much more similar to standard mathematics. Multiplication is shown in one of two ways: by simply putting the two expressions next to one another, as in AB or (A+5)sin-1 B, or with the key labeled ×. The expression 5sin-1 3y would be written in the calculator as 5sin-1 3Y, where sin-1 is the arc sine operator.

In mathematics, division is frequently denoted using typographical layout: one quantity is placed above another quantity, with a bar separating the two. Thus, the ratio of a+5 and b-3 could be shown as

However, this convention cannot be easily replicated in linear text, which is used to encode most computer programs. Instead, in most languages, and in particular in both Visual Basic for Applications and JavaScript, the slash (/) character is used for division. However, since division is regarded as having a higher precedence than addition and subtraction, the above expression must be written as (a+5)/(b-3). The expression a+5/b-3 would be regarded as equivalent to a+(5/b)-3, which is not what is intended.

Once again, the calculator uses notation more similar to mathematics. The division operator in the calculator is the key labeled ÷. However, the precedence of division is still higher than that of addition and subtraction, to the above quantity would be represented in the calculator as (a+5)÷(b-3).

Visual Basic for Applications has an additional division operator: the backwards slash, \. This operator requires integer operands, rather than general numeric operands, and produces an integer result, rather than a general numeric result. This operator produces the integer portion of the division, and discards any remainder. Although this operator does not appear in JavaScript and the calculator, its effect can be obtained through applying the Math.floor() or Int functions, respectively, to the result produced by standard division, after suitable adjustment for negative quantities.

JavaScript and Visual Basic for Applications also have a division-like operator, the modulus operator. In JavaScript, this operator is denoted by the percent sign (%); in Visual Basic for Applications, by the Mod keyword. This operator produces the remainder after the division of its left operand by its right operand. In JavaScript, the two operands can be general numbers. In Visual Basic for Applications, the two operands must be integers. Although the calculator does not have a remainder operator, the remainder can be calculated as follows:

A mod B = A – B Int (A÷B)

Finally, after all this, comes addition and subtraction. These are comparatively simple. Addition is denoted by +, just as it is in mathematics, and subtraction by -, just as it is in mathematics.

Page last updated 13 January 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