Sei sulla pagina 1di 8

Lab: Computer Programming with QBASIC

In the early days of computers, most people wrote and ran their own programs. This led to the
conclusion that everybody that was going to use a computer needed to learn how to program.
Courses like "Computers and Society" were substantially about programming.
Now, however, it is clear that most people who use computers are not programmers, but use
programs (applications) written by others; applications such as word processors, spreadsheets
and database programs. Today, some of these can involve programming. Here, building formulae
in Excel, and with the Paper and Pencil Computer, you have had some experience with
programming. Even major word processors such as Microsoft Word contain built-in
programming languages for customizing work; for example summing a column in a table as in
the EverRipe document. Usually, however, anything substantial is done by specialists within an
organization. In studying the impact of computers on society, it can still help to understand
something about programming. But this is nowhere near the amount of programming that was
done to create the applications.
What is a Computer Program?
A computer program is a list of instructions for a computer to execute. Programs come in several

Programs that a computer can execute directly are in binary "machine language." These
are called executable modules. Common extensions are exe, com, and dll. In this form,
instructions have two parts, the operation code which says what action to take (addition,
subtraction, load, store, print, etc.) and the address, which says which memory location to
use. In machine language, both parts are in binary code. In the original Paper and Pencil
Computer, STOP was 000, ADD was 001, SUB was 010 (counting in binary, of course),
and so forth. Programming in machine language is very difficult and, for this reason, rare.
Each type of (micro)processor has its own machine language, so that programs written
for one processor are not compatible with other types.
Slightly easier is "assembler," which uses mnemonic codes which the computer translates
into binary codes. This is the case with the symbolic names STOP, ADD, SUB and the
similar commands for the Paper and Pencil Computer. However, in full assembler
language, memory addresses also have symbolic names, which the computer assigns to
definite memory locations. Assembler language programs must be translated into
machine language by the computer. Each type of (micro)processor has its own assembler
language, so that programs written for one processor are not compatible with other types.
Most programming is done using so-called "higher level languages" such as Basic,
Fortran, Cobol and C. Here the statements are more like English, or at least algebra, and
work more the way humans calculate, for example
"x = a + b." Programs written in higher level languages also must be translated or
compiled into machine language, but each type of processor has its own compiler for

Page 1 of 8

Lab: Computer Programming With QBasic

each computer language, so that programs written in higher level languages can be run on
many different processors, by using that processor's compiler.
The newest type of computer language is the so-called 4GL, for Fourth Generation
Language such as C++ and Java. Programs in 4GL languages must also, of course, be
translated or compiled into machine language, but here the programmer need not program
exactly what the computer should do, but instead sets up "objects" and "methods" for
objects to use when they interact.

