BASIC LANGUAGE SYNTAX
How to Use This Material
Descriptions of some of the BASIC language statements and functions are described in this appendix. The entries are arranged alphabetically.
The description of each statement and function is formatted
as follows:
|
Purpose: |
Tells what the statement or function
does. |
|
Format: |
Shows the correct format for the
statement or function. Keep the
following rules in mind: |
|
|
·
Words in capital letters are keywords
and must be entered as shown, except that they can be entered in any
combination of uppercase and lowercase letters. BASIC automatically converts letters to
uppercase unless they are enclosed in quote marks. ·
You are to supply any items shown in
lowercase italic letters. ·
Items in square brackets ([ ]) are
optional. ·
An ellipsis (…) indicates that an
item can be repeated as many times as you wish. ·
All punctuation except square
brackets (such as commas, parentheses, semicolons, hyphens or equal signs)
must be included where shown. |
|
Remarks: |
Describes in detail how to use the
statement or function. |
|
Example: |
Shows sample programs, or program segments that demonstrate the use of the statement or function. |
ABS
Function
![]()
|
Purpose: |
Returns the absolute value of the
expression x. |
|
|
|
|
Format: |
v = ABS(x) |
|
|
|
|
Remarks: |
x can be any numerical expression. The absolute value of a number is
always positive or zero. |
|
|
|
|
Example: |
This example shows that the absolute
value of -35 is positive 35. PRINT
ABS(7*(-5)) 35 |
ASC
Function
![]()
|
Purpose: |
Returns the ASCII code of the first
character in string (x$). |
|
|
|
|
Format: |
v = ASC(x$) |
|
|
|
|
Remarks: |
x$ can be any string expression. The result of the ASC function is a
numerical value that is the ASCII code of the first character of the string x$.
If x$ is null, an
"illegal function call" error is returned. |
|
|
|
|
Example: |
This example shows that the ASCII
code for capital "T" is 84. PRINT ASC("TEST")
would work just as well. 10 X$
= "TEST" 20 PRINT ASC(X$) RUN 84 |
|
|
|
CHR$
Function
![]()
|
Purpose: |
Converts an ASCII code to its
character equivalent. |
|
|
|
|
Format: |
v$= CHR$(n) |
|
|
|
|
Remarks: |
n must be in
the range 0 to 255. The CHR$ function returns the
one-character string with ASCII code n. CHR$ is commonly used to send a special
character to the screen or printer.
For example, the BEL character, which beeps the speaker, might be
included as CHR$(7) as the preface to an error message. |
|
|
|
|
Example: |
This example prints the character
equivalent of ASCII code 66. PRINT
CHR$(66) B |
DATA
Statement
![]()
|
Purpose: |
Stores the numeric and string
constants that are accessed by a program's READ statement. |
|
|
|
|
Format: |
DATA constant{,constant}… |
|
|
|
|
Remarks: |
constant can be a numeric or string
constant. No expressions are allowed
in the list. The numeric constants can
be in any format: integer, fixed point, floating point, hex, or octal. String constants in DATA statements do not
have to be enclosed by quotation marks, unless the string contains commas,
colons, or significant leading or trailing blanks. DATA statements are nonexecutable and
can be placed anywhere in the program.
A DATA statement can contain as many constants as will fit on a line, and any number of DATA statements can be used in a
program. The READ statement accesses
the DATA statements in line-number order. The variable type (numeric or string( must agree with the corresponding constant in the
DATA statement or a "Syntax error" occurs. You may only use a
:REM to add remarks to DATA statements. Use the RESTORE statement to reread
information from any line in the list of DATA statements. |
|
|
|
|
Example: |
See examples under the READ
statement. |
DIM
Statement
![]()
|
Purpose: |
Specifies the maximum values for
array variable subscripts and allocates storage accordingly. |
|
|
|
|
Format: |
DIM variable(subscripts)[,variable(subscripts)]… |
|
|
|
|
Remarks: |
variable is the name used for the array. subscripts is a list of numeric expressions,
separated by commas, which define the dimensions of the array. When executed, the DIM statement sets all the elements of the specified numeric arrays to an initial value of zero. String array elements are all variable length, with an initial value of null value (zero length). The first array subscript is zero
unless changed by using the "OPTION BASE 1" statement which changes
the first array subscript to 1. This
must be done before the array is dimensioned. |
|
|
|
|
Example: Example: |
This creates an array named XRAY that
has 10 subscripts, 0 through 9. 10 DIM XRAY(9) This creates a two-dimensional array
named TOAD that has 10 rows (0 through 9) and 10 columns (0 through 9). 20
DIM TOAD(9,9) |
END
Statement
![]()
|
Purpose: |
Terminates program execution, closes
all files, and returns to the command level. |
|
|
|
|
Format: |
END |
|
|
|
|
Remarks: |
END statements can be placed anywhere
in the program to terminate execution.
An END statement at the end of a program is optional but is always
good practice. |
|
|
|
|
Example: |
This example ends the program after
all the instructions have been executed. 100 PRINT "This is an example of
an END statement: 199 END |
FOR and NEXT
Statements
![]()
|
Purpose: |
Performs a series of instructions in
a loop a given number of times. |
|
|
|
|
Format: |
FOR variable = x TO y [STEP z]
.
.
. NEXT [variable [,variable]…] |
|
|
|
|
Remarks: |
variable is an integer variable used as a counter. x is a numeric expression that
is the initial value of the counter. y is a numeric expression that
is the final value of the counter. z is a numeric expression to be
used as an increment. The default is 1
if omitted. The program lines following the NEXT
statement are executed in order until the NEXT statement is encountered. Then the counter is incremented by the
amount specified by the STEP value (z). A check is performed to see if the counter
is now greater than the
maximum value in y. If the counter is greater than the maximum
then the statement following the NEXT is executed else the loop is re-started
at the statement following the FOR. The NEXT statement in the form: NEXT
var1, var2, var3,… is equivalent to the sequence of
statements: NEXT var1 NEXT var2 NEXT var3
etc. |
|
|
|
|
Example: |
This example shows a loop that
executes 10 times. 10
FOR T =
1 TO 5 20 PRINT T
* 2; 30
NEXT T 40
PRINT "The end" RUN 2
4 6 8
10 The end |
GOSUB and RETURN
Statements
![]()
|
Purpose: |
Branches to and returns from a
subroutine. |
|
|
|
|
Format: |
GOSUB
line number at the REM
statement . . REM "Start of some subroutine"
. . RETURN |
|
|
|
|
Remarks: |
line is the line number of the first line in
some subroutine. RETURN causes control to be
transferred back to the statement following
the GOSUB statement.. |
|
|
|
|
Example: |
This example the GOSUB at line 10
calls the subroutine at line 40. The
RETURN at line 50 causes control to go back to line 20. 10 GOSUB 40 20 PRINT "Back from
routine" 30 END 40 PRINT "Subroutine in
progress" 50 RETURN RUN Subroutine in progress Back from subroutine |
|
|
|
GOTO
Statement
![]()
|
Purpose: |
Branches unconditionally out of the normal program sequence to a specified
line number. |
|
|
|
|
Format: |
GOTO line |
|
|
|