Author Topic: Quite BASIC  (Read 3312 times)

JRS

  • Guest
Quite BASIC
« on: September 22, 2010, 04:58:59 AM »
Here is another web Basic that adds a graphics canvas. (site is meant to teach programming to kids but fun for adults as well.)

http://www.quitebasic.com/

Quite BASIC Help

If you ever programmed classic BASIC it should be easy to get started. Just type your BASIC program in the 'Basic Program' text field and then click on 'Run".

If you are the type that learns from examples, take a look at the preloaded sample BASIC program and the projects in the "Projects" menu. You'll see examples of all available BASIC commands and many of the functions.

Classic BASIC syntax

Quite BASIC is classic BASIC, with line numbers and all.  There is a line number and one command per line.  Line numbers are integers between 1 and 9999. To get that classic look you can use all upper case for commands like:
100 PRINT "Hello world!"
But the commands are actually not case sensitive, so you could just as well write:
100 print "Hello world!"
if you like that better.

Expressions in classic BASIC are similar to modern programming languages, but there are a few notable differences.  Not equal is written <> and applies to numbers as well as strings.  Testing for equality and assignment are both written with a single equal sign, =.  Here are examples of how to print a few expressions:
100 PRINT 5 + 3
100 PRINT "Hello" + " " + "world!"
100 PRINT 5 % 3
100 PRINT SIN(3 * PI)
and here's an expression in an assignment:
100 LET X = 5 + 3
and in a conditional:
100 IF A = B THEN GOTO 500

Operators in expressions are the usual: +, -, /, and *.  The modulo operator % is also supported. The plus sign can be used for numbers and for concatenating strings.

Constants

The constant PI has the value: 3.141593...

The parameter LEVEL is not strictly a constant. It is a read-only integer between 1 and 5 that is controlled by the user. You can use it to control the speed of a game for example. In that case you would use it in the program's main loop similarly to this:
100 PAUSE (6 - LEVEL) * 100

Functions

The common mathematical functions that you see on a standard calculator are supported.  They are used by simply writing the name of the function followed by the numeric argument enclosed by parentheses. For example:
100 PRINT SQR(2)
for the square root of two.

  • ABS(X)
  • SGN(X)
  • ASIN(X)
  • ASIN(X)
  • ATAN(X)
  • COS(X)
  • SIN(X)
  • TAN(X)
  • SQR(X)
  • EXP(X)
  • LOG(X)
In addition there are two functions for rounding and flooring a real number into an integer.
  • ROUND(X)
  • FLOOR(X)
And there is a random number generating function. It generates a random number between zero and X (the argument).
  • RAND(X)

This wouldn't be classic BASIC without those string functions, but fear not, "LEFT", "RIGHT" and "MID" are all supported as well as "LEN". For backwards compatibility, functions may have a dollar sign appended (like so: MID$), but Quite BASIC variables are not typed so this has no meaning.

LEFT(string, n) -- returns the 'n' first characters for 'string'.  For example, try this program:
100 LET A = "ABC123"
200 PRINT LEFT(A, 3)

RIGHT(string, n) -- returns the 'n' last characters for 'string'.  For example:
100 LET A = "ABC123"
200 PRINT RIGHT(A, 3)

MID(string, start, n) -- returns 'n' characters starting at character 'start' (the first character is index zero).  For example, try this program:
100 LET A = "ABC123"
200 PRINT MID(A, 2, 3)

LEN(string) -- Returns the length of a string.

UPPERCASE(string) -- returns 'string' converted uppercase.

LOWERCASE(string) -- returns 'string' converted lowercase.

GETCHAR() -- Returns key strokes. Quite BASIC has a queue for key strokes, and GETCHAR picks them up in order until the queue is empty. Then GETCHAR returns an empty string.  Try this for example:
100 LET C = GETCHAR()
200 IF C <> "" THEN PRINT C;
300 GOTO 100

GETCHAR is mostly useful in games.  See for example the pong game (Projects->Games->Pong).

COLOR(x, y) -- returns the color of the pixel at position x, y. This is a string that is initially "gray". Try for examle:
10 PRINT COLOR(5, 3)
20 PLOT 5, 3, "pink"
30 PRINT COLOR(5, 3)

Variables

All variables are a single letter optionally followed by digits. For backwards compatibility, we also allow variable names to have a dollar sign appended to them.  Quite BASIC variables are not typed so the dollar sign has not meaning. So this is all OK:
100 LET A = "ABC123"
110 LET A1 = 17
120 LET A2 = 4711
130 LET A$ = "ABC123"
But this does not work:
100 LET ABC = "ABC123"
Because variables can only have one letter.

