Programming Languages

QBasic Tutorial - QBasic is a very simple language to pick up, and yet it can accomplish a great deal. Granted you will probably never write Doom or Word Perfect with QBasic, but it has its strong points. One of them is to introduce people to programming without having to worry about the internal workings of the computer. It's simple to create games, business applications, simple databases, and graphics. The best aspect of the language is its close resemblance to English.

TW Tech Glossary - Misplaced your bible? Well here it is! This truly took a while to complete and should be used by all from beginners to advance techies. Look into it, you won't be sorry. (Very Resourceful)

Software Engineering Phases - There are four fundamental phases in most, if not all, software engineering methodologies. These phases are analysis, design, implementation, and testing. These phases address what is to be built, how it will be built, building it, and making it high quality…

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
CLS
DO
value%=PEEK(addr) ' Ascii code
char$=CHR$(value%) ' The letter
PRINT "Address: ";addr,"Character";char$
PRINT "Press Esc to quit, or another key to continue."
kbd$=INPUT$(1)
addr=addr+1
LOOP UNTIL kbd$=CHR$(27)

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
DIM number2 AS INTEGER
DIM yourname AS STRING
DIM number3 AS LONG
DIM number4 AS SINGLE
number1 = 32767
number2 = 43
yourname = "Freddy Bloggs"
number3 = 1443233
number4 = 23435667
PRINT number1
PRINT number2
PRINT yourname
PRINT number3
PRINT number4

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
number2% = 43
yourname$ = "Freddy Bloggs"
number3& = 1443233
number4! = 23435667
PRINT number1%
PRINT number2%
PRINT yourname$
PRINT number3&
PRINT number4!

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
CLS
DIM a(100)
LOCATE 1, 1
FOR x = 1 TO UBOUND(a)
a(x) = INT(RND * 100) + 1
PRINT a(x);
NEXT
Limit = UBOUND(a)
DO
Switch = FALSE
FOR x = 1 TO (Limit - 1)
IF a(x) > a(x + 1) THEN
SWAP a(x), a(x + 1)
Switch = x
END IF
NEXT x
' Sort on next pass only to where the last switch was made:
Limit = Switch
LOOP WHILE Switch
LOCATE 10, 1
FOR x = 1 TO UBOUND(a)
PRINT a(x);
NEXT
Creating Delays in Qbasic
The SLEEP statement suspend the program execution for a specified time measured in seconds. If you want to use a more accurate delay use the TIMER function. The procedure "delay" suspends your program execution in terms of milliseconds.
DECLARE SUB delay (duration!)
SUB delay (duration AS SINGLE)
tim = TIMER
DO
LOOP UNTIL (TIMER - tim + 86400) - (INT((TIMER - tim + 86400) / 86400) * 86400) > duration
END SUB
Monitoring how long a key has been pressed
This piece of code calculates the time a key is pressed until it is released.
DIM tim AS SINGLE
DIM b AS INTEGER, c AS INTEGER
DO
'Catch the spacebar
getkey$ = INKEY$
'The ASCII code for the spacebar is 32. Change it if you want to monitor another key.
IF getkey$ = CHR$(32) THEN
b = INP(&H60)
tim = TIMER
DO
c = INP(&H60)
v$ = INKEY$
LOOP UNTIL c <> b
PRINT "delay equals to : "; TIMER - tim
END IF
LOOP UNTIL getkey$ <> ""


Did You Know?

  • QBasic is found on the Win95 CD-ROM in the 'OTHER\OLDMSDOS' directory. All you have to do is copy QBASIC.EXE and QBASIC.HLP to your hard drive from this directory, and you're set!

  • In the '70s when Bill Gates and Paul Allen licensed their BASIC to M.I.T.S. for the Altair. This version took a total of 4K memory including the code and data used for a source code.

  • The first Basic considered to be a full language implemented on a microprocessor was Li Chen Wang's "Tiny Basic", which appeared in Dr. Dobbs.

  • Basic was the first product sold by Microsoft corporation, and also the first major case of software piracy - It was copied widely even before Microsoft made it available (Billy Boy Gates lost track of a copy on paper tape during a computer show).



AlarmPlanet: Web's First Home Security Portal

3,500 HD Channels

Email Marketing on Sterioids


Links