Debugging

If you write and run your own program, it probably wonít work.

Your first reaction will be to blame the computer. Donít!

The probability is 99.99% that the fault is yours. Your program contains an error. An error is called a bug. Your next task is to debug the program, which means get the bugs out.

Bugs are common; top-notch programmers make errors all the time. If you write a program that works perfectly on the first run and doesnít need debugging, itís called a gold-star program and means you should have tried writing a harder one instead!

Itís easy to write a program thatís nearly correct but hard to find the little bug fouling it up. Most time you spend at the computer will be devoted to debugging.

Debugging can be fun. Hunting for the bug is like going on a treasure hunt Ė or solving a murder mystery. Pretend youíre Sherlock Holmes. Your mission: to find the bug and squish it! When you squish it, have fun: yell out, "Squish!"

How can you tell when a roomful of programmers is happy? Answer: when you hear continual cries of "Squish!"

To find a bug, use three techniques:

Inspect the program.

Trace the computerís thinking.

Shorten the program.

Here are the details.Ö

Inspect the program

Take a good, hard look at the program. If you stare hard enough, maybe youíll see the bug.

Popular typos Usually, the bug will turn out to be just a typing error, a typo. For example.Ö

Maybe you typed the letter O instead of zero? Zero instead of the letter O?

Typed I instead of 1? Typed 1 instead of I?

Pressed the SHIFT key when you werenít supposed to? Forgot to press it?

Typed an extra letter? Omitted a letter?

Typed a line you thought you hadnít? Omitted a line?

Fix your strings You must put quotation marks around each string, and a dollar sign after each string variable:

Right: a$ = "jerk"

Wrong: a$ = jerk

Wrong: a = "jerk"

Too much? Here are three reasons why the computer might print too much:

1. You forgot to insert the word END or EXIT DO into your program.

2. Into a DO loop or FOR loop, you inserted a PRINT line that should be outside the loop.

3. When you started typing the program, you forgot to choose New from the file menu; so the computer is including part of the previous program.

Trace the computerís thinking

If youíve inspected the program thoroughly and still havenít found the bug, the next step is to trace the computerís thinking. Pretend youíre the computer. Do what your program says. Do you find yourself printing the same wrong answers the computer printed? If so, why?

To help your analysis, make the computer print everything itís thinking while itís running your program. For example, suppose your program uses the variables b, c, and x$. Insert lines such as these into your program:

10 PRINT "I'm at line 10. The values are"; b; c; x$

20 PRINT "I'm at line 20. The values are"; b; c; x$

Then run the program. Those extra lines tell you what the computer is thinking about b, c, and x$ and also tell you how many times the computer reached lines 10 and 20. For example, if the computer prints what you expect in line 10 but prints strange values in line 20 (or doesnít even get to line 20), you know the bug occurs after line 10 but before line 20.

Hereís a good strategy. Halfway down your program, insert a line that says to print all the values. Then run your program. If the line you inserted prints the correct values, you know the bug lies underneath that line; but if the line prints wrong values (or if the computer never reaches that line), you know the bug lies above that line. In either case, you know which half of your program contains the bug. In that half of the program, insert more lines, until you finally zero in on the line that contains the bug.

Shorten the program

When all else fails, shorten the program.

Hunting for a bug in a program is like hunting for a needle in a haystack: the job is easier if the haystack is smaller. So make your program shorter: delete the last half of your program. Then run the shortened version. That way, youíll find out whether the first half of your program is working the way itís supposed to. When youíve perfected the first half of your program, tack the second half back on.

Does your program contain a statement whose meaning youíre not completely sure of? Check the meaning by reading a book or asking a friend; or write a tiny experimental program that contains the statement, and see what happens when you run it.

Hint: before you shorten your program (or write tiny experimental ones), save the original version (by choosing Save from the file menu), even though it contains a bug. After youíve played with the shorter versions, retrieve the original (by choosing Open from the file menu) and fix it.

To write a long, correct program easily, write a short program first and debug it, then add a few more lines and debug them, add a few more lines and debug them, etc. So start with a small program, perfect it, then gradually add perfected extras so you gradually build a perfected masterpiece. If you try to compose a long program all at once Ė instead of building it from perfected pieces Ė youíll have nothing more than a mastermess Ė full of bugs.

Moral: to build a large masterpiece, start with a small masterpiece. To build a program so big that itís a skyscraper, begin by laying a good foundation; double-check the foundation before you start adding the programís walls and roof.

Error messages

If the computer canít obey your command, the computer will print an error message. The following error message are the most common.Ö

Syntax errors

If you say "prind" instead of "print", the computer will say:

Syntax error

That means the computer hasnít the faintest idea of what youíre talking about!