With a standard higher-level program, there are two program files (and sometimes more): the file
with the binary codes, also called "machine language," that the computer could execute (the
"executable" file with the exe extension, but other variations are used in addition) and the
"source file" with the English-like words. The programmer writes the source file, which usually
has one instruction per line, and the computer translates that into the executable file, using a
computer program that can be called either an "assembler" or "compiler." In this case, the entire
program is translated into binary codes or machine language, and then executed all at once.
In an alternate scheme, there is only a source file, and the source file is compiled into machine
language line by line, as it is executed (at run time). This is slower to execute, but for short
programs it can be more convenient, and since computers are so fast today, can still run quickly
in terms of human reaction times. In this case, the computer program that does the translation
from source code to machine language is called an "interpreter." In the case of interpretation, the
source program is translated into machine language line by line on the fly as it is executed, and
there is no machine language file.
Here you will examine some pre-written programs to see how they work, using the QBASIC
interpreted programming language. DOS always came with a built-in programming language.
Earlier DOS versions came with GWBASIC, but later, QBASIC became the standard. Early
versions of Windows came with a programming language, but starting with Windows 95,
programming languages became for-sale add-ons.
Programming has several steps:
1. Design of the program (we will skip that step here, but in real-life programming it is
critical, and often determines success or failure of the whole project).
2. Entering the program, usually via the keyboard. Programs are usually text files, although
they may have extensions other than txt (QBASIC, for example, uses bas). However,
even with other extensions, program files can be edited with text editors such as NotePad.
The program at this point is in a programming language, and is generically known as
"source code" (the form that can most easily be read and understood by humans.
3. Dubugging (easy part): fixing syntax problems. This is the easiest part of debugging. All
programming languages have a grammar or syntax, that is formal rules that all statements
must comply with. For example, in QBASIC, two variables must be separated by an
operator that combines them, such as +, -, * (multiplication) or /. Each program statement
must comply with the syntax, which means that the computer will be able to execute it.
This by no means assures that the program will do what you want, however.

Page 2 of 8

Lab: Computer Programming With QBasic

4. Debugging (hard part) execution. The source language statements must be translated into
machine code that the computer can directly execute, either by compilation or assembly
ahead of time, or by interpreting line-by-line as the program is run. QBASIC, again, uses
the second method, interpretation. As the program is executed, the programmer must
verify that the program is actually doing what was intended. This is also debugging, and
is more difficult that the first step.
QBASIC is designed for small programs and quick results. It has one outstanding feature for
beginners; each program line is checked for syntax after you leave the line with <Enter> or the
up or down arrow keys. It may not do what you want, but the check ensures that it is legal. If the
line is OK, it is put into a standard format with:

all mathematical operators such as + and - have a space on each side

all QBASIC commands and other keywords are capitalized. Examples of commands and
keywords are INPUT (get user input from keyboard) and PRINT (display on the screen).

If the line is not legal, there is no change from the way you typed it; this is a signal to look for a
problem and edit the line to fix the problem(s).
Notice that if you enter lines already in the standard format, there will be no change if the line is
legal. Therefore, it pays to enter lines without spaces around the operators, and with the
keywords in lowercase.
Downloading QBASIC:
If QBASIC is installed on the computer you are using, you can skip this installation step. To tell
if QBASIC is installed, start Windows Explorer and do a search on the C: drive for
"QBASIC.EXE". If you do not find QBASIC installed, copy two files to a suitable directory on
the C: drive: (1) QBASIC.EXE which is the program that you run to use the QBASIC
programming language, and (2) the help file QBASIC.HLP. You can download both of these, one
at a time, from the IST 2710 web site at
If necessary, you can save both of these files to a floppy diskette; QBASIC runs well from a
floppy diskette, without any noticeable loss of speed.
Remember to right-click on the link to each of these files!!!!!
It is convenient to save them to your computer desktop, which you can select in the "Save As..."
dialog by clicking the "up the path" icon several times until "Save In" shows "Desktop" as in the
figure below (your screen may look slightly different, depending upon your Browser, Browser
version, and Windows version):

Starting QBASIC:

Page 3 of 8

Lab: Computer Programming With QBasic

If you have a QBASIC desktop icon, double-click that. If you have separate icons for
will not run alone; it is called from within QBASIC.EXE.

If you do not have a QBASIC desktop icon, but you have copied QBASIC to your C: or
A: drive, start Windows Explorer, right-click on the C: or A: icon, then choose "Find" and
type "qbasic.exe" and click "Find Now." When QBASIC is found, you have two options:
o To create a desktop icon, right-click on the qbasic.exe icon, choose "Create
Shortcut" and respond "Yes" when you are asked if you want the shortcut to be
placed on the desktop. Close the "Find" dialog, close or minimize Windows
Explorer, and locate the new icon on the desktop. Proceed as under the first bullet
o To run directly, right-click on the qbasic.exe icon, choose "Open", click "OK" to
clear the Parameters dialog, and QBASIC will open.

As it says on the screen, tap the <Esc> key to clear the opening dialog from the screen. The
cleared screen is ready to accept your program, typed in from the keyboard.
If you see an Immediate screen, ignore it.
1. Using QBASIC if the mouse works
You may be able to use the mouse with QBASIC menus. In QBASIC, the mouse icon is a
rectangle the same size as a letter on the screen. If you see this icon, and the icon moves when
you move the mouse, then you can use the mouse to control QBASIC. QBASIC menus are very
similar to Windows menus. Unlike Windows, the mouse cursor may start off the screen on the
left. In this case, move the mouse to the right to bring the cursor onto the screen.
2. Using QBASIC if the mouse does not work
If you do not see a mouse icon, or if you see a normal arrow-shaped mouse icon, you cannot use
the mouse to control QBASIC. In this case, you must use the keyboard instead. To control the
QBASIC menus using the keyboard, first hit the <Alt> key. This will highlight the menu items.
(If you hit <Alt> by mistake, you can hit <Esc> or <Alt> again to cancel and return to editing
your program.) To pull down a menu item, such as File, type its first letter (white when menus
are highlighted). With the menu subitems pulled down for display, move up and down with the
corresponding arrow keys. When the item you want is highlighted, hit <Return> to choose it.
3. Printing your program

Page 4 of 8

Lab: Computer Programming With QBasic

If a printer is connected directly to your computer, the chances are that the File > Print menu
items will work. However, if you are connected to a shared printer through a network, QBASIC
is old enough that File > Print most likely will not print your program. In this case, (a) save your
program (source file) to disk with the default extension bas, (b) start Microsoft Word, (c)
choose the File > Open menu item or click on the Open toolbar button, (d) at the bottom of the
Open dialog change Files of type: to All Files (*.*), select your program from the list, and
(e) print the program from Word. Note that you will have to run your program to write the
comments. Your program still only runs from QBASIC, not from Word.
4. Other hints
The programming screen is blue by default; the help screens are black.
In QBASIC, like most high-level programming languages, the programmer keeps tracks of
variables by a name or symbol (e.g. x) instead of by the numerical address of the location.
QBASIC keeps track of the numerical addresses.
QBASIC has several types of values it can store and work with. Here, these are specified by the
last character of the variable name, as follows:

$: a string, or sequence of characters.

%: an integer, or whole number.
!: a real number (a number with a decimal point).

On a program line, anything after a single quote (') is treated as a comment and ignored. You will
use this as part of the assignment.

(NOTE 1: Below, for a key with a word label, such as Esc, the key is shown as <Esc>. If you see
the angle brackets <> they enclose the text on the key.)
(NOTE 2: All of these program, when you run them, will wait for you to type something in. The
point of each is to figure out what the computer is doing in response to your typing.)
1. Start QBASIC as described above.
2. One by one, type the programs listed below into the QBASIC programming screen. Put a
comment at the end of each line explaining what the line does in the program.
(Remember: comments start with '.) Put another comment line at the beginning with your
name on it, and a second line (or lines) explaining what the program does. Print out and
turn in the program. The File / Print menu items prints the current program. Save each
program to a separate file on your floppy diskette. Use the File / Save As... menu to save
your programs. Save regularly.
A. Type in each line using all lowercase characters, and with no spaces around the =
and + characters. For example, for the line in Program 1 shown as
j$ = INKEY$
Page 5 of 8

Lab: Computer Programming With QBasic

type in instead
B. If you haven't made any typing mistakes, you will your typing change to the
format shown in the program listing when you hit <Enter> or <Return> to start
typing the next line.
3. You will probably make typing mistakes, which usually result in the line just typed not
having the correct syntax, so that QBASIC will not put the line in the standard format as
explained above. When this happens (doesn't happen), examine the line on the screen,
compare it to the listing here, and make corrections, until the syntax errors have all been
4. After each program is working, print the program (refer to #3 above, Printing your
5. Also, run each program to see exactly what it does. (In fact, you may have to do this
before you can figure out what some of the program lines do.) Once a program is typed
in, use the Run / Start menu item (or the shortcut <Shift><F5> - HOLD DOWN <Shift>
and tap <F5> then release <Shift>) to run the program. Each of these programs waits for
the user (you) to type. On the program listing, write out by hand what the program does
when you type input.
You may want to read the appendix for help in deciphering what the program is doing.
6. If a program does nothing, or does something whacky, it is important to understand that
you may have mistyped the program in a way that still has a valid syntax. Omission of a
$, % or !, for example, will throw the program off.
7. After you are finished with a program, clear the screen to get ready for the next program.
To clear the screen, the easiest method is to use the menu item File > New (<Alt>FN).
8. These programs all demonstrate how it is the microprocessor that controls what appears
on the screen. In other words, there is no direct connection between the keyboard and the
screen, but everything is subject to the control of the microprocessor, and therefore under
control of the programmer (you).

NOTE 1: each program listing has a NOTE at the end, about how to end the
program once you run it. These NOTEs are not part of the program do
not type them in as part of the program.
NOTE 2: programs are shown as they should appear AFTER you have tapped
<Enter> at the end of a line. Remember to do your actual typing in lower
case for letters, with no spaces around operators (such as + - * / =).
Program 1: Simple Word Processor
PRINT "C:> ";
DO WHILE (2 > 1)
j$ = INKEY$
IF (LEN(j$) = 1) THEN
IF (ASC(j$) = 27) THEN STOP

Page 6 of 8

Lab: Computer Programming With QBasic

NOTE: you will have to use <Esc> to end this one.

Program 2: Crazy Keys

PRINT "C:> ";
DO WHILE (2 > 1)
j$ = INKEY$
IF (LEN(j$) = 1) THEN
IF (ASC(j$) = 27) THEN STOP
IF (j$ <> " ") THEN j$ = CHR$(ASC(j$) + 1)
NOTE: you will have to use <Esc> to end this one.

Program 3: Prisoner
s$ = "Help, I'm a prisoner inside this computer. "
j% = LEN(s$)
i% = 0
PRINT "C:> ";
DO WHILE (2 > 1)
k$ = INKEY$
IF (LEN(k$) = 1) THEN
IF (ASC(k$) = 27) THEN STOP
i% = i% + 1
IF (i% > j%) THEN i% = 1
PRINT MID$(s$, i%, 1);
NOTE: you will have to use <Ctrl>c to end this one;

After you are done with QBASIC, you can end it using the menu item File > Exit, or <Alt>FX.

Appendix: More detail about the QBASIC commands

There are many QBASIC commands and keywords. To learn about one, click on Help, doubleclick on Index and type the first letter of the keyword. Then double-click on the one you want to
read about. To leave Help, hit <Esc> to get back to the programming screen. Here are the
keywords we will be using:

ASC(): A number equal to the character code of the character inside the parentheses.

Page 7 of 8

Lab: Computer Programming With QBasic

CLS: Clear the screen

DO WHILE (condition): Carry out the program lines that follow, as long as the
condition following the WHILE is true.
IF (condition) THEN action: If the condition is true, then the action is executed. If the
condition is false, then nothing is done.
IF (condition) THEN action1 ELSE action2: If the condition is true, then actions is
executed; otherwise action 2 is executed.
INKEY$: Gets the keyboard from the user, one key at a time, but without displaying it
on the screen. As indicated by the $ at the end, they key is treated as a character, not as
INPUT: Get keyboard input from the user. Also displays the keyboard on the screen. For
example, INPUT x% gets number keystrokes from the keyboard, converts them from
ASCII and stores them in the variable x%. If there is a string inside quotation marks
between INPUT and the variable, it is output as a prompt to the user. If the user entry
does not match the variable, it cannot be stored in the variable, and an error message is
displayed. This happens if, for example, the variable is an integer (ends in %) and the
user types z.
LEN(): A number equal to the length of the string inside the parentheses
LOOP: Ends a DO WHILE (lines between DO WHILE and LOOP will be carried out
LPRINT: Print to the printer
PRINT: Print to the screen. Ending with a semicolon (;) means that the next print starts
immediately to the right.
MID$(string,i,j): A substring of the string that is the first argument, starting at character
number i. The substring is j characters long. (If j is omitted, the substring is one character

Page 8 of 8