Arrays are supported, but before using it, you must declare a variable to be an array like this:
100 ARRAY A
Array indexing starts at zero, but there is not big harm in ignoring the zeroth element and use the array as if it starts at one.  You access array elements by appending the index in enclosed by brackets like this:
100 ARRAY A
110 LET A[4711] = "Hi!"
120 PRINT A[4711]

In classic BASIC, all variables are available at all program lines (aka "global scope"). This may sound innocent enough, but as your program grows bigger it can play tricks on you so be careful and make sure you are not accidentally using the same variable name for two different things.

Commands

All lines of a classic BASIC program have the same fundamental structure: a line number followed by a command and arguments to the command.  Quite BASIC is no different.  Most commands take one or more expressions as arguments and in this section those will be indicated by the abbreviation expr.

Rather than listing the commands alphabetically, the most fundamental commands are described first and then increasingly complex and specialized commands follow.

PRINT expr
Print output to the output area.  This can be as simple as:
100 PRINT "Hello world!"

PRINT expr;
Print output to the output area.  Note the semi colon.  It indicates that we want to continue printing more on the same line.  For example:
100 PRINT "Hello ";
110 PRINT "world!"

LET X = expr
Assignment.  Assign the value of an expression to a variable or array element.  Some examples:
100 LET A = "Hello world!"
110 LET B = 17
120 LET C[5] = "Number five"
130 LET D = COS(7 * PI)

ARRAY X
Declare (or designate) a variable to be an array.

IF expr THEN command
The IF command embeds a second command that is only executed if the first expression (called the conditional) is true.  For example:
110 IF 7 * 8 = 56 THEN PRINT "Yes"
will print "Yes", but:
110 IF 7 > 8 THEN PRINT "Yes"
will not print anything.

Right after THEN or ELSE a scortcut is allowed for GOTO commands. So this is allowed for example:
110 IF A=B THEN 500 ELSE 800

IF expr THEN command ELSE command
The IF command with both a THEN and an ELSE. If the conditional is true then the command following THEN is executed, otherwise the command following ELSE is executed.  For example:
110 IF A > 0 THEN PRINT "Yes" ELSE PRINT "No"

GOTO line
Ah, the trusty old GOTO command.  It simply means what it says: goto a new line in the program.  It is often combined with the IF command.  For example to avoid division by zero:
100 IF A = 0 THEN GOTO 120
110 LET A = 1 / A
120 PRINT A

In large computer programs a command like GOTO makes life hard for programmers.  In fact it has given classic BASIC quite a bad reputation.  But for small programs like the ones we are writing it is just fine.  And it is easy to understand too.

FOR I = expr TO expr
The FOR command is the classic BASIC loop command.  You use it if you want to do the same thing may times over.  Here's an example:
100 FOR I = 1 TO 10
110 PRINT I
120 NEXT I

Try it!  The program loops back to the FOR command ten times, each time incrementing the loop variable (I) by one.  The NEXT command is where the program jumps back.  Note that the loop variable is an argument to the NEXT command.  This is so the program knows which FOR loop (in case the program has more than one).

FOR I = expr TO expr STEP expr
The FOR loop with a step other than one.  For example you may want to loop over only odd numbers like this:
100 FOR I = 1 TO 9 STEP 2
110 PRINT I
120 NEXT I

GOSUB line
The classic BASIC subroutine.  The command GOSUB line makes the program jump to a new line in the program like the GOTO command, but there is an important difference.  The program remembers from where it jumped, and when it encounters a RETURN command, it comes back to where it started.  This way you can write reusable parts of your program (called subroutines).  Here is an example:
100 LET A = 17
110 GOSUB 1000
120 LET A = 50
130 GOSUB 1000
140 END
1000 PRINT A;
1010 PRINT " is an ";
1020 IF A % 2 == 0 THEN PRINT "even"; ELSE PRINT "odd";
1030 PRINT " number."
1040 RETURN

END
The END command simply stops the program.  You saw the use of the END command in the GOSUB example.

INPUT prompt; variable
The INPUT command prompts the user for input.  It assigns the user's input to the variable.  For example:
100 INPUT "Please enter your age"; A
110 PRINT "You are " + A + " years old."

PAUSE duration
The pause command pauses the program for a number of milliseconds. For example, the following example pauses for one second:
10 PAUSE 1000

PLOT X, Y
Plot a black point on the canvas.  The point on the lower left on the canvas is 0,0. You can change the size of the canvas on the View menu, and when you create a new project you can also select the canvas size.  The canvas is not exactly high resolution, but you'll be surprised how much fun you can have with it!  Take a look at the welcome project for an example of how to draw circles using the trigonometric functions.

PLOT X, Y, color
Plot in a given color.  Some available colors are: "red", "pink", "green", "blue", "yellow", "magenta", "cyan", "orange", "black", "white", "gray".  Try this for example:
100 PLOT 5, 5, "cyan"
The full set of available colors depends on your browser. WARNING: If you accidentally try to plot a color that doesn't exist, you will get a rather cryptic error (it could look like this: [Object error]) or worse, the program just stops without any message at all.  