If the computer says you have a syntax error, itís usually because you spelled a word wrong, or forgot a word, or used a word the computer doesnít understand. It can also result from wrong punctuation: check your commas, semicolons, and colons. It can also mean your DATA statement contains a string but your READ statement says to read a number instead; to fix that problem, change the READ statement by putting a dollar sign at the end of the variableís name.

If you try to say PRINT 5 + 2 but forget to type the 2, the computer will say:

Expected: expression

If you type a left parenthesis but forget to type the right parenthesis that matches it, the computer will say:

Expected: )

If you accidentally type extra characters (or an unintelligible word) at the end of the line, the computer will say:

Expected: end-of-statement

Numeric errors

If the answer to a calculation is a bigger number than the computer can handle, the computer will say:

Overflow

To help the computer handle bigger numbers, remember to put a decimal point in any problem whose answer might be bigger than 32 thousand.

If you try to divide by zero, the computer will say:

Division by zero

If you feed the computer a number thatís inappropriate, the computer will say:

Illegal function call

Thatís what the computer will say if you try saying WIDTH 50 instead of WIDTH 40, or you try saying LPRINT CHR$(1200) instead of LPRINT CHR$(12).

Printer errors

If your printer runs out of paper, the computer will say:

Out of paper

If your printer isnít communicating well with the computer, the computer will say:

Device fault

That means printerís cable to the computer is unplugged or loose or defective or plugged into the wrong socket, or the printer is turned off, or the printer is off-line (because you pressed a button that turned off the printerís ON LINE light), or the paper is jammed, or thereís no more ink left, or the printer broke.

Logic errors

Some commands come in pairs.

DO The words DO and LOOP form a pair. If you say DO but no line says LOOP, the computer will gripe by saying:

DO without LOOP

If you say LOOP but no line says DO, the computer will say:

LOOP without DO

The words FOR and NEXT form another pair. If part of the pair is missing, the computer will say Ė

FOR without NEXT

or:

NEXT without FOR

SELECT If a lineís first word is SELECT, youíre supposed to have a line below saying END SELECT.

If you say SELECT but no line says END SELECT, the computer will say:

SELECT without END SELECT

If you say END SELECT but no lineís first word is SELECT, the computer will say:

END SELECT without SELECT

Between the SELECT and END SELECT lines, youíre supposed to have several lines saying CASE. If you say CASE but no lineís first word is SELECT, the computer will say:

CASE without SELECT

GOTO If you say GOTO 10, the computer tries to find a line numbered 10. If you say GOTO joe, the computer tries to find a line named joe. If thereís no line numbered 10 or no line named joe, the computer will say:

Label not defined

IF Here are other messages about unmatched pairs:

ELSE without IF

END IF without block IF

Block IF without END IF

DATA If you say READ but the computer canít find any more DATA to read (because the computer has read all the DATA already), the computer will say:

Out of DATA

Type mismatch The computer handles two major types of information: numbers and strings. If you feed the computer the wrong type of information Ė if you feed it a number when you should have fed it a string, or you feed it a string when you should have fed it a number Ė the computer will say:

Type mismatch

When you feed the computer a string, you must put the string in quotation marks, and put a dollar sign after the stringís variable. If you forget to type the stringís quotation marks or dollar sign, the computer wonít realize itís a string; the computer will think youíre trying to type a number instead; and if a number would be inappropriate, the computer will say "Type mismatch". So when the computer says "Type mismatch", it usually means you forgot a quotation mark or a dollar sign.

PAUSE key

Magicians often say, "The hand is quicker than the eye." The computerís the ultimate magician: the computer can print information on the screen much faster than you can read it.

When the computer is printing faster than you can read, tap the PAUSE key. (If your keyboard is modern, thatís the last key in the top row. If your keyboard is old-fashioned, no key says PAUSE on it, so do this instead: while holding down the Ctrl key, tap the NUM LOCK key.)

Then the computer will pause, to let you read whatís on the screen.

When youíve finished reading whatís on the screen and want the computer to stop pausing, press the ENTER key. Then the computer will continue printing rapidly, where it left off.

If your eyes are as slow as mine, youíll need to use the PAUSE key often! Youíll want the computer to pause while youíre running a program containing many PRINT statements (or a PRINT statement in a loop).

F keys

You already learned that to run your program, you press SHIFT with F5; and while viewing the blue screen, you can peek at the black screen instead by pressing F4 (and then pressing F4 again to return to the blue screen). Here are other F keys you can press.Ö

F6 (immediate window)

Near the bottom of QBASICís blue screen, you see the word "Immediate". The area below that word is called the immediate window. The area above that word is called the program window.

Usually you type in the program window. Thatís where you type your programs.

To use the immediate window, press the F6 key. That moves the cursor (blinking underline) down to the immediate window. Whatever you type afterwards will be in the immediate window.

