QBASIC_LAB
Shared by: cuiliqing
-
Stats
- views:
- 1
- posted:
- 11/7/2011
- language:
- English
- pages:
- 3
Document Sample


Lab: Computer Programming with QBASIC
Background
QBasic is a quick-response computer programming language, designed for small programs a fast
response. If you are going to do this lab away from the classroom, see the document,
“Programming With QBasic” on the IST 2710 website.
Printing your program
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, (e), add your name and any
comments, (f) make sure the font for the actual program lines is “Courier” or “Courier New”,
and (g) 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.
Lab:
(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. Some keyboards may use different
abbreviations, or not us an abbreviation at all.)
(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$
type in instead
j$=inkey$
B. Each character is critical, including punctuation such as ;. Type carefully.
C. If you haven't made any typing mistakes, you will see your typing change to the format
shown in the program listing when you hit <Enter> or <Return> to start typing the next
line.
Page 1 of 3
Lab: Computer Programming With QBasic
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
corrected.
4. After each program is working, print the program (see “Printing your program”).
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.
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.
6. 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).
7. 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”
CLS
PRINT "C:> ";
DO WHILE (2 > 1)
j$ = INKEY$
IF (LEN(j$) = 1) THEN
IF (ASC(j$) = 27) THEN STOP
PRINT j$;
END IF
LOOP
NOTE: you will have to use <Esc> to end this one.
Program 2: “Crazy Keys”
CLS
PRINT "C:> ";
Page 2 of 3
Lab: Computer Programming With QBasic
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)
PRINT j$;
END IF
LOOP
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
CLS
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);
END IF
LOOP
NOTE: you will have to use <Ctrl>c to end this one;
Ending
After you are done with QBASIC, you can end it using the menu item File > Exit, or <Alt>FX.
Page 3 of 3
Get documents about "