QBASIC Getting Started
Mesa Community College - Business Administration Department - Computer Information Systems (CIS) - 2000

This QBASIC Getting Started exercise is designed to help students access and use Microsoft’s QuickBASIC (QBASIC). QBASIC is Microsoft’s version of the BASIC (Beginner’s All-purpose Symbolic Instruction Code) programming language. QBASIC was developed for MS-DOS, so the interface is not as intuitive as most software packages developed for Windows, thus a need for a simple exercise, which can then be used as a reference when completing lab assignments. Instructions on how to enter, save, execute, print, and retrieve a program are covered. This exercise does not cover program design, development, QBASIC syntax, and QBASIC commands. However, a list of QBASIC commands is provided to support any other instructional material that may be used.

Accessing QBASIC:

        Click on Start and follow the following menu path:

           Start > Classes > Structured Classes > CIS105 > QBASIC

           Press ESC to enter the QBASIC editor
QBASIC Editor


QBASIC Editor:

In a full installation, there is some online help available. Each time QBASIC is executed, the option of entering the Survival Guide is presented. Simply press ESC to clear the dialog box and enter the QBASIC editor. Although QBASIC is not a Windows program, it provides menus as well as shortcut keystrokes. A combination of these are used throughout the exercise as the practice program is entered.

Once in the editor, enter the following simple program to practice using QBASIC.
Press the Enter key at the end of each line.
   REM This program adds two numbers and prints the sum
   CLS
   READ num1, num2
   LET sum = num1 + num2
   PRINT “The sum is ”; sum
   DATA 8,16
   END
Practice Program

Saving Programs:

Before continuing, the program should be saved. In most cases, the program will be saved on the floppy drive (A:). Click on File and select Save As. Enter A:PRACTICE as the filename, and then click on OK. Leave the QuickBASIC format option selected. QBASIC automatically assigns an extension of .BAS.

Save Program


Executing Programs:

After entering the program, you are ready to execute it. When you execute it, QBASIC will first check the program for syntax errors. If any errors are identified, correct the errors and try executing it again. Click on Run and select Start to execute the program, or press the function key F5 on top of the keyboard.
In the example that follows, the READ statement was changed to RED to cause a syntax error. As depicted below, the error message is not very helpful, but at least the word RED is highlighted. This gives you an idea of where the error is. For this error, we would click on OK, change RED to READ, and then press F5 again. If another error existed, then the next time we ran it, that error would be highlighted. The BASIC interpreter reads the program one line at a time and stops either when it finds an error or it gets to the end of the program.
Sometimes a program needs to be terminated by the programmer before it reaches the end. This usually happens when the programmer makes an error and codes an endless loop, which means the program will run forever. Use Ctrl + Break to terminate a program before it reaches its normal ending point.

Syntax Error

When a program runs successfully, the output is displayed in a DOS window. At the end of the program execution, press any key to continue and return to QBASIC.

Program Execution

Printing Sample Output:

If a copy of a program’s output is required, you’ll need to capture a picture of the window by pressing Alt + Print Screen. The Print Screen button is on top of the keyboard, just to the right of the F12 key. The picture of the window can then be pasted into and printed from WordPad, Word, or similar wordprocessor.

Another option is to change the PRINT command to LPRINT. LPRINT sends the output to the device assigned to LPT1. The use of LPRINT is not recommended especially if there is a chance of having a print command within an endless loop. This is a problem because once the output is sent to a network printer, the user can not stop it.

Students should place their name in a REM and PRINT statement. This will help identify printed copies among the similar programs being printed by other students.

Copying Sample Output:

The text displayed in the output window can also be selected, copied, and pasted into a wordprocessor, instead of taking a picture of the window. In the output window, click on the selection button Select Button, click and drag around the desired text, click on the copy button Syntax Error, and then switch to a wordprocessor and paste it. This is effective when multiple pages of output should appear as one page. The catch is that the programmer needs to ensure that no more than 23 lines of output are displayed at one time, and that an input prompt is added to pause the program’s execution. During the pause is when the output should be captured. Review lab assignments #2 and #3 provided on a separate document to see this technique employed.

