The other night, my daughter called me over to the old personal computer to show me her latest program. And it was a real joke. No kiddin'.
"Hello and welcome to Jennifer's fabulous knock-knock joke," proclaimed the computer's video monitor. Right underneath that line was the familiar "Knock-knock," which was followed by the blinking cursor that told me the computer was awaiting my response.
Right then I knew I had found my next column: I was going to show you how a simple computer program works.
It so happens that Jennifer's program is written in BASIC, which stands for Beginner's All-purpose Symbolic Instruction Code, the most common and popular programming language. So it's perfect for the task.
A program, of course, is a set of instructions that a computer follows, often under the direction of the user, so that a given task--in this case, the telling of a joke--can be accomplished.
The important thing to remember in all this is that the computer will follow its instructions (the program) without deviation. So any "mistakes" that occur in the end result are the fault of the program writer or the user.
Not the computer.
Here, then, is how Jennifer wrote her program so that the computer would tell an old joke:
10 PRINT "Hello, and welcome to Jennifer's fabulous knock-knock joke."
20 PRINT "Knock-knock."
30 LET A$ = "Who's there?"
40 INPUT B$
50 IF A$ B$ THEN 20
60 IF A$ = B$ THEN 70
70 PRINT "Creature."
80 LET A$ = "Creature who?"
90 INPUT B$
100 IF A$ B$ THEN 140
110 IF A$ = B$ THEN 120
120 PRINT "Creature neighbor with a smile."
130 GOTO 160
140 PRINT "You didn't answer, 'Creature who?' so we have to start all over."
150 GOTO 20
The numbers from 10 to 160 provide the computer with two pieces of information. First, they give it a general guide as to the sequence in which the program should be run. Second, they give the computer a reference point in case it has to go back and perform an earlier instruction again. That makes the numbers akin to street addresses.
One way to look at it is to imagine you are told to walk down a street and visit each house according to its house number. You are to start with the house with the lowest number and end up at the house with the highest. You are also told that, at each address, you will find instructions and that you must follow them before you visit the next house in the sequence.
Why not just number the above program from 1 to 16 instead of 10 to 160? Answer: So that, should the program writer discover later that he needs to go back and add another instruction between two original ones, there is space to do so without having to renumber--and therefore rewrite--the program from that point on.
The term PRINT that first appears in line 10 is a relic from the old days when computers used printers rather than video display screens. It simply tells the computer to display the information that appears on that line on the video screen.
Line 30 tells the computer that the correct answer (designated as A is "Who's there?" The response you type in is, according to line 40, to be called "B$". That's important, because in line 50, the computer is told that, if B$ isn't the same as A$ (Who's there?), the computer is to go back to line 20 (PRINT "Knock-knock") and start going back up the block again.
For instance, had I typed in "Why am I doing this?" instead of "Who's there?", the computer would put me back at the start of the joke again. And it will keep doing so until I type in the correct answer.
Providing the right answer (line 60 where A$ = B gets the computer to move on to line 70 and display "Creature."
Lines 80 and 90 provide the computer with new criteria for the sorting out correct ("Creature who?") and incorrect reponses to line 70's "Creature." But the routine established earlier varies in line 100. There, should an incorrect response be detected, the computer is instructed to leap ahead a few addresses to line 140 and follow the directions it finds.
In this case, they are to tell the user that "You didn't answer, 'Creature who?" so we have to start all over." The computer then moves to line 150, which tells it to go back to (the BASIC instruction is GOTO) line 20 and begin the whole sequence again.
A correct answer (line 110) prompts the computer to move to line 120 where it is instructed to display the punch line. Then it moves to 130 where it is told to go to line 160. That's where it finds the entry END, telling the computer it's finished the job.
How does it all look on the screen? Here's how:
Hello, and welcome to Jennifer's fabulous knock-knock joke. Knock-knock.
? That means it's your turn Who's there?
Creature neighbor with a smile.
I bet you can't wait to hear her next joke, either.