Constants, Variables, and Data Types

Constants are values that remain unchanged throughout program execution.

Assigning and Using Constants

Constants can be used directly, or they can be assigned a name with the assignment (=) or EQUATE statement.

For assigning a constant, the EQUATE statement is preferable since there is nothing to stop a constant assigned with = (equal sign) from being changed later in the program’s execution. A constant assigned with EQUATE, on the other hand, can never be changed: if EQUATE is used, the programmer is ensured that a constant will remain a constant.

The EQUATE statement is also more efficient, since a constant assigned with = needs to be reassigned each time the program is executed.

Example

PRINT "HELLO, WORLD"

The string "HELLO, WORLD" is a constant string value used directly in the PRINT statement. Alternatively, the program might have read:

EQUATE GREETING TO "HELLO, WORLD"

PRINT GREETING

By assigning the name GREETING to the constant string HELLO WORLD, it can be accessed by that name any time later in the program.

Assigning and Using Variables

Variables are symbolic names that represent stored data values and can change in value during program execution. The value can be explicitly assigned by the programmer, can be read as input, or can be the result of operations performed by the program during execution.

At the start of program execution, all variables are set to an unassigned state. Any attempt to use a variable in the unassigned state produces an error message, and a value of 0 is assumed.

Names for both variables and constants must begin with an initial alphabetic character. They can also include one or more digits, letters, periods, or dollar signs. (Note that hyphens and underscores are not valid in a variable name.) Uppercase and lowercase are interpreted differently. A variable name can be any length, but it cannot be the same as any reserved word.

Data Typing in mvBASIC

In many other programming languages, such as Pascal and PL/I, a distinction is made among types of data. In these languages, all constants, variables, and their data types (integer, real, string, character, etc.) have to be declared at the beginning of the program so that the compiler will know how to store the data. Furthermore, the size of the variable often has to be declared so that the compiler will know how much space to set aside.

In mvBASIC, on the other hand, no data typing is made by the compiler: all data typing is made at run time, by context. A variable can therefore alternate between numeric and string values within the program. The only thing to be careful of is that when string values are assigned in the program text, they must be delimited by single quotes ('), double quotes ("), or backslashes (\). Otherwise, they are assumed to be variable names.

There is, of course, a difference between the way a numeric value and a string value can be treated: it is unreasonable to expect a program to take the square root of the string CARL. In such a situation, however, a fatal error will not occur—when a string value is applied to a numeric function, a value of 0 is assumed, a warning message is printed, and the program continues from there. String operations, on the other hand, can be executed on numeric values without conflict.

The advantage to no data typing is obvious; less work for the programmer and more flexibility for the program. The disadvantage is that errors which one might expect the compiler to detect are not caught. For example, if a variable name is misspelled, the compiler will simply assume that it is a new variable, and the program will successfully compile without an error or warning. Similarly, if a string variable containing CARL were accidentally used in the SQRT function, the programmer would not find out until the program was executed.

See Also

Format, Data and Expressions

Program Format

Building Expressions

Advanced Data Types