Classic Computer Magazine Archive Article from Compute! magazine

THE BEGINNER'S PAGE

Tom R. Halfhlll, Editor

IF-THEN Intelligence
At one time or another you've probably seen the term artificial intelligence. It refers, of course, to computer intelligence-the ability of a machine to reproduce (or, if you prefer, simulate) some of the thought processes of a human being.
    We're not going to reopen here the philosophical debate about whether computers are really intelligent, or if they ever will be intelligent (or for that matter, if humans are intelligent). Scientists still can't agree on exactly how the human brain works, much less whether it can be duplicated in silicon circuitry.
    But we do know how computers work. Although computers aren't (yet) capable of independent thought or action, they certainly appear intelligent at times. They can play chess at the grandmaster level, forecast tomorrow's weather as well as anybody else, help plan the economy of a household or a nation, create wonderfully abstract art, and even simulate the responses of a psychoanalyst closely enough to fool many laypeople. How can a mass of wires and silicon chips seem to be so smart? What sets computers apart from all other machines?
    Programmability alone isn't the answer. There were programmable machines long before computers came along. One example is the centuries-old music box. A melody is programmed into the box by punching little bumps onto the surface of a revolving drum; as the drum turns, the bumps pluck a series of tiny metal prongs tuned to different notes. Of course, a music box is capable of playing only one melody drum, or "program." A more sophisticated example is the player piano, with its interchangeable paper rolls that operate on the same principle.
    Still, there's something missing from a programmable player piano that keeps it from qualifying as a true computer. Even some of today's programmable calculators lack the essential element of computer intelligence. They, too, can be programmed to carry out a series of steps, but they can't imitate the decision-making power of a real brain.
    What do computers have that all these other machines don't? Conditional logic. Although some other devices are capable of conditional logic on a very primitive level, no machine can do it as flexibly as a computer.

Michael Jackson Vs. Beethoven
Here's how conditional logic works. Let's say you send a friend to a record store with a $10 bill and these instructions: "If the store has Michael Jackson's latest album, then buy it for me. Otherwise, buy the Cleveland Symphony Orchestra's new recording of Beethoven's Fifth."
    Now, you've done more than simply programmed your friend to visit the store and buy you a record. You've given him the power to make a decision in your absence, and also the information he needs to make the decision. Depending on whether a certain condition is met (if the store has Michael Jackson's latest album or not), your friend will act on either of the two alternatives (he'll buy you the Jackson record or the Beethoven record). Even if your friend has the brains of a hamster, he'll appear semi-intelligent to the record store clerk as he flips through the bins and picks the correct album.
    All computer programming languages have commands that let you tell the computer to make the same sort of decisions. In BASIC, the most common command for conditional logic is the IF-THEN statement. It takes this form:

IF condition is met THEN perform this action.

    Computers don't understand English, of course, so the italicized words above must be replaced with terms the computer can understand. Usually the conditional part of the statement involves a comparison between variables and numbers. And often the resulting action will be a second command which sends the computer to another section of the program. Let's try some actual examples.

Conversational Computing
At one time or another you've probably used a computer program which carries on a conversation with you, depending on how you respond to certain questions. The machine appears almost human, and conditional logic is the key.
    Let's say you're an insurance salesman who is writing a program designed to analyze a client's life insurance needs. One of the first questions the program needs to ask is the person's age. This can be done with a simple PRINT statement. Clear the computer's memory by turning it off, then on again, and type the following line exactly as it appears (remember that to enter a program line into memory, you must press the RETURN or ENTER key after typing the line):

10 PRINT "What is your age";

When the program runs, the PRINT statement will print the text between the quotes on the screen. Next, type this line:

20 INPUT A

When the program runs, this simple statement does three things. First, it prints a question mark after the text in the PRINT statement. Second, it makes the computer pause until someone types in a number on the keyboard. Finally, it takes the number and stores it in a memory location which can be referenced with the variable name A.
    Now it's time for some conditional logic. Enter these two lines:

30 IF A>100 THEN PRINT "It's a little late to be
thinking about life insurance, isn't it?":END
40 PRINT "We have just the policy that you need."

    Now clear the screen and run the program. When it asks for your age, try typing in a number less than 100. Then run the program again and type in a number greater than 100. See the difference? (Note: This program requires Extended BASIC on the TI-99/4A.)
    Here's how it works. Line 30 compares the value stored in the variable A with 100. If A is greater than 100 (> is the greater-than sign), then the condition is met and the computer performs the instruction which immediately follows. The second half of an IF-THEN statement is executed only when the condition in the first half is true. (Incidentally, line 30 is also a multistatement line; a colon separates the second statement, END, from the IF-THEN statement. This command ends the program after the remark is printed.)
    If the user claims to be less than 100 years old, the condition in line 30 is not met. Therefore, the computer ignores the rest of line 30 and continues (or "falls through") to the next line. The computer prints a different message on the screen and, presumably, would go on to the remainder of the program.

