Last updated: 5/13/01
Link back to site Welcome...

Lab: Computer Programming with QBASIC


Background

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; programs such as word processors, spreadsheets and database programs. Today, some of these can involve programming. Here, building formulae in Excel, you have done some programming. And even major word processors such as Microsoft Word contain built-in programming languages for customizing work. Usually, however, anything substantial is done by specialists within an organization. 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.

Here you will examine some pre-written programs to see how they work, using the QBASIC 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. Fixing syntax problems. This is the easiest part of debugging. All programming languages have grammars or syntaxes, 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.
  4. Execution and debugging. The source language statements must be translated into machine code that the computer can directly execute. This can be done statement-by-statement and executed after translation. This method is known as "interpeting." Or, all of the translation can be done at once, up front, and saved as a file (exe, com or dll extension) known as "compilation." Compilation results in faster execution rates since the computer does not have to do the translation while it is executing. QBASIC is an interpreted language. As the program is executed by whichever of these two types of processes, 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:

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.

Starting QBASIC

When QBASIC starts, tap the <Esc> key to clear the opening dialog, unless you want to read the background help information.

Using QBASIC:

You may be able to use the mouse with QBASIC menus. QBASIC menus are very similar to Windows menus. Unlike Windows, the mouse cursor starts off the screen on the left. Move the mouse to the right to bring the cursor onto the screen.

To use the QBASIC menus on the lab computers, 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 the screen.) 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.

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 symbol (e.g. x) instead of by the numerical address of the location. QBASIC keeps track of the 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:

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.

Assignment:

(NOTE: 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.)

  1. Start QBASIC:
  2. One by one, type the programs listed below into the QBASIC programming screen. Put a comment on 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.
    1. 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$
    2. 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 corrected.
  4. After each program is working, use the File / Print menu items to print the program with your comments, then delete everything from the screen to type in the next program. You can use File / Save (As) to save your programs.
  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 to run the program. Each of these programs waits for the user (you) to type. On the program listing, write out what the program does.
  6. In general, 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.

Program 1

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

CLS
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)
    PRINT j$
  END IF
LOOP

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

Program 3

s$ = "Help, I'm a prisoner inside this computer. "   ' Set up line for display
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;

Appendix: More detail about the QBASIC commands

There are many QBASIC commands and keywords. To learn about one, click on Help, double-click 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: