# Calculator Programming Tutorial

## Programming Building Blocks I

### Variables and Assignment

The execution of a program performs a set of operations upon a set of data, and yields a set of results. The operations are fixed, encoded by the text of the program. The data, however, may change from one execution of a program to the next. (There is relatively little value in repeating a calculation exactly: presumably the results would have been saved the first time were they interesting.) One of the basic mechanisms giving rise to this ability to repeat a computation upon a different set of data is the variable.

Fundamentally, a variable is a datum identified by a name, rather than by value.

There are numerous fixed values identified by a name, rather than by a value, in science and mathematics. For example, the ratio between the circumference and the diameter of a circle, approximately 3.1415926535, is known by the name “π” (the Greek letter pi). Similarly, the speed of light, approximately 2.995×108 meters per second, is known by the name “c” (the Latin letter c). These fixed values, however, do not change: they are constants, not variables.

There are two ways of thinking about variables. The first way is to think of them as containers into which values are placed. In this way of thinking, a new value can displace an old value. The second way is to think of them as labels placed on values. In this way of thinking, a label can be moved from one value to another. Both of these are valid and useful ways of thinking about variables. Considering variables to be containers into which values are placed is the traditional way of thinking about variables. Considering variables to be labels placed on values is a more accurate model of what happens with what are called “reference types.” Reference types, however, do not appear in the calculator, so we will not pay them a great deal of attention in this tutorial. The overall properties of variables, expressed in these two manners, are given in the following table.

Variables are containers

Variables are labels

A variable initially has no particular value. It can be anything, and is nothing of interest. A variable in this state is called undefined.

A variable initially refers to no particular value. It can be a label on anything, and is nothing of interest. A variable in this state is called undefined.

A new value can be placed into a variable. This new value displaces any old value. The operation of replacing a variable’s value with a new value is called assignment.

A label can be moved from one value to another value. The operation of moving a label from one value to another value is called assignment.

At any specific time, a variable has a specific value. This value is called the current value of the variable.

At any specific time, a variable labels a specific value. The value so labeled is called the current value of the variable.

Strictly speaking, variables do not have a value. Instead, variables have a sequence of values — some of which lie in the past, one of which is current, and some of which lie in the future. The set of all current variable values is called the program state.

#### JavaScript Variables and Assignment

In JavaScript, variable names must follow the following rules:

• The first character of the name may be an uppercase or lowercase letter, the underscore (_) character, or the dollar sign (\$) character.
• Any following characters of the name may be uppercase or lowercase letters, the underscore character, the dollar sign character, or a decimal digit.
• Names are case sensitive: i and I are different variables, as are clickEvent and clickevent.
• The name may not be any of the following reserved words: abstract boolean break byte case catch char class const continue debugger default delete do double else enum export extends false final finally float for function goto if implements import in instanceof int interface long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var void while with.

Note that there are no length limits on JavaScript names.

In JavaScript, variables are declared in a var statement, and may optionally have an expression giving an initial value to the variable:

```var i, j = 10, k;
```

The statement above defines three variables, and assigns a value to one.

Values are assigned to variables with the assignment statement. The value of an expression e can be assigned to a variable v with the statement:

```v = e;
```

#### Calculator Variables and Assignment

In the Casio FX-7400G calculator, there are 26 variables, named A through Z. These variables always exist and cannot be declared.

In the Casio CFX-9850G calculator, there are 28 variables, named A through Z, r, and θ. These variables always exist and cannot be declared.

In the calculators, the value of an expression e can be assigned to a variable v with the statement:

```e→v
```

#### VBA Variables and Assignment

In Visual Basic for Applications, variable names must follow the following rules:

• The first character of the name may be an uppercase or lowercase letter.
• Any following characters of the name may be uppercase or lowercase letters, the underscore (_) character, or a decimal digit.
• Names may be at most 255 characters long.
• Names are case insensitive: i and I are the same variable. However, editing in the VBA environment preserves the case of declared names.
• The name may not be any of the VBA reserved words.

The VBA on-line help does not have any complete list of reserved words. However, an attempt to declare a reserved word as a variable will result in an error message.

Visual Basic for Application variables are declared with the Private, Public, Static, or Dim statements. Public and Private are used at the module level to declare variables accessible from other modules, or inaccessible from other modules, respectively. Static and Dim are used inside subroutines or functions to declare variables that retain their values across invocations, or do not retain their values across invocations, respectively. For example, the declaration:

```Dim lCount as Long
```

declares a variables, lCount, with the Long data type.

Values are assigned to variables with the assignment statement. The value of an expression e can be assigned to a variable v with the statement:

```Let v = e
```

The keyword Let is optional, and is usually omitted. If present, it must be the first element in the statement. (Other assignment keywords are Lset, Rset, and Set, but these are used in specialized contexts.)

Page last updated 30 December 2001.

# Brian’s Casio Calculator Corner

Home

Programs

Tutorial

Preface

Introduction

Fundamentals

Building Blocks I

Introduction

Data Types

Numbers

Variables

Expressions

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