REM remark
A remark.  Remarks (aka comments) don't have any effect on the program when it runs, but they can make the program much easier to read and understand.  It is good practice to sprinkle REM commands in the code explaining the meaning of variables and how the program works.  Example:
110 REM The variable A holds the user's age

CLS
Clear the output area from all text and the canvas from all graphics. No arguments, just:
100 CLS

CLT
Clear only the output text area. No arguments, just:
100 CLT

CLC
Clear only the canvas. No arguments, just:
100 CLC

« Last Edit: September 22, 2010, 05:06:54 AM by admin »

JRS

  • Guest
Re: Quite BASIC
« Reply #1 on: September 22, 2010, 05:50:00 AM »
I wanted to see how compatible ScriptBasic was with Quite BASIC. I converted the QB bubble sort program to SB with the goal of changing as little as possible to get it to run.

Code: Text
  1. 1000 REM Quite BASIC Computer Science Project
  2. 1010 REM Bubble Sort program
  3.  
  4. 1100 REM Initialize the array
  5. 1100 LET N = 10
  6. 1110 ARRAY A
  7. 1120 GOSUB 3000
  8. 1130 REM Print the random array
  9. 1140 PRINT "Random list:"
  10. 1150 GOSUB 4000
  11. 1160 REM Sort the array
  12. 1170 GOSUB 2000
  13. 1180 PRINT "Sorted list:"
  14. 1200 REM Print the sorted array
  15. 1210 GOSUB 4000
  16. 1220 END
  17.  
  18. 2000 REM Bubble sort the list A of length N
  19. 2010 FOR I = 1 TO N - 1
  20. 2020 FOR J = 1 TO N - I
  21. 2030 IF A[J] <= A[J+1] THEN GOTO 2070
  22. 2040 LET X = A[J]
  23. 2050 LET A[J] = A[J+1]
  24. 2060 LET A[J+1] = X
  25. 2070 NEXT J
  26. 2080 NEXT I
  27. 2090 RETURN
  28.  
  29. 3000 REM Create a random list of N integers
  30. 3030 FOR I = 1 TO N
  31. 3040 LET A[I] = FLOOR(RAND(100))
  32. 3070 NEXT I
  33. 3090 RETURN
  34.  
  35. 4000 REM PRINT the list A
  36. 4010 FOR I = 1 TO N
  37. 4020 PRINT A[I];
  38. 4030 PRINT ", ";
  39. 4040 NEXT I
  40. 4050 PRINT
  41. 4060 RETURN
  42.  

Code: Text
  1. ' 1000 REM Quite BASIC Computer Science Project
  2. ' 1010 REM Bubble Sort program
  3.  
  4. ' 1100 REM Initialize the array
  5. 1100 N = 10
  6. ' 1110 ARRAY A
  7. 1120 GOSUB 3000
  8. ' 1130 REM Print the random array
  9. 1140 PRINT "Random list:\n"
  10. 1150 GOSUB 4000
  11. ' 1160 REM Sort the array
  12. 1170 GOSUB 2000
  13. 1180 PRINT "Sorted list:\n"
  14. ' 1200 REM Print the sorted array
  15. 1210 GOSUB 4000
  16. 1220 END
  17.  
  18. 2000:
  19. REM Bubble sort the list A of length N
  20. 2010 FOR I = 1 TO N - 1
  21. 2020 FOR J = 1 TO N - I
  22. 2030 IF A[J] <= A[J+1] THEN GOTO 2070
  23. 2040 X = A[J]
  24. 2050 A[J] = A[J+1]
  25. 2060 A[J+1] = X
  26. 2070 NEXT J
  27. 2080 NEXT I
  28. 2090 RETURN
  29.  
  30. 3000:
  31. REM Create a random list of N integers
  32. 3030 FOR I = 1 TO N
  33. ' 3040 LET A[I] = FLOOR(RAND(100))
  34.      RANDOMIZE I * 100
  35.      A[I] = FIX(RND % 100)
  36. 3070 NEXT I
  37. 3090 RETURN
  38.  
  39. 4000:
  40. REM PRINT the list A
  41. 4010 FOR I = 1 TO N
  42. 4020 PRINT A[I]
  43. 4030 PRINT ", "
  44. 4040 NEXT I
  45. 4050 PRINTNL
  46. 4060 RETURN
  47.  


C:\scriptbasic\test>bubsort.sb
Random list:
65, 91, 18, 44, 71, 97, 24, 51, 77, 4,
Sorted list:
4, 18, 24, 44, 51, 65, 71, 77, 91, 97,

C:\scriptbasic\test>
« Last Edit: September 22, 2010, 06:12:16 AM by JRS »