Chapter Two
Program Looping
KeyWords: IF...THEN



We certainly covered a lot of ground in that first chapter, didn't we? Well you can relax just a little bit in this chapter. Here we will only introduce two commands, but boy, one of them sure is a doozey! It is a command that can make the computer appear to think. It is the decision making command of QBASIC. We will also let you in on a couple of shortcuts for some commands. But all in due time.

Let's get right into it. Sit in front of your computer, turn it on, and get yourself into QBASIC if you're not already there. We are going to have the computer do some counting for us. Let's enter the program now (in the program {upper} window), and we'll explain it afterwards.



LET COUNT = 1

MORE:

PRINT COUNT

LET COUNT = COUNT + 1

IF COUNT < 21 THEN GOTO MORE

END

Now press F5. Watch closely, it will go pretty fast. Let's explore the program. You should already understand lines 1 and 3, but can you explain line 4? Try to figure it out for yourself. Line 5 contains our new, powerful command. It is called the IF...THEN command. Here is what it does. The expression between the words IF and THEN is evaluated to find out if it is true or false. In this case it tests to see if the current value of COUNT is less than 21. If it is, the program executes the command after the word THEN. In this case, the program goes to the label MORE. If the expression is false, the program skips down to the next line, ignoring everything after the THEN on that line. In our case, the next line is 5, which has the END command, which stops the program.

Let's talk about labels for a little bit. Line 2 is a label in our little program. A label is a single word that is not a QBASIC command (we call those reserved words because they are reserved for a special purpose) followed immediately by a colon. A label is a way to tell the computer where things are. If you put a bunch of stuff in cardboard boxes and would want to be able to find some of that stuff later (without having to open all the boxes to find it), you yould put a label on that box. We are doing something similar here. We use a label to tell the computer where we will want to find something later on. In line 5, we look for that label, so that the computer program can jump to the next line (line 3) in the program and continue on from there.

Would you like to know a couple of secrets? No, they're not really secrets. They are shortcuts for two of the commands we've already learned. They make typing programs in a little easier. The first one is for the LET command. The word LET is actually optional! In other words the line

LET VALUE = 17
is identical to the line
VALUE = 17.
The other shortcut is for the PRINT command. Since it is used so often, the writers of QBASIC decided to use a shortcut for typing it in. It is the question mark (?). It is conveniently located next to the right shift key, which makes printing things quite fast and easy. Using these two shortcuts, the above program would have been typed in like such:



COUNT = 1

MORE:

? COUNT

COUNT = COUNT + 1

IF COUNT < 21 THEN GOTO MORE

END

Notice that when you press the ENTER key after typing line 3 that the ? is automatically converted to PRINT, but the words LET will not be added. They are not considered necessary. Just a couple of ways to make typing programs easier. We will continue to use the word LET in all our future program listings, as well as the full word PRINT. You can leave out the LET and use the question mark character to save some typing, though.

Have you noticed that when you print something that it only prints one thing on a line? Seems a waste of space, doesn't it? What if you wanted two pieces of data on one line? Here is our first advanced function of the PRINT command. It's called the comma, and it works like this. Press F6 to switch to the Immediate window. Type this in:

PRINT 2,4,6

(Don't forget to press ENTER - We won't be telling you to do that anymore) and notice what happens. You get those three numbers back on the same line, separated by about 7 spaces. What the comma does is perform like a Tab key on a typewriter. The only thing about the computer is the tabs are automatically set for every fifteen character positions on most computers, and they cannot be altered; you're stuck with them. It comes in handy for doing columns, though. Let's write another program now. This one will display a list of numbers from 1 to 15, and show each number multiplied by 2, and multiplied by itself (that would be its square). Press F6 to switch back to the program window, erase any existing program by selecting New from the File menu (if you need to), and type this program in:

LET NUMBER = 1

AGAIN:

LET DOUBLE = NUMBER * 2

LET SQUARE = NUMBER * NUMBER

PRINT NUMBER, DOUBLE, SQUARE

LET NUMBER = NUMBER + 1

IF NUMBER < 16 THEN GOTO AGAIN

END

Now Press F5 and watch the program fly! Incidently, the PRINT command can end with a comma, such as PRINT RESULT, and the next time you run across a PRINT command in the program, it will pick up in the next column on the same line. In other words, it defeats the carriage return function of the PRINT command. Flexible, huh? Just wait until later, when we tell you even more about the PRINT command!

Do you want to spice up that last program with column headings? Sure you do. Why? Because this is where you are going to learn how to print words out! Add this line to the very top of the program (Put the cursor at the top of the screen by pressing CTRL-HOME):

