BASIC Statements - Bricksnspace/MegaBASIC GitHub Wiki

Index

Operators

Operators supported:

  • Arithmetic: + - * / MOD and unary minus
  • Logical: AND OR NOT
  • Comparison: = <> (not equal) >= <= < >
  • String: + (concatenation)
  • Precedence: ( )

Variables and assignment

Variables are eight alphanumeric character identifiers, starting with a letter:

a
val42
Longname

All names are case insensitive:

varname
VARname
VarName
VARNAME

refers all to same variable.

There are four variable type:

  • numeric A=12,34
  • string A$="Hello!"
  • numeric array A(2)=42
  • string array A$(3)="Third string" String variables contains any number of characters and is identified by dollar sign after variable name.

To assign a value to variable:

LET variable = <expr>
variable = <expr>

LET can be omitted.

CLS

Clears screen and move cursor to first row, first column.

CONT

Continue execution after a STOP statement. If program was interrupted using Esc key can't resume execution.

DIM

Declares an array.

10 DIM a(100)
20 DIM a$(5)
30 DIM matrix(5,5)

Line 10 declares an array of 100 numbers. Line 20 declares an array of 5 strings. Line 30 declares an array of 5x5 numbers.

Array index starts at 1 (I know, I know...)

FREE

Prints free program memory in bytes.

FOR ... NEXT

Executes a loop for a defined number of times.

100 FOR var=startValue TO endValue STEP stepValue
...
...
200 NEXT var

Assign startValue tor var, adding stepValue at the end of every loop, exiting when var is greater to endValue, or when var is less than endValue if stepValue is negative. If STEP stepValue is missing, default value of 1 is used.

Examples:

200 FOR i=1 TO 200
...
230 NEXT i

Executes exactly 200 loops, and:

20 FOR i=20 TO 0 STEP -1
...
50 NEXT i

executes 21 loops.

GOTO

Jumps to line.

220 GOTO 120

GOSUB ... RETURN

Calls a subroutine, e.g. a code that can be called from different part of program.

230 GOSUB 500
...
...
500 REM Asks for value
510 PRINT "Enter an int:"
520 INPUT n
530 RETURN

IF ... THEN

Checks a condition and execute all the statement after THEN

420 IF a=0 THEN GOTO 500
...
470 IF c<>0 THEN c=42:GOTO 230

In line 470 if c is not zero assign 42 to c and jumps to line 230.

INPUT

Accept data from user.

INPUT variable
200 PRINT "Value for A:"
210 INPUT a

LIST

Print program listing on display.

LIST [start][,end]

If you specify a line number, LIST will print program lines starting at that number:

LIST 100
100 PRINT "Hello!"
110 FOR i=0 TO 100
120 PRINT i
130 NEXT i
...

You can also specify a second number, that limit listing up to that:

LIST 100,120
100 PRINT "Hello!"
110 FOR i=0 TO 100
120 PRINT i

NEW

Deletes program and data from memory, preparing system for a new program. Deleted program cannot be recovered.

PAUSE

Stop execution for given milliseconds. It uses Arduino delay() function.

40 PAUSE 2000

Stops for two seconds.

PIN

Write a logical value on Arduino pin.

30 PIN 12,0
40 PIN 7,3

Line 30 write a low level on pin 12. Line 40 write an high level on pin 7. Any non-zero value is defined as logical '1'

PINMODE

Define Arduino PIN configuration.

PINMODE 4,1

set pin 4 as OUTPUT. Accepted values are:

  • 0 - pin set as INPUT
  • 1 - pin set as OUTPUT
  • 2 - pin set as INPUT_PULLUP

POSITION

Move cursor to column,row.

30 POSITION 4,6
40 PRINT "X"

Print a capital x on row 6, column 4.

PRINT

Output characters and number to display.

PRINT <expr>;<expr>... e.g. PRINT "A=";a

Use semicolon (;) to separate elements that goes on same line:

PRINT "PI=";3.1415

writes:

PI=3.1415

Use semicolon at end end of line to avoid cursor jumping on newline:

PRINT "PI=";
PRINT 3.1415

writes:

PI=3.1415

REM

A comment. Line is ignored.

230 REM this is a comment. 
240 REM This line is ignored

RUN

Executes a program.

RUN [lineNumber]

If you specify a line number, program starts running from that line.

To stop program, press 'Esc' key.

STOP

Stop program execution.

100 PRINT "STOP here"
110 STOP
120 PRINT "Resume running"

To resume execution use command CONT from prompt.

LOAD (from internal EEPROM) SAVE (to internal EEPROM) e.g. use SAVE + to set auto-run on boot flag LOAD "filename", SAVE "filename, DIR, DELETE "filename" if using with external EEPROM.

"Pseudo-identifiers"

INKEY$ - returns (and eats) the last key pressed buffer (non-blocking). e.g. PRINT INKEY$ RND - random number betweeen 0 and 1. e.g. LET a = RND

⚠️ **GitHub.com Fallback** ⚠️