Every line starts with a command, or a line number followed by a command.
PRINT "Hello world"
Line numbers are optional and allow us to do control flow like subroutines and goto statements (more on this later).
10 PRINT "hello" GOTO 10
Each basic command has its own syntax.
REM allows us to add comments to our programs. Comments are for you or other people to read. Computers ignore them.
REM this is a comment
LET lets us declare variables. Variables are single letters that hold values. Values could be either strings or numbers. Note that
LET is optional and can be left out of an assignment expression.
Example using a number:
10 LET X = 10 20 PRINT X
Example using a string:
10 LET X = "hello" 20 PRINT X
10 X = "hello" 20 PRINT X
ARRAY lets us declare an array. Arrays can be thought of as lists of values.
10 ARRAY a 20 a = "car" 30 a = "bus" 40 a = "bike" 50 print a
If we want to create a multi-dimensional array, which is an array of arrays, we can declare the array with a dimension:
10 ARRAY a, 2 15 REM the first array is ground transportation 20 a = "car" 30 a = "bus" 40 a = "bike" 45 REM the second array aerial transportation 50 a = "plane" 60 a = "helicopter" 70 a = "jetpack" 80 print a
INPUT lets you communicate with the program by typing into the console. Whatever you typed will be stored into a variable that you can then use in your program.
10 INPUT "type your name: "; A 20 PRINT "Hello " + A
END ends the program.
10 END 20 PRINT "We never reach this statement"
GOTO advises which line number is executed next. Normally, lines are executed from the lowest to the highest number, but
GOTO allows us to jump to a specific line.
10 GOTO 30 20 PRINT "This line never executes" 30 PRINT "Jumped here from 10"
GOTO can be used to create a loop. Loops are when programs repeatedly execute a set of lines.
The following examples prints "hello" forever:
10 PRINT "hello" 20 GOTO 10
IF...THEN is like
GOTO in that it influences the execution of the program, but it's different in that it all happens on the same line. If the mathematical or relational test is true, we execute the command that comes after
THEN. If it's untrue, we simply proceed to the next line.
10 IF X > Y THEN PRINT "X is larger than Y"
This is the same as
IF...THEN except we execute the command following the
10 IF X > 0 THEN PRINT "X is positive" ELSE PRINT "X is negative"
It's often useful to repeatedly execute a number of lines. This is called "looping", and
FOR statements allow us to create loops between ranges of numbers.
For example, we can print numbers from 1 to 10:
10 FOR I = 1 TO 10 20 PRINT I 30 NEXT I
1 2 3 4 5 6 7 8 9 10
But what if we only want to print even numbers between 1 and 10? This is where
STEP comes in.
STEPS tells the computer how much to add to the variable (in this case
I) with each loop.
10 FOR I = 2 TO 30 STEP 2 20 PRINT I 30 NEXT I
2 4 6 8 10
Remember to call
NEXT with the variable name after the last line of the loop.
A subroutine is a group of statements that you wish to use repeatedly in a program. They're similar to loops in that they can be executed repeatedly, but you have to use
GOSUB to call (move the program to) the subroutine's first line number. After the subroutine finishes executing, you can use
RETURN to go back to where you used
10 GOSUB 40 20 PRINT "This is the end of the program" 30 END 40 PRINT "This is the start of the subroutine" 50 PRINT "We can easily call it as many times" 60 PRINT "as we'd like" 70 RETURN
PLOT lights a pixel with x, y coordinates on the display with a certain color.
10 PLOT 0, 0, "red"
Colors can be any of the these colors.
DISPLAY changes the display size (rows, columns) and lets us turn off pixel borders.
DISPLAY 100, 100, FALSE
DISPLAY will affect the values of the constants
DISPLAY 50, 100, FALSE PRINT ROWS PRINT COLUMNS
DRAW is like
PLOT except it lights up multiple pixels on the display. It takes a two-dimensional array of colors.
10 ARRAY a, 2 20 a = "red" 30 a = "yellow" 40 DRAW a
TEXT draws text on the display at an x, y coordinate. Optional text size and color parameters can be passed in.
10 TEXT 0, 0, "hello world", 25, "red"
Colors can be any of the these colors.
PAUSE pauses the program for a number of milliseconds. Milliseconds are 1/1000th of a second.
10 PRINT "pause for a second" 20 PAUSE 1000 30 PRINT "end"
CLS clears the console and the display.
CLT clears the console.
CLC clears the display.
SOUND lets us play a frequency for a duration in seconds.
10 SOUND 400, 4
Duration is optional and will default to 1 second.
PLAY lets us play a note in an octave for a duration in seconds.
10 PLAY "C", 4, 5
C C# D D# E F F# G G# A A# B
You can think of functions as built-in subroutines you can call. It's important to differentiate between functions and commands. Lines should start with commands, while functions can be used as part of other commands but not on their own.
ABS returns the absolute value of a number. The sign of the number will always be positive after this function is executed.
10 PRINT ABS(-11) 20 PRINT ABS(11)
COS returns the trigonometric cosine of a number.
10 PRINT COS(1)
SIN returns the trigonometric sine of a number.
10 PRINT SIN(1)
TAN returns the trigonometric tangent of a number.
10 PRINT TAN(1)
ATAN returns the trigonometric arctangent of a number.
10 PRINT ATAN(1)
EXP returns Euler's number (e) raised to the power of a number.
10 PRINT EXP(2)
INT returns the lowest closest integer of a number.
10 PRINT INT(2.6)
ROUND rounds a number to the closest integer.
10 PRINT ROUND(2.6)
LOG returns the natural logarithm of a number.
10 PRINT LOG(5)
SGN returns the sign of a number. The sign is +1 if the number is positive, 0 if the number is 0, and -1 if the number is negative.
10 PRINT SGN(-23)
SQR returns the square root of a number.
10 PRINT SQR(25)
VAL converts a string to a number, and
0 if it cannot be converted.
10 PRINT VAL("33")
RND returns a random number between 0 and 1.
10 PRINT RND()
If a number is placed between the parenthesis, then the command will return a random number between 1 and that number.
10 PRINT RND(10)
ASC returns the ASCII representation of a letter.
10 PRINT "s"
LEFT returns the first
n number of letters from a string.
10 PRINT LEFT("basic", 2)
MID returns a substring as defined by a starting and ending position in the string.
10 PRINT MID("basic", 1, 2)
RIGHT returns the last
n number of letters from a string.
10 PRINT RIGHT("basic", 2)
CHR returns the ASCII letter from a number.
10 PRINT CHR(115)
LEN returns the length of a string.
10 PRINT LEN("basic")
SPC returns a number of spaces.
10 PRINT "hello" + SPC(5) + "world"
UPPERCASE returns the uppercase string.
10 PRINT UPPERCASE("basic")
LOWERCASE returns the lowercase string.
10 PRINT LOWERCASE("BASIC")
COLOR returns the color of a pixel at x, y coordinates.
10 PLOT 1,1,"red" 20 PRINT COLOR(1, 1)
GETCHAR returns a single character of user input. The program maintains a first-in-first-out queue of user inputs. If there are no user inputs in the queue, it will return an empty string
10 LET I = GETCHAR() 20 IF I = "" THEN PRINT "no input" else PRINT "input: " + I
GETCLICK returns an array of x, y coordinates of user mouse clicks. The program maintains a queue of clicks. If there are no user clicks in the queue, it will return an empty string
TIME returns the current milliseconds elapsed since the UNIX epoch.
10 PRINT TIME()