PRINT "#" , "#*2" , "#*#"

and then press ENTER to move the rest of that line down. Make extra sure that you have those quotation marks in the right place! Now press F5 and notice the program output. See how everything that is inside a pair of quotation marks in a PRINT command gets displayed on the screen exactly the way it is typed into the program.

Now that we have a decent running program, you may want to save it for posterity. Select "Save As..." from the File menu. You will be presented with a box that asks for a filename. Type in any legal DOS filename that you want. You are limited to 8 characters, with no spaces. You may use letters, digits, dashes, underlines, and a handful of other symbols. Don't put a filename extension (like .BAS) because QBASIC will add it automatically.

With the cursor in the File Name box, type in

MULT

and press ENTER. If everything went OK, you will receive no error messages, and the word "Untitled" at the top of the program window will change to "MULT.BAS", indicating the name of the program.

We need to talk about typing errors. Erase any existing program, and type this in:

PRIUNT ANSWER

Obviously, the word PRINT is spelled wrong. Something about large fingers, I think. You may notice that if you typed the program in lower case that QBASIC did not automatically convert the word into capital letters. That's because QBASIC doesn't know what the word PRIUNT means; It thinks you are going to use it as a variable. However, when you press F5 to run the program, the computer scans the program for errors, highlights the unknown word, and presents a white box that says "Syntax Error". Syntax is a tax that you pay on syn. Just kidding. That means that it doesn't quite understand what you are trying to do. To correct this error, click on OK in the white box, move the cursor under the U, and press the Delete key once to remove the offending letter. QBASIC behaves just like a text editor! DO NOT PRESS ENTER AFTER DELETING THE "U", OR YOU WILL SPLIT UP THE LINE! Just press the Down Arrow once, and you will see the word "print" converted to upper case. Go ahead and press F5 again, and the program will run correctly. The answer will be zero, of course.

Let's try another IF...THEN program. Clear out any existing program and enter:

LET A = 1

MORE:

LET B = A*A

PRINT A, B

LET A = A+0.1

IF B<2.4 THEN GOTO MORE  

END

See if you can predict what will happen here. You don't have to know on just what number the program will end (unless you can figure square roots of fractional numbers in your head!), but see if you can figure out what value that the variable B will not go over. Then RUN the program to see if you are correct. I'll bet you're wrong! I was! It's because the PRINT comes before the IF...THEN.

Here are all of the relational operators that can be used with the IF...THEN statement:

"=" is equals; the expressions on both sides of the equal sign must be numerically equivalent
"<" is less than; just like in grade school math class.
">" is greater than; grade school again.
"=<" or "<=" is less than or equal
"=>" or ">=" is greater than or equal
"<>" or "><" is not equal; if the two sides are different, the THEN GOTO label will be jumped to.

Try each relational operator in an IF...THEN statement and see what happens. Now you can write lots of programs! You've learned how to put output into columns using the comma in the PRINT command. By the way, if you want to move over two columns, just put two commas together like this:

PRINT FIRST,,SECOND

and that will do the trick! You also know how to print words or letters by using quotation marks in the PRINT command. You know how to create a program loop using the IF...THEN command. A loop is a section of program that gets executed over and over again (usually changing the variables each time through) until a certain condition is met (that is what the IF...THEN tests for). By the way, if something goes wacky, and your loop keeps going and going and going and going and going and going, you can stop the program by holding down the Ctrl key and pressing the Break Key. The word Break is usually printed on the front of a keycap on the keyboard. It is usually either on the Scroll Lock (sometimes abbreviated ScrlLk) key or the Pause key on keyboards that have it. Both keys are toward the right side of the keyboard. You may release the Ctrl key after releasing the Break key. QBASIC will stop running the program, and return you to the program window. The next line that would have been executed will be displayed in bright white. To continue the program from where you left off, just press F5 again. To start over from the top, press SHIFT-F5. You may make changes to the program after stopping it with the BREAK key. Some changes will require you to start the program from the top again. If so, QBASIC will warn you about that.

Next chapter, we'll show you how to load a QBASIC program that you saved earlier, as well as another way to set up a loop without using an IF...THEN command. But in the meantime, have fun using and learning about what can be done with that pesky little IF...THEN command!

Introduced In This Chapter:
Keywords: IF...THEN

Concepts: Program Labels, Typing Shortcuts, Basic Output Formatting, Printing String Constants, Program Line Editing, Relational Operators, Stopping a Program Manually, Continuing and Restarting a Stopped Program.




Previous Chapter Next Chapter