Simulated Intelligence
Although this simple four-line program contains only one conditional statement, it illustrates how computers can appear intelligent. The computer not only makes different responses depending on the user's input, it also seems to know that centenarians are unlikely candidates for life insurance. It even reveals a snappy sense of humor. Of course, it's really the programmer talking, not the computer. (Remember this the next time your computer makes a rude remark.)
    Simple conditional statements like the one above are the basis for nearly all of what passes as artificial intelligence today. All programs work on the same principle, from the sophisticated modeling software that forecasts the nation's economy to the chess program which threatens the supremacy of the world's top champions. In fact, the chess program published in the December 1984 issue of COMPUTE!, when playing on level 5, uses a similar technique to evaluate up to 50 million possible moves during each turn. Naturally, a program that complicated must be written in machine language, not BASIC, or you'd be waiting months for the computer's response.
    The IF-THEN statement isn't the only way to simulate intelligence in BASIC. Most BASICS have at least two other statements which accomplish more or less the same thing. This indicates how important conditional logic really is in programming. (Linguists say you can determine a language's most often used concepts by counting the synonyms-interestingly, somebody once figured out that English has more terms for being inebriated than almost any other concept.)
    In BASIC, as we mentioned, IF-THEN is by far the most common conditional statement. Some of the more powerful BASICs - such as IBM BASIC - augment the IF-THEN statement with an ELSE condition. This lets you combine two lines into one. For instance, lines 30 and 40 above could be rewritten like this in IBM BASIC:

30 IF A<100 THEN PRINT "We have just the policy for your needs." ELSE PRINT "It's a little late to be thinking about life insurance, isn't it?":END

    ELSE doesn't let you do anything you couldn't do otherwise; it just makes programming more convenient.
    Other examples of conditional statements in BASIC are ON-GOTO and ON-GOSUB. In effect, these let you combine a whole series of IF-THENs into one compact line. They are also called conditional branching statements because they branch to other parts of the program. An IF-THEN statement can be used for conditional branching, too. We'll cover both conditional and unconditional branching in next month's column, and also show a couple of ways to avoid long, cumbersome lists of IF-THENs in your programs.



Questions Beginners Ask
Q What exactly is a crash or a freeze and how can I avoid it? All I know so far is that it can happen when one mistypes numbers in DATA statements, but not why, nor what else can cause it, so I can't yet reason backward from crash to cause. I've been trying to translate a budget and cash-flow analysis program written for the IBM PC so it will run on a Timex Sinclair TS-1000 with a 16K RAM pack, which in theory should take a program up to 900 lines long. But I can't even begin to type it in. The first 30 lines contain a DIM block of about ten lines and other statements. Before I can finish, I get a crash: blank screen, no cursor, keyboard dead, BREAK and STOP keys disabled. Nothing to do but unplug, replug, and start from scratch. Nothing here seems similar to mistyping figures in DATA statements, and I don't see how I could be running out of memory so soon (the manual says you will eventually-not bang all of a sudden-reach a blank screen when you've jammed the poor beast beyond capacity).
Norman Hartweg

A You've described the symptoms of a system crash or freeze perfectly-the screen often goes haywire, the computer won't respond to commands typed on the keyboard, and your only alternative is to power down and wipe out whatever you were working on.
    Disregarding rare hardware failures, system crashes generally happen when the computer gets stuck in what's called an infinite loop at the machine level. This means the computer tries to execute a series of instructions which loop back on themselves or cancel each other out. The computer might look paralyzed, but it's really very busy trying to accomplish the impossible. So busy, in fact, that it ignores everything else, including your demands for attention on the keyboard.
    As an example, suppose you told somebody to resolve these two statements: "Assume everything I say is always the truth. Now, I'm telling a lie." If everything you say is the truth, then you're telling the truth when you say you're lying. But if you're really lying, then everything you say isn't always the truth. But it's a given that you never tell a lie ... and so on. You can go back and forth like this forever-except you're smart enough not to try, and a computer isn't. (Star Trek fans may recall how Captain Kirk used this classic paradox to provoke an android into a system crash and free his crew.)
    System crashes can happen when DATA statements are mistyped, because DATA numbers often contain machine language subprograms. A mistyped number can create a wrong instruction, which in turn can trap the computer in an endless loop. Typing the wrong number in a POKE statement can do the same thing.
    However, these crashes happen only when the program runs, not when it's being typed. As you surmised, your symptoms indicate a memory problem. Check these possibilities:
    1. The 16K RAM pack on a TS-1000 fits rather loosely. If it's not plugged in all the way, the computer may not be recognizing the extra memory, leaving you with only the 2K internal RAM, not the 16K you think you have. Also, if the pack wiggles when you type on the keyboard, the faulty connection can crash the computer.
    2. The DIM statements you mentioned at the beginning of the program may be eating up all your RAM. DIM statements aren't like other statements; they take up more memory than just the characters they're composed of. The purpose of a DIM statement is to reserve a block of memory for an array (a series of related variables). In Sinclair BASIC, each element of a numeric array consumes five bytes each. The statement DIM X(375) might look pretty short and harmless, but it actually consumes so much memory that it causes an error when typed into a TS-1000 with 2K of RAM. If the DIM statements are the source of your trouble, you'll have to scale down the IBM program to fit it into your Timex.