Printing Programs:

Print the program currently opened by clicking on File and selecting Print. Within the print option’s dialog window, the correct choice of Current Module is selected automatically if no text was selected before issuing the print command. Click on OK or press the enter key to print the program.

Printing Programs

Retrieving Programs:

To retrieve your program from the floppy, click on File and select Open Program. Click on [-A-] under Dir/Drivers. A list of programs should be listed in the Files window. Only the files with an extension of .BAS are listed. Click on the desired file and click OK.

Retrieving Programs

After retrieving the program, it can be modified and executed again. Be sure to save again it if any changes are made.

Exiting QBASIC:

To exit QBASIC click on File and select Exit. Be sure to save before exiting.

QBASIC Commands - Level I:

Statement

Description

Example

CLS

Clears the screen

CLS

DO WHILE / LOOP

Used to form a do-while loop

DO WHILE x < 10
.
QBASIC statements
.
LOOP

END

Last statement to mark the end

END

GOSUB label

Branch to a subroutine; subroutine is identified by its label:.

GOSUB INPUT.RTN

See the RETURN statement.

RETURN

Return to the statement right after the GOSUB that branched the processing to this routine

INPUT.RTN:
.
QBASIC statements
.
RETURN

IF-THEN-ELSE-ENDIF

Conditional branching

IF x < 10 THEN
LET y = 5
ELSE
LET y = 10
ENDIF

INPUT

Enables data to be entered interactively

INPUT "Enter hourly rate: "; hr

LET

Assign a value to variable

LET sum = num1 + num2

PRINT

Displays information

PRINT "The sum is "; sum

READ

Assigns values to variables from a list of DATA statements

READ num1, num2

DATA

Batch data within the program

DATA 8, 16

REM

Remark

REM This program adds 2 numbers

RESTORE

Moves data pointer to first DATA statement

RESTORE


QBASIC Commands - Level II:

Statement

Description

Example

CALL

Calls a subroutine

CALL calc.rtn (num1, num2)

CLOSE

Closes a file

CLOSE #1

DEF

Sets up a user-defined function

DEF FNC(n1,n2) = n1 + n2

DIM

Dimensions an array

DIM X(12)

DO / LOOP UNTIL

Used to form a do-until loop

DO
.
QBASIC statements
.
LOOP UNTIL x < 10

FOR / NEXT

Used to form a FOR-NEXT loop

FOR x = 1 to 10
.
QBASIC statements
.
NEXT x

FUNCTION /
END FUNCTION

Used to define a function procedure

FUNCTION area (num1)
.
QBASIC statements
.
END FUNCTION

INPUT #n

Reads data from a sequential file

INPUT #1, num1, num2

ON x GOSUB

Branches to a specific label based on the value of the variable

ON x GOSUB rtn-1, rtn-2, rtn-3

OPEN file FOR

OUTPUT AS #n

Opens an external file for input or output and assigns a numeric value for internal reference (#1, #2, #3, etc)

OPEN "A:out_file"
FOR OUTPUT AS #1
or
OPEN "A:in_file"
FOR INPUT AS #1

PRINT USING x$

Use to display formatted output

S$ = "sum is ####.##"
PRINT USING S$; sum

SELECT CASE

Branches to a specific case

SELECT CASE num1
CASE 1, 2, 3, 4,
PRINT "LOW NUMBER"
CASE ELSE
PRINT "HIGH NUMBER
END SELECT

SUB / END SUB

 

SUB calc.rtn (num1, num2, sum)
.
QBASIC statements
.
END SUB

WRITE #n

Writes data to a sequential file

WRITE #1, num1, num2, sum


This reference guide was compiled and prepared by Juan Marquez, Ed.D. (2000) to be used by faculty and students of Mesa Community College. A big thank you to all of the faculty that openly shared their materials.
Revised: 7/30/2000 - www.mc.maricopa.edu/academic/business/cis105/basic/basic2_getting_started.html