Programming Languages
What is Basic?
Basic stands for Beginner's All-purpose Symbolic Instruction Code. Developed by John Kemeney and Thomas Kurtz in the mid 1960s at Dartmouth College, BASIC is one of the earliest and simplest high-level programming languages. During the 1970s, it was the principal programming language taught to students, and continues to be a popular choice among educators.
There is an ANSI standard for the BASIC language, but most versions of BASIC include many proprietary extensions. Microsoft's popular Visual Basic , for example, adds many object-oriented feature to the standard BASIC.
Recently, many variations of BASIC have appeared as programming , or macro, languages within applications. For example, Microsoft Word and Excel both come with a version of BASIC with which user can write programs to customize and automate these applications.
What is QBasic?
QBasic ("QuickBasic") is a language published by Microsoft. The current (and last) version is 4.5, and version 1.1 is distributed with DOS in place of GW-BASIC.
QBasic is extremely easy to use. Its math functions are especially good, and it's a great "learners language" for schools and other similar institutions. It resembles BASIC in its statements, but is more structured -and- flexible at the same time. For instance, line numbers are not required but can be used - as well as "line labels" with text names. The graphics support 256 color 320 x 200 VGA, and 16-color 640x480 VGA, as well as an array of other graphics cards.
QBasic can be expanded with the use of Quick Libraries (.QLB's) which add routines from Assembler and C. It can also be expanded through structured programming - the re-use of SUBroutines and functions. There are many possibilities. Some QLB examples include QBSVGA, a library which gives QB 4.5 SVGA capabilities - up to 1024x768 and 256+ colors, PLUS animation and sprite routines.
Tips
Did you know you can change the menu text in the Qbasic environment?
Type the following into the Immediate window:
POKE 696,65 |
Now open the File menu. You'll notice that the letter N of New has been changed to A. Now type the following in the Immediate window:
POKE 1686,65 |
Notice that the letter F of File has changed to an A.
Here is a program to try to find out where the menu titles & choices are:
addr=696 |
Feel free to keep experimenting with the menu system.
Mathematical tips
Try to use integers(% and #) instead of floating point(! and &) numbers, they are much faster.
When dividing integers use "a \ b" instead of "INT(a / b)"
If you can add numbers("a + a") instead of multiplying("a * 2"), adding is much faster.
Also multiplying("x * x") is faster than raising a number to a power("x ^ 2").
To find out if a number is odd do "x AND 1", if it returns 1 it is odd.
You could also do "x MOD 2" to find out if a number is odd, if it returns 1 it is odd.
To find out if a number is divisible by another number do "x % y", if it returns 0 x is divisible by y.
Instead of doing "b = x : x = y : y = b" to swap the numbers x and y, do "SWAP x, y"
5 impotent Datatypes
INTEGER
2-byte value in the range -32,768 through 32,767
LONG
4-byte value in the range -2,147,483,648 through 2,147,483,647
SINGLE
4-byte value with 7 digits of significance
DOUBLE
8-byte value with 15 digits of significance
STRING
A sequence of up to 32,767 characters
You can see above the different types of datatypes there are, but let's now look at a programming example. The following program returns some different results depending on the datatypes.
DIM number1 AS INTEGER |
Variable Names
A QBasic variable name can contain up to 40 characters (letters, numbers, and periods). In addition, you can append one of the following characters to the name to indicate a specific variable type:
% = Integer variable
& = Long integer variable
! = Single-precision variable
# = Double-precision variable
$ = String variable
Names reserved for Basic commands, functions, or operator names cannot be used as variable names. QBasic is not case sensitive. (For example, the variable names count and COUNT are identical to QBasic.)
To extend th e above theory, you could use these variable types like the below example.
number1% = 32767 |
So instead of actually specifying the type of datatype you would like to variable to as in the first example, you could use the technique in the second example which actually compacts your program.
ARRAYS
To create a QBasic array, use the following syntax:
DIM arrayname ([start_index TO] last_ index[, ?]) AS typename |
start_index TO last_index is the range of index values for the elements of the array. If you omit a starting index, QBasic uses the value 0 by default. (The OPTION BASE statement allows you to set the default starting index.) The three periods indicate that QBasic supports multidimensional arrays.
You can specify up to 60 dimensions. For example, the following statement creates a two-dimensional array with 3 rows and 5 columns:
DIM box (1 TO 3, 1 TO 5) AS INTEGER |
typename is the type of the array: INTEGER, LONG, SINGLE, DOUBLE or STRING. The maximum array size is 64KB. Valid index values range from -32,768 to 32,767.
Symbolic Constants
QBasic allows your programs to reference symbolic constants that you define with the CONST statement:
CONST size% = 255 |
QBasic constant names follow the naming conventions used for variables. Once you define a constant, you can use it throughout your program:
DIM a(size%) AS INTEGER |
In so doing, you simplify future changes to your program and improve the program's readability.
Label Names
For programs that don't use line numbers, QBasic allows you to use labels to reference specific locations in the program. A label name can contain up to 40 characters. Label names must begin with a letter and must end with a colon( : ). Names reserved for Basic commands, functions, or operator names cannot be used as label names, QBasic is not case sensitive. (For example, the label Handler: and HANDLER: are identical to Qbasic.)
Subroutines and Functions
The maximum QBasic subroutine size is 64 KB. You can pass up to 60 parameters to a subroutine. You can create subroutines and functions using the SUB and FUNCTION statements.
Data files
QBasic data files can be as large as the available space on your disk. You can use the file numbers 1 to 255. The largest possible record size for a random-access file is 32,767 bytes. The largest possible record number is 2,147,483,647.
Sorting: Bubble Sort
CONT FALSE = 0 |