Qbasic Shopping
QBasic
Articles about
Qbasic
Website Links For
Qbasic
 

Information About

Qbasic




QBasic (a name derived from QuickBASIC, BASIC being an acronym for '''B'''eginner's '''A'''ll-purpose '''S'''ymbolic '''I'''nstruction '''C'''ode) is a variant of the BASIC Programming Language . The source code is compiled to an intermediate form within the Integrated Development Environment (IDE), and this intermediate form is immediately Interpreted on demand within the IDE.


SYNTAX


Like QuickBASIC , but unlike earlier versions of Microsoft BASIC, QBasic was a Structured Programming language, supporting constructs such as named Subroutines and While Loop s. Line Number s, a concept often associated with BASIC, were supported for compatibility, but were not necessary and not considered good form, having been replaced by descriptive line labels. QBasic has limited support for user-defined data types ( Structure s), and several primitive types used to contain strings of text or numeric data.


HISTORY

It was intended as a replacement for GW-BASIC , and was shipped together with MS-DOS 5.0 and higher, including Windows 95 . QBasic was based on the earlier QuickBASIC 4.5 compiler but without QuickBASIC's compiler and linker elements.

Microsoft stopped shipping QBasic with later versions of Windows. Windows 98 users, however, will find it in the \TOOLS\OLDMSDOS directory of the CD-ROM; on the Windows 95 CD-ROM, it is in the \OTHER\OLDMSDOS directory. It is now only available from Microsoft's website for licensed users of MS-DOS. QBasic provided a state-of-the-art IDE (for its time), including a Debugger with features such as on-the-fly expression evaluation and code modification that were still relatively unusual more than ten years later.

QBasic was also the subject of several programming books for beginners.

QBasic is able to be run natively under nearly all versions of DOS and Windows, and by using the free DOSBox emulator, it can run on platforms such as Linux and FreeBSD .

QBasic came complete with a couple of pre-written example programs. These were Nibbles (a variant of the Snake game), Gorilla , an explosive-banana throwing game and RemLine, a GW-BASIC code line number removing program.


CODE EXAMPLE

The following code example shows some rudimentary QBasic functionality

DO
CLS 'This clears the screen. It's the default first line, as it gets rid of
'unwanted text and graphics. By the way, the apostrophe is a comment.
'Everything past it is ignored by QBasic, which is why I can say anything.

PRINT "Press escape to end the insane beeping! Muahaha!" 'PRINT prints text to the screen.
'The quotes tell Qbasic that the stuff
'inside is a ''string''. PRINT can be
'replaced by a question mark "?".

DO WHILE INKEY$ <> CHR$(27) 'Checks to see if the user is pressing "escape"; the
'ASCII value of that key is represented by "CHR$(27)",
'the ASCII value of escape is 27. "DO WHILE" creates a loop, saying "Do this until
'the user Whatever Comes After The "DO WHILE", In Our Case Presses Escape

  • 9000 + 100) '"LET"-This "declares" the variable. It's obsolete, so you could

  • 'just say "x% = Whatever ".

'"RND" generates a smallish random number. Multiplying it by 9000
'makes it a number between 1 and 9000. "+ 100" makes it between 101
'and 9100. "INT" makes it an integer (number without a
'decimal point) and the "%" tells QBasic it's an integer, rather
'than, say, a string.

SOUND x%, 1 'This makes a ''sound'', with its frequency (pitch) being x Hz . This number has to
'be between 36 and 32767, but the speaker can't produce an audible pitch of more than
'9000 or so. ", 1" says that the sound will last 1 '' Clock Tick ''. A clock tick in
'QBasic happens 18 times a second. So, this program will make some CRAZY beeps.

LOOP 'This ties in with the "DO WHILE" part. Without this, the "DO WHILE" won't know when
'stop and start over.

PRINT "Ok, I'll stop." 'Prints "Ok, I'll stop."
SLEEP 2 'Waits two seconds, then continues. Remove the "2" and it will wait for a
'keypress. Note that different functions use different time units.

INPUT "Beep again? (y/n , then hit enter, n is default)", beep$ 'Checks to see what the user types
'before they hit enter, and stores it in a
'variable ("beep")

LOOP WHILE beep$ = "y" OR beep$ = "Y" 'Tells the program to goto the start, if the users types "y"



The following code illustrates some of the structured programming features of QBasic, such as Sub-routines and functions, and loops, and some of the QBasic graphics capabilities.


' Spiro.bas
'
DECLARE SUB MoveTo (x AS DOUBLE, y AS DOUBLE)
DECLARE SUB Spiro (ns AS INTEGER, nw AS INTEGER, nt AS INTEGER, d AS INTEGER, spos AS INTEGER, x AS DOUBLE, y AS DOUBLE, col AS INTEGER)
DECLARE FUNCTION HighestCommonFactor (a AS INTEGER, b AS INTEGER)
'
' TEST ROUTINE
DIM s AS INTEGER ' loop counter
DIM col AS INTEGER ' Drawing color
DIM r AS INTEGER, spos AS INTEGER, x AS DOUBLE, y AS DOUBLE

SCREEN 9 ' Set screen mode to 640x350, 16 colors
x = 320 ' or Max x coordinate / 2
y = 175 ' or Max y coordinate / 2
col = 1
diam = 2 ' Diameter of drawing wheel
spos = 100 ' Starting position
FOR b = 1 TO 12 ' Draw 12 spirographs
Spiro 360, 120, 360, r, spos, x, y, col
r = r + 2 ' increase distance from centre of drawing wheel
spos = spos + 10 ' increase starting position
col = col + 1 ' next color
NEXT
END

' Draw the loci of a point on a circle revolving inside
' another circle, or a circle revolving round another circle.
' Parameters:
' ns, No. teeth in stationary part (-ve = outside)
' nw, No. teeth in wheel (-ve = clockwise)
' nt, No. of teeth to 'do'
' r, Radius
' spos, tooth to start at
' x, y Coordinates of centre
' col Drawing color
SUB Spiro (ns AS INTEGER, nw AS INTEGER, nt AS INTEGER, r AS INTEGER, spos AS INTEGER, x AS DOUBLE, y AS DOUBLE, col AS INTEGER)
DIM n1 AS INTEGER, n2 AS INTEGER, i AS INTEGER, n AS INTEGER, no AS INTEGER
DIM a AS DOUBLE, b AS DOUBLE, alpha AS DOUBLE, beta AS DOUBLE
DIM offang AS DOUBLE, dab AS DOUBLE, adif AS DOUBLE, aob AS DOUBLE
DIM x1 AS DOUBLE, y1 AS DOUBLE, x2 AS DOUBLE, y2 AS DOUBLE
DIM PI AS DOUBLE

PI = 3.141592658979324#
n1 = ABS(ns): n2 = ABS(nw)
  • PI): b = n2 / (2# --- PI)

  • IF ns < 0 THEN

dab = a + b: r = -r
ELSE
dab = a - b
END IF

  • 2# --- PI / n1

  • alpha = 0#: adif = PI / n1: aob = a / b

n = (n2 / HighestCommonFactor(n1, n2))
  • n --- ABS(nt)

  • x1 = dab + r: y1 = 0#

  • COS(offang) + x: y2 = x1 --- SIN(offang) + y

  • MoveTo x2, y2

FOR i = 0 TO no - 1
IF nw < 0 THEN alpha = alpha - adif ELSE alpha = alpha + adif
  • aob ELSE beta = alpha --- aob

  • COS(alpha) + r --- COS(alpha - beta)

  • SIN(alpha) + r --- SIN(alpha - beta)

  • COS(offang) - y1 --- SIN(offang) + x

  • SIN(offang) + y1 --- COS(offang) + y

  • ' setcolor(col); lineto(x2, y2)

LINE -(x2, y2), col
NEXT
END SUB

' MoveTo x, y
' For QBasic
' For other languages, replace or omit
SUB MoveTo (x AS DOUBLE, y AS DOUBLE)
DIM dr AS STRING
dr = "BM " + STR$(INT(x)) + "," + STR$(INT(y))
DRAW "X" + VARPTR$(dr)
END SUB

' Highest common factor - Euclid's algorithm
FUNCTION HighestCommonFactor (a AS INTEGER, b AS INTEGER)
DIM i AS INTEGER, j AS INTEGER, r AS INTEGER
IF a > b THEN
i = a: j = b
ELSE
i = b: j = a
END IF
r = i \ j
WHILE (r <> 0)
i = j: j = r: r = i \ j
WEND
HighestCommonFactor = j
END FUNCTION



SPECIAL KEYS

Press ''Ctrl+Break'' or ''Ctrl+C'' to break a running program.

Press ''F5'' to continue a program whose execution was broken.

Press ''Shift+F5'' to restart a program back from the beginning.

Press ''F4'' while program execution is broken to view the run-time screen, then press any key to switch back to the code screen.

Press ''F1'' for help.



EASTER EGG

Press and hold ''LeftCtrl+LeftShift+LeftAlt and RightCtrl+RightShift+RightAlt'' simultaneously after running QBasic at the DOS prompt but before the title screen loads: this lists ''The Team'' of programmers. Note that on modern computers, it is much too fast to perform. It is best done on an old PC (preferably one with a working Turbo button, with the switch on to slow the CPU to 4.77MHz) or in an emulator like Bochs .


SEE ALSO




EXTERNAL LINKS