Any command you type in the immediate window will be obeyed by the computer immediately. (The computer will not wait for you press SHIFT F5.)

For example, in the immediate window type this:

PRINT 4 + 2

When you press the ENTER key at the end of that line, the computer obeys that line immediately; the computer immediately makes the screen turn black and prints the answer (6) near the bottom of the black screen. When you finish admiring the answer, press the F4 key to switch back to the blue screen.

The cursor will still be in the immediate window. Go ahead: type another PRINT line in the immediate window. When you press the ENTER key at the end of the line, the computer will print the answer on the black screen. Press F4 again, so you see the blue screen again.

You can use the immediate window to quickly PRINT the answers to calculations (so you can discard your calculator) and to explore advanced aspects of the PRINT command.

When you get tired of using the immediate window, press the F6 key again, which moves the cursor back up to the program window.

Try this experiment: in the program window, write a program and run it (by pressing SHIFT F5). When the program finishes running (or gets interrupted by an error), go down to the immediate window (by pressing F6) and tell the computer to PRINT the programís variables. For example, if the program mentioned a variable called x, say this in the program window:

PRINT x

That makes the computer print the number that x stands for. That information will help you debug the program.

F9 (breakpoint)

If you point at a line of your program (by using the arrow keys) and then press the F9 key, the line turns red. Try it! Make several lines in your program turn red.

So when pointing at a line, pressing F9 makes the line turn red; pressing F9 again makes the line turn back to blue.

Try this experiment: type a long program (containing 5 lines or more), and make two of the lines turn red. Make the red lines be in the middle of the program, instead of being the top or bottom lines.

When you tell the computer to run the program (by pressing SHIFT with F5), the computer starts running the program; but when the computer reaches a red line, it pauses at the beginning of that line and shows you the blue screen again.

While the computer pauses at the red line, do whatever you wish! For example:

You can peek at the black screen (by pressing F4 to see the black screen, then pressing F4 again to return to the blue screen).

You can use the immediate window (by pressing F6, then typing a PRINT command in the immediate window, then pressing F6 to return to the program window).

You can edit your program.

If you press F5, the computer will continue running the program: it will obey that red line and the lines underneath. If the computer comes to another red line, the computer will pause at that red line also.

Pressing F5 makes the computer continue where it left off. Pressing SHIFT with F5 makes the computer run the program from the beginning instead.

Each red line is called a breakpoint, because when the computer is running a program and encounters a red line, the computer breaks its train of thought and pauses.

If youíve turned many lines red, hereís how to get rid of all the redness: choose "Clear All Breakpoints" from the Debug menu (by pressing Alt then D then C).

F7 (run to here)

To have fun, make sure none of the lines in your program is red; then point at a line in the middle of your program (by using the arrow keys), and press F7.

The computer will temporarily turn the line red (so the line becomes a breakpoint). Then the computer will run the program up to that line. Then the computer will get rid of the red.

So if you point at a line and then press F7, the computer will run just the programís beginning, up to that line.

Notice the contrast:

Pressing SHIFT with F5 runs the whole program.

Pressing F7 runs just the programís beginning.

F8 (single step)

Instead of telling the computer to run your entire program, you can tell the computer to run just one line at a time, as slowly as you wish. Hereís how: press the F8 key. Each time you press the F8 key, the computer will run one more line of your program. So if you press the F8 key three times, the computer will run three lines of your program.

Pressing F8 makes the computer run one more line, then show you the blue screen again. At the blue screen, do whatever you wish: you can press F4 (to peek at the black screen), or press F6 (to move to the immediate window), or edit your program, or press F8 again (to run the next line).

The first time you press F8, the computer typically runs the programís first line; but if the computer was in the middle of running your program and was interrupted (by a red breakpoint line or a line saying STOP), pressing F8 makes the computer continue where it left off and do one more line.

Apostrophe

Occasionally, jot a note to remind yourself what your program does and what the variables stand for. Slip the note into your program by putting an apostrophe before it:

'This program is another dumb example, written by Russy-poo, a stupid jerk.

'It was written on Halloween, under a full moon, shaded by his fangs.

CLS

c = 40 'because Russ has 40 computers

h = 23 'because 23 of his computers are haunted

PRINT c - h 'That is how many computers are unhaunted and safe for kids.

When you run the program, the computer ignores everything thatís to the right of an apostrophe. So the computer ignores lines 1 & 2; in lines 4 & 5, the computer ignores the "becauseÖ"; in the bottom line, the computer ignores the comment about being unhaunted. Since c is 40, and h is 23, the bottom line makes the computer print:

17

Everything to the right of an apostrophe is called a comment (or remark). While the computer runs the program, it ignores the comments. But the comments remain part of the program; they appear on the blue screen with the rest of the program. Though the comments appear in the program, they donít affect the run.