After you have learned the fundamentals of programming in 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 BASIC, and how they can be used to develop more sophisticated programs.
Constants are static values the 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 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.
Whole numbers between -32768 and +32767. They do not contain decimal points.
Positive or negative real numbers that contain decimal points.
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.0 10-39 to 1.7 1038. 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
Seven or fewer digits
Exponential form using E
A trailing exclamation point (!)
A double-precision constant is any numeric constant with either
Eight or more digits
Exponential form using D
A trailing number sign (#)
The following are examples of single- and double-precision numeric constants:
Variables are the names that you have chosen to represent values used in a 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, BASIC assumes the variable's value to be zero.
6.2.1 Variable Names and Declarations
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 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:
Type of Variable
The following are sample variable names for each type:
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 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.
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 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 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:
Rows and columns begin with 0, not 1, unless otherwise declared. For more information, see the OPTION BASE statement in the 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.
Required Bytes of Storage
Required Bytes of Storage
2 per element
4 per element
8 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, BASIC converts a numeric constant from one type of variable to another, according to the following rules:
If a numeric constant of one type is set equal to a numeric variable of a different type, the number is stored as the type declared in the variable name.
10 A% = 23.42
20 PRINT A%
If a string variable is set equal to a numeric value or vice versa, a "Type Mismatch" error occurs.
During an expression evaluation, all of the operands in an arithmetic or relational operation are converted to the same degree of precision; that is, that of the most precise operand. Also, the result of an arithmetic operation is returned to this degree of precision. For example:
10 D# = 6#/7
20 PRINT D#
The arithmetic is performed in double-precision, and the result is returned in D# as a double-precision value.
10 D = 6#/7
20 PRINT D
The arithmetic is performed in double-precision, and the result is returned to D (single-precision variable) rounded and printed as a single-precision value.
Logical operators convert their operands to integers and return an integer result. Operands must be within the range of -32768 to 32767 or an "Overflow" error occurs.
When a floating-point value is converted to an integer, the fractional portion is rounded. For example:
10 C% = 55.88
20 PRINT C%
If a double-precision variable is assigned a single-precision value, only the first seven digits (rounded), of the converted number are valid. This is because only seven digits of accuracy were supplied with the single-precision value. The absolute value of the difference between the printed double-precision number, and the original single-precision value, is less than 6.3E-8 times the original single-precision value. For example:
10 A = 2.04
20 B# = A
30 PRINT A;B#
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 BASIC are divided into four categories:
6.4.1 Arithmetic Operators
The following are the arithmetic operators recognized by BASIC. They appear in order of precedence.
Operations within parentheses are performed first. Inside the parentheses, the usual order of precedence is maintained.
The following are sample algebraic expressions and their BASIC counterparts:
Two consecutive operators must be separated by parentheses.
184.108.40.206 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 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 BASIC, modulus arithmetic follows integer division. The INT and FIX functions, described in the BASIC User's Reference, are also useful in modulus arithmetic.
220.127.116.11 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.
Less than or equal to
Greater than or equal to
The equal sign is also used to assign a value to a variable. See the LET statement in the 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.
Results Returned by Logical Operations
X AND Y
X OR Y
X XOR Y
X EQV Y
X IMP Y
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
IF NOT P THEN 100
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.
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. BASIC has intrinsic functions that reside in the system, such as SQR (square root) or SIN (sine).
BASIC also allows user-defined functions written by the programmer. See the DEF FN statement in the BASIC User's Reference.
6.4.5 String Operators
To compare strings, use the same relational operators used with numbers:
Less than or equal to
Greater than or equal to
The 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.
"AA" < "AB"
"FILENAME" = "FILENAME"
"X&" > "X#"
"CL " > "CL"
"kg" > "KG"
"SMYTH" < "SMYTHE"
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:
20 PRINT A$+B$
30 PRINT "NEW " + A$+B$
All Frank Sinatra recordings, All Beatles albums, All solo albums from John Lennon, Paul McCartney, George Harrison and Ringo Starr, All Beatles songs, List of Platinum selling albums, Database with hits from 1950 to 1999.
Sought after DLL files, Nutworks magazine (retro), Worldwide ISO and TLD codes, File extensions database, The Hackers dictionary, Free software for the Atari, Amiga and Macintosh, Nokia smartphone, PocketPC, List of all BASIC programming language commands, List of all DOS commands, Free Advanced Bash-Scripting Guide (Linux-Unix)