Each keyword in MiniBasic is listed alphabetically, with a brief description of its function and how to use it.
Each entry
e - Euler's number.
The mathematical constant e, 2.71281828... The base of natural logarithms, and used in many formulae.
Usage
e
10 LET y = POW(e, -x)
Calculates the inverse cosine of a number. The result is in radians. Input must be between -1.0 and 1.0.
Usage
num = ACOS(numeric)
10 LET rad = ACOS(x)
Used in IF ... THEN statements to perform two tests. If both are true then the test succeeds. Note it cannot be used as a bitwise AND operator, as in some programming languages.
Usage
IF relational AND relational THEN numeric
10 IF name$ = "Fred”=" AND age > 18 AND age < = 65 THEN 100
Calculates the computer's internal code for the first character in a string, or 0 if the empty string is passed. It is useful for performing direct manipulations on the representation, for instance testing for newlines (code 13).
Usage
num = ASCII( string )
LET x = ASCII("*abc")
x now contains the code for an asterisk, or 42.
Calculates the inverse sine of a number. The output is in radians. The input must be between -1.0 and 1.0
Usage
num = ASIN(numeric)
10 LET rad = ASIN(x)
Calculates the inverse tangent of a number. The output is in radians. Note that for very extreme values accuracy may be lost.
Usage
num = ATAN(numeric);
10 LET rad = ATAN(x)
Converts the computer's internal numerical character code to a MiniBasic string of one letter. It is useful for performing numerical manipulations with the code. For instance, to insert a newline call CHR$(13).
Usage
str = CHR$(numeric)
X$ now contains "Line 1" and "Line 2" separated by a newline character.
10 LET X$ = "Line 1" + CHR$(13) + "Line2"
Calculates the cosine of an angle. The input must be in radians.
Usage
num = COS(numeric)
10 LET x = COS(degrees/180 * PI)
Use DIM to create a named list of numbers or strings. This is extremely useful when dealing with large amounts of data. For instance, if you a writing a program for a company with several employees, you can DIM an array to hold all their names.
Arrays can have up to five dimensions. In practise, even on modern computers, memory fills up very fast with big arrays, and three dimensions is the maximum recommended.
There must be no space between the name of the dimensioned variable and the opening parenthesis.
10 DIM name$(100)
Creates an array of 100 names.
10 DIM map(width, height)
Creates a 2d array of width * height entries, maybe representing grid squares on a map.
Array elements are in the range 1 - maximum, so
20 LET map(1,10) = 2.0
sets the top left element to 2.0. map(width, height) is the bottom
right element. If you try to access out-of-range elements the computer
will throw an error.
MiniBasic allows you to resize an array at any point by calling DIM on it again. If the array is one-dimensional, elements will be preserved. If the array has higher dimensions then the elements will be scrambled. Resizing an array is useful if, say, you are inputting a list of employee names and don't know how many there will be. Arrays of zero dimensions may not be declared.
MiniBasic also allows you to initialise arrays when you dimension them. This
10 DIM days$[7] = "Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"
will declare an array of days of the week. This method is useful for defining data. For 2d arrays, the first dimension is the lowest (x) dimension, so
DIM name$(2, 4) = "Fred", "Bloggs",
"Joe", "Sixpack"
"Homer", "Simpson"
"John", "Doe"
is the correct order.
Dimensioned variables are intimately connected with FOR ... NEXT loops. Use the loop counter to index into your array.
Usage
DIM id(numeric, numeric)
Creates a single-dimensioned array of 10 numerical elements
10 DIM array(10)
10 DIM dictionary$(2, N)
Creates a 2-dimensional array of N * 2 strings
10 DIM factorial(10) = 1!, 2!, 3!, 4!, 5!, 6!, 7!, 8!, 9!, 10!
Creates a list of the first ten factorials
FOR ... NEXT loops are extremely useful in programming. The FOR statement consists of three parts, the initial set-up value, a TO value, and an optional STEP value.
10 DIM array(100)
will input a hundred values into the array. The variable in the
NEXT statement must be the same as that in the matching FOR.
20 FOR I = 1 TO 100
30 INPUT array(I)
40 NEXT I
FOR loops may be nested to a maximum depth of 32.
10 DIM chess(8,8)
The step value does not need to be 1, and may be negative.
For instance
20 FOR I = 1 TO 8
30 FOR J = 1 TO 8
40 chess(j,i) = 1.0
50 NEXT J
60 NEXT I
10 FOR I = 1 TO 10 STEP 2
20 PRINT I
30 NEXT I
40 FOR I = 10 TO 1 STEP -0.3
50 PRINT I
60 NEXT I
The initial, to, and step values are calculated once on entering the FOR loop, they are then constant.
10 LET x = 10
In MiniBasic, if the TO value is lower than the initial value
(or higher if the STEP value is negative) then the loop does not
execute. Control passes to the first matching NEXT.
20 FOR I = 1 TO x STEP x/5
30 PRINT I
35 REM Next line has no effect
40 LET x = x + 1
50 NEXT I
It is important not to jump out of FOR ... NEXT loops, or get the nesting order wrong, otherwise MiniBasic's control flow will become confused. The "Too many FORs" error is likely to be caused by jumping out of a loop. To terminate a loop prematurely, set the counter to the TO value, and jump to the matching NEXT
Usage
FOR id = numeric TO numeric STEP numeric
...
NEXT id
GOTO executes a jump to another line. The line number is usually a constant, but GOTO x is supported.
GOTO is not considered good programming practise, but is essential in MiniBasic because flow control is so simple.
Usage
GOTO numeric
10 GOTO 100
10 GOTO x
The IF ... THEN construct allows a MiniBasic program to make decisions. It can emulate any other control structure.
If the test condition is true, then control jumps to the line indicated after the THEN keyword. If false, control passes to the next line. No statements other than a line number may appear after the THEN keyword, though the form
IF y < 10 THEN x
is supported
The relational operators are =, <> (not equal) , >, >=, < and <=. They can be applied to strings or to numerical expresions.
The AND and OR logical operators can also be used. MiniBasic does not perform lazy evaluation - all expressions will be evaluated so all array indices etc must be legal.
Usage
IF relational THEN numeric
10 IF x < 10 THEN 100
10 IF a$ <> "OK" AND a$ <> "YES" THEN x
To input data, use the INPUT function. In a test environment this will usually be typed by the user, if MiniBasic is a component of another program input will be provided by caller.
INPUT x
inputs a variable. Any non-numerical characters are skipped over until a number appears.
INPUT n$
inputs a string. Characters are read up to the first newline, which is discarded.
Most consoles provide data a line at a time, so input you type will not be available until you press ENTER.
If the input stream comes to an end, the program will fail with an error message.
Usage
INPUT id
10 INPUT x
20 PRINT x
Looks for occurrences of a substring within a string. The first argument is the string to search, the second argument the string to search for, and the third argument the position at which to start. The return value is 0 if the string is not found, or else the offset of the first occurence.
For instance
x = INSTR("zigzag", "zag", 1)
will return 4
It is very useful for string manipulation. For instance, to test if the one-character string ch is a digit we could write
10 IF INSTR("0123456789", ch, 1) <> 0 THEN 100
Usage
num = INSTR(string, string, numeric)
10 LET x = INSTR(sentence, "and", 1)
All MiniBasic numerical variables are stored as floating point. INT() returns the lower integer potion of the number. Thus INT(1.9) = 1.
To round, call INT(x + 0.5)
.
INT() is also useful for getting rid of small errors caused by floating-point calculation.
Usage
num = INT(numeric)
10 LET x = INT(x/2)
To take the leftmost characters of a string, call LEFT$. If the string is too short to contain that number of characters, it returns the whole string.
Usage
str = LEFT$(string, numeric)
10 LET hello$ = LEFT$("Hello World", 5)
To find the length of a string in characters, call LEN(). The empty string "" returns 0. It is often necessary to examine each character of a string for processing.
5 REM PRINT A$, omitting the letter "x"
Usage
6 INPUT A$
10 FOR I = 1 TO LEN(A$)
20 LET ch$ = MID$(A$, 1, 1)
30 IF ch$ = “x” THEN 50
40 PRINT ch$;
50 NEXT I
60 PRINT
num = LEN(string)
The LET statement assigns a variable a value. If the variable does not exist it is created.
10 LET x = 10
10 LET name$ = fname$ + " " + sname$
Plain variables like x or length are always numerical, string variables like name$ always end with a dollar sign. It is illegal to try to assign a variable of the wrong type.
LET will not create or increase the size of a dimensioned variable
10 DIM array(2,2)
The form
15 REM Legal
20 LET array(1,2) = 10
25 REM Illegal out of bounds
30 LET array(1,3) = 0
10 LET x = x + 1
is legal and is often very useful. It is even legal is x has not been created (it is initialised to zero).
Usage
LET id = numeric
LET id$ = string
10 LET x = 10
10 LET x = x + 1
10 LET a$ = CHR$(13)
Computes the natural logarithm of a number, which must be greater than zero. Natural logarithms are to the base e.
To convert to a base 10 logarithm,
LET log10 = LN(x)/LN(10)
To convert to a base 2 logarithm
LET log2 = LN(x)/LN(2)
Usage
num = LN(numeric)
10 LET log = LN(x)
Use this function to obtain a string from the middle of another string.
The first argument is the target string, the second argument the offset (1 - based) and the third argument the length of the substring to extract.
LET x$ = MID$("Distraction", 4, 5)
sets x$ to "tract"
If the length is too long for the target string, the answer is truncated.
By passing -1 for the length, we tell the function to extract the remainder of the string.
LET x$ = MID$("Distraction", 4, -1)
sets x$ to "traction".
Usage
str - MID$(string, numeric, numeric)
10 LET x$ = MID$(y$, 3, 4)
10 LET x$ = MID$(y$, 3, -1)
Modulus is not a function but an arithmetical operator. It calculates the remainder after division.
LET x = 12 MOD 5
sets x to 2.
Both sides of the MOD operator should be of the same sign. MOD 0 is an error.
MOD also works for fractional values. 0.75 MOD 0.5 equals 0.25
Usage
num = numeric MOD numeric
10 LET X = Y MOD 10
10 LET X = Y MOD -0.1
For description see FOR
Usage
NEXT id
10 FOR I = TO 10
20 PRINT I
30 NEXT I
Used in IF ... THEN statements to perform both tests. If either one is true, then the test succeeds and the jump is taken.
IF job$ = "caretaker" OR age < 65 THEN 100
Note that lazy evaluation is not performed. Both sides of the expression will always be evaluated.
When used in conjunction with AND use parentheses to disambiguate.
Usage
IF relational OR relational THEN numeric
10 IF x = y OR x = z OR x < 0 THEN 100
The mathematical constant PI, or 3.14159265... This is the ratio of a circle's circumference to its diameter and is used in many mathematical formulae
Usage
PI
10 LET area = radius * radius * PI
POW() raises x to the power y. Fractional and negative powers are supported.
LET x = POW(10, 2)
Will set x to 100.
By passing 1/y as the exponent, we can obtain the yth root of x. For example, to obtain the cube root of two pass
LET x = POW(2, 1/3)
Passing a negative power calculates the reciprocal. For example
LET y = POW(x, -3)
sets y to 1/(x^3)
Some values are illegal. For instance, POW(-1,1/2) will produce an indefinite result.
Usage
num = POW(numeric, numeric)
10 LET a = POW(x,y)
All of MiniBasic's output is via the PRINT statement. It is used to print both numbers and strings.
PRINT "Hello World"
Will output the string "Hello World", followed by a newline.
PRINT x
Will print the value of x in a human-readable format.
It is possible to print many values in one line by separating them with commas.
PRINT"Your salary is", x, "Mr" name$
The comma will automatically insert a space.
To suppress the newline, terminate the PRINT statement with a semicolon.
10 LET x = 10
will output the string "1025"
20 LET y = 25
30 PRINT x;
40 PRINT y
To print a bare newline, use the empty string
10 PRINT ""
Usage
PRINT numeric or string, numeric or string ; (optional)
10 PRINT "Hello World"
10 PRINT x
10 PRINT "Hello", name$
10 PRINT "Enter your telephone number";
10 PRINT ""
This statement is purely for adding comments to programs so that a human reader can understand them. It is also frequently used for "commenting out" code - prefixing with a REM so it is not executed.
MiniBasic allows for multi-line comments, as long as the first character of every continued line is a space.
Usage
REM any comments
10 REM Demonstration program by Malcolm McLean
10 REM This is an extremely long comment, which is spread over two
10 REM PRINT "This PRINT statement is commented out"
This is the twin to LEFT$. It takes the rightmost characters of a string.
For instance
LET A$ = RIGHT$("Beholden", 3)
Would set A$ to = "den".
If the target string isn't long enough, all of the string is copied.
Usage
str = RIGHT$(string, numeric)
10 LET A$ = RIGHT$(B$, 10)
Many applications need random numbers. RND() provides a pseudo-random number generator. The argument, which should be an integer, tells RND() to generate a random integer in the range 0 to N -1
10 FOR I = 1 TO 100
20 PRINT RND(10)
30 NEXT I
will output a stream of random digits in the range 0 - 9.
If we pass RND() the value 1 the number generated is a floating point value in the range 0 - (slightly below) 1.
The random number generator is deterministic. To force a certain behaviour, call RND() with a negative argument. This will "seed" the random number generator.
LET dummy = RND(-10)
will give us numbers based from the seed 10
Calling RND(0) will always return 0.
Usage
num = RND(numeric)
10 LET die = RND(6) + 1
10 LET dummy = RND(-10)
10 LET p = RND(1)
Returns the sine of a number. The argument must be in radians.
Usage
10 LET s = SIN(theta)
10 LET s = SIN(degrees/180 * PI)
Calcualates the square root of its argument, which must be positive.
Usage
num = SQRT(numeric)
10 LET root2 = SQRT(2)
10 LET dist = SQRT( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2))
STEP is by default 1, but can be any value, positive or negative. It is evaluated once when the FOR ... NEXT loop is entered. For further details see FOR
Usage
FOR id = numeric TO numeric STEP numeric
10 FOR i = 1 TO 100 STEP 10
10 FOR i = 100 TO 1 STEP -1
10 FOR i = min TO max STEP delta
The numerical value x = 10 and the string value x$ = "10" are two different things. To convert a number into a human-readable string use STR$
Usage
str = STR$(numeric)
10 LET reg$ = "ABC" + STR$(num)
If we need to create a string that consists of a shorter string duplicated many times, use STRING$.
LET stars$ = STRING$("*", 20)
will create a string of twenty asterisks. A common use is creating variable numbers of spaces for output formatting.
Usage
str = STRING$(string, numeric)
10 PRINT STRING$(" ", 10), out$
Calculates the tangent of an angle, which must be in radians.
Usage
num = TAN(numeric)
10 LET x = TAN(theta)
10 LET x = TAN(degrees/180 * PI)
THEN introduces the jump destination which is taken if the expression in the IF statement is true. The expression is always evaluated, even if the branch is not taken.
For further details see IF
Usage
IF relational THEN numeric
In a FOR ... NEXT loop, TO introduces the terminal value. When it is exceeded, the loop terminates at the next NEXT statement.
For further details see FOR
Usage
FOR id = numeric TO numeric
This function converts a human-readable string containing numbers to a numerical variable. The string may contain numbers in scientific notation e.g. 1.5e20.
The string is read up until the first non-numerical character is encountered. If the string does start with a number, 0 is returned
Usage
num = VAL(string)
10 LET x = VAL("1024")
10 LET x = VAL("1.5e20")
This function is designed for use with VAL() to tell the caller how many numerical characters were translated. This is useful if stepping through a string containing many numbers. It also tells the caller whether a string is numerical or not - if non-numerical it returns 0.
10 INPUT a$
20 IF VALLEN(a$) <> 0 THEN 50
30 PRINT "You must enter a number"
40 GOTO 10
50 LET x= VAL(a$)
60 LET a$ = MID$(VALLEN(A$), -1)
This code will read a number from the input, and prompt is valid input is not entered.
Usage
num = VALLEN(string)
10 LET slen = VALLEN("121 dalmations")