Chapter 6
Constants, Variables, Expressions and Operators

After you have learned the fundamentals of programming in GW-BASIC, you will find that you will want to write more complex programs. The information in this chapter will help you learn more about the use of constants, variables, expressions, and operators in GW-BASIC, and how they can be used to develop more sophisticated programs.

6.1 Constants

Constants are static values the GW-BASIC Interpreter uses during execution of your program. There are two types of constants: string and numeric.

A string constant is a sequence of 0 to 255 alphanumeric characters enclosed in double quotation marks. The following are sample string constants:

Number of Employees

Numeric constants can be positive or negative. When entering a numeric constant in GW-BASIC, you should not type the commas. For instance, if the number 10,000 were to be entered as a constant, it would be typed as 10000. There are five types of numeric constants: integer, fixed-point, floating-point, hexadecimal, and octal.

Constant Description
Integer Whole numbers between -32768 and +32767. They do not contain decimal points.
Fixed-Point Positive or negative real numbers that contain decimal points.
Floating-Point Constants

Positive or negative numbers represented in exponential form (similar to scientific notation). A floating-point constant consists of an optionally-signed integer or fixed-point number (the mantissa), followed by the letter E and an optionally-signed integer (the exponent). The allowable range for floating-point constants is 3.010-39 to 1.71038. For example:


Hexadecimal numbers with prefix &H. For example:


Octal numbers with the prefix &O or &. For example:


6.1.1 Single- and Double-Precision Form for Numeric Constants

Numeric constants can be integers, single-precision, or double-precision numbers. Integer constants are stored as whole numbers only. Single-precision numeric constants are stored with 7 digits (although only 6 may be accurate). Double-precision numeric constants are stored with 17 digits of precision, and printed with as many as 16 digits.

A single-precision constant is any numeric constant with either

A double-precision constant is any numeric constant with either

The following are examples of single- and double-precision numeric constants:

Single-Precision ConstantsDouble-Precision Constants

6.2 Variables

Variables are the names that you have chosen to represent values used in a GW-BASIC program. The value of a variable may be assigned specifically, or may be the result of calculations in your program. If a variable is assigned no value, GW-BASIC assumes the variable's value to be zero.

6.2.1 Variable Names and Declarations

GW-BASIC variable names may be any length; up to 40 characters are significant. The characters allowed in a variable name are letters, numbers, and the decimal point. The first character in the variable name must be a letter. Special type declaration characters are also allowed.

Reserved words (all the words used as GW-BASIC commands, statements, functions, and operators) can't be used as variable names. However, if the reserved word is embedded within the variable name, it will be allowed.

Variables may represent either numeric values or strings.

6.2.2 Type Declaration Characters

Type declaration characters indicate what a variable represents. The following type declaration characters are recognized:

CharacterType of Variable
$String variable
%Integer variable
!Single-precision variable
#Double-precision variable

The following are sample variable names for each type:

Variable Type Sample Name
String variableN$
Integer variableLIMIT%
Single-precision variableMINIMUM!
Double-precision variablePl#

The default type for a numeric variable name is single-precision. Double-precision, while very accurate, uses more memory space and more calculation time. Single-precision is sufficiently accurate for most applications. However, the seventh significant digit (if printed) will not always be accurate. You should be very careful when making conversions between integer, single-precision, and double-precision variables.

The following variable is a single-precision value by default:


Variables beginning with FN are assumed to be calls to a user-defined function.

The GW-BASIC statements DEFINT, DEFSTR, DEFSNG, and DEFDBL may be included in a program to declare the types of values for certain variable names.

6.2.3 Array Variables

An array is a group or table of values referenced by the same variable name. Each element in an array is referenced by an array variable that is a subscripted integer or an integer expression. The subscript is enclosed within parentheses. An array variable name has as many subscripts as there are dimensions in the array.

For example,


references a value in a one-dimensional array, while


references a value in a two-dimensional array.

The maximum number of dimensions for an array in GW-BASIC is 255. The maximum number of elements per dimension is 32767.


If you are using an array with a subscript value greater than 10, you should use the DIM statement. Refer to the GW-BASIC User's Reference for more information. If a subscript greater than the maximum specified is used, you will receive the error message "Subscript out of range."

Multidimensional arrays (more than one subscript separated by commas) are useful for storing tabular data. For example, A(1,4) could be used to represent a two-row, five-column array such as the following:

Column  0 1 2 3 4
Row 0 10 20 30 40 50
Row 1 60 70 80 90100

In this example, element A(1,2)=80 and A(0,3)=40.

Rows and columns begin with 0, not 1, unless otherwise declared. For more information, see the OPTION BASE statement in the GW-BASIC User's Reference.

6.2.4 Memory Space Requirements for Variable Storage

The different types of variables require different amounts of storage. Depending on the storage and memory capacity of your computer and the size of the program that you are developing, these can be important considerations.

VariableRequired Bytes of Storage
ArraysRequired Bytes of Storage
Integer2 per element
Single-precision4 per element
Double-precision8 per element


Three bytes overhead, plus the present contents of the string as one byte for each character in the string. Quotation marks marking the beginning and end of each string are not counted.

6.3 Type Conversion

When necessary, GW-BASIC converts a numeric constant from one type of variable to another, according to the following rules:

6.4 Expressions and Operators

An expression may be simply a string or numeric constant, a variable, or it may combine constants and variables with operators to produce a single value.

Operators perform mathematical or logical operations on values. The operators provided by GW-BASIC are divided into four categories:

6.4.1 Arithmetic Operators

The following are the arithmetic operators recognized by GW-BASIC. They appear in order of precedence.

/Floating-point Division

Operations within parentheses are performed first. Inside the parentheses, the usual order of precedence is maintained.

The following are sample algebraic expressions and their GW-BASIC counterparts:

Algebraic ExpressionBASIC Expression

Two consecutive operators must be separated by parentheses. Integer Division and Modulus Arithmetic

Two additional arithmetic operators are available: integer division and modulus arithmetic.

Integer division is denoted by the backslash (\). The operands are rounded to integers (must be within the range of -32768 to 32767) before the division is performed, and the quotient is truncated to an integer.

The following are examples of integer division:

10\4 = 2
25.68\6.99 = 3

In the order of occurrence within GW-BASIC, the integer division will be performed just after floating-point division.

Modulus arithmetic is denoted by the operator MOD. It gives the integer value that is the remainder of an integer division.

The following are examples of modulus arithmetic:

10.4 MOD 4 = 2
(10/4=2 with a remainder 2)
25.68 MOD 6.99 = 5
(26/7=3 with a remainder 5)

In the order of occurrence within GW-BASIC, modulus arithmetic follows integer division. The INT and FIX functions, described in the GW-BASIC User's Reference, are also useful in modulus arithmetic. Overflow and Division by Zero

If, during the evaluation of an expression, a division by zero is encountered, the "Division by zero" error message appears, machine infinity with the sign of the numerator is supplied as the result of the division, and execution continues.

If the evaluation of an exponentiation results in zero being raised to a negative power, the "Division by zero" error message appears, positive machine infinity is supplied as the result of the exponentiation, and execution continues.

If overflow occurs, the "Overflow" error message appears, machine infinity with the algebraically correct sign is supplied as the result, and execution continues. The errors that occur in overflow and division by zero will not be trapped by the error trapping function.

6.4.2 Relational Operators

Relational operators let you compare two values. The result of the comparison is either true (-1) or false (0). This result can then be used to make a decision regarding program flow.

Table 6.1 displays the relational operators.

Table 6.1

Relational Operators

OperatorRelation TestedExpression
<Less thanX<Y
>Greater thanX>Y
<=Less than or equal toX<=Y
>=Greater than or equal toX>=Y

The equal sign is also used to assign a value to a variable. See the LET statement in the GW-BASIC User's Reference.

When arithmetic and relational operators are combined in one expression, the arithmetic is always performed first:

X+Y < (T-1)/Z

This expression is true if the value of X plus Y is lessthan the value of T-1 divided by Z.

6.4.3 Logical Operators

Logical operators perform tests on multiple relations, bit manipulation, or Boolean operations. The logical operator returns a bit-wise result which is either true (not zero) or false (zero). In an expression, logical operations are performed after arithmetic and relational operations. The outcome of a logical operation is determined as shown in the following table. The operators are listed in order of precedence.

Table 6.2

Results Returned by Logical Operations

Operation Value Value Result

Just as the relational operators can be used to make decisions regarding program flow, logical operators can connect two or more relations and return a true or false value to be used in a decision. For example:

IF D<200 AND F<4 THEN 80
IF I>10 OR K<0 THEN 50

Logical operators convert their operands to 16-bit, signed, two's complement integers within the range of -32768 to +32767 (if the operands are not within this range, an error results). If both operands are supplied as 0 or -1, logical operators return 0 or -1. The given operation is performed on these integers in bits; that is, each bit of the result is determined by the corresponding bits in the two operands.

Thus, it is possible to use logical operators to test bytes for a particular bit pattern. For instance, the AND operator may be used to mask all but one of the bits of a status byte at a machine I/O port. The OR operator may be used to merge two bytes to create a particular binary value. The following examples demonstrate how the logical operators work:

63 AND 16=16 63 = binary 111111 and 16 = binary 10000, so 63 AND 16 = 16
15 AND 14=14 15 = binary 1111 and 14 = binary 1110, so 15 AND 14 = 14 (binary 1110)
-1 AND 8=8 -1 = binary 1111111111111111 and 8 = binary 1000, so -1 AND 8 = 8
4 OR 2=6 4 = binary 100 and 2 = binary 10, so 4 OR 2 = 6 (binary 110)
10 OR 10=10 10 = binary 1010, so 1010 OR 1010 =1010 (10)
-1 OR -2=-1 -1 = binary 1111111111111111 and -2 = binary 1111111111111110, so -1 OR -2 = -1. The bit complement of 16 zeros is 16 ones, which is the two's complement representation of -1.
NOT X=-(X+1) The two's complement of any integer is the bit complement plus one.

6.4.4 Functional Operators

A function is used in an expression to call a predetermined operation that is to be performed on an operand. GW-BASIC has intrinsic functions that reside in the system, such as SQR (square root) or SIN (sine).

GW-BASIC also allows user-defined functions written by the programmer. See the DEF FN statement in the GW-BASIC User's Reference.

6.4.5 String Operators

To compare strings, use the same relational operators used with numbers:

=Equal to
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to

The GW-BASIC Interpreter compares strings by taking one character at a time from each string and comparing their ASCII codes. If the ASCII codes in each string are the same, the strings are equal. If the ASCII codes differ, the lower code number will precede the higher code. If the interpreter reaches the end of one string during string comparison, the shorter string is said to be smaller, providing that both strings are the same up to that point. Leading and trailing blanks are significant.

For example:

"AA" < "AB"
"X&" > "X#"
"CL " > "CL"
"kg" > "KG"
B$ < "9/12/78" where B$ = "8/12/78"

String comparisons can also be used to test string values or to alphabetize strings. All string constants used in comparison expressions must be enclosed in quotation marks.

Strings can be concatenated by using the plus (+) sign. For example:

10 A$="FILE":B$="NAME"
20 PRINT A$+B$
30 PRINT "NEW " + A$+B$