Chapter Three
Program Looping, Continued
KeyWords: FOR...NEXT

Let's kick off this chapter by learning how to load a program that was saved to disk at an earlier time. This is where the wonderful menu system comes in. See that line at the top of the screen that has the words "File Edit View Search" and so on? Those are all menus. Since we are dealing with something that is on the disk (that's where we store our program as a file - get it?), open the File menu by either clicking on the word "File" with the mouse, or holding down the "Alt" key while pressing the "F" key (This will be referred to as "Alt-F" in the future). Notice that a menu pops down, with the words "New, Open", etc. Click on "Open". A big box will pop up in the middle of the screen that has a handful of other smaller boxes inside it, labeled "File Name", "Files", and "Dirs/Drives". If you are in the same directory that you were in when you went through Chapter 2, you should see a file called MULT.BAS in the Files box. To load it, just double-click on that file name with the mouse, or use the Tab key to move the highlight bar to the "Files" window, and then the arrow keys to move the highlight bar to the desired file (if there's more than one - If it's the only file, you will have to press the spacebar to get the highlight to show up). Press Enter to load the file, and it will appear in the upper window. Neat, huh?

Notice that you have the program that you wrote in Chapter Two. You may run the program if you wish. Incidently, anytime you load a program into memory, any existing program is automatically erased! Be sure that the current program is saved (if you want to keep it) before loading in a new one!!! If you try to load a program in without saving the current one, QBASIC will tell you about the error of your ways.

As promised last chapter, we are now going to show you a new way to set up a program loop. This new method is much easier, albeit at a very slight expense of flexibility. For the most part, however, this new method will work just about anywhere that the one you are using now works. For those rare cases where this new form wouldn't work, there is even another type of looping statement that usually will. That will come in the Advanced section, though. Enough chatter, let's get to a program. Erase any existing program in memory by selecting New from the File menu, and enter the following program:

FOR NUMBER = 1 TO 30

     PRINT NUMBER

NEXT NUMBER

END

It seems that we only understand two of the lines here. Are we learning two new commands? Actually, no. It is one statement that happens to take up two lines. The first part of the statement is in the first line, and has the basic form: FOR variable = initial value TO final value. Let's explain that in regular English, using the example we have here. What happens is that the variable NUMBER is given a value of 1, as shown in line 1. It's almost like a LET command saying LET NUMBER = 1. Now the program drops down to line 2 (the indented line), and the variable NUMBER is printed. The program then goes on to line 3 and sees the command NEXT NUMBER. What does that mean? Well, it adds 1 to the variable NUMBER, and checks back with the FOR statement to see if it has exceeded (in our case) 30. That 30 in the first line is called the final value. Anyway, if the variable NUMBER has not exceeded that final value, the program jumps to the command right after the FOR statement, in our case, line 2. The new value of NUMBER is again printed (it is 2 this time around). Then we get to NEXT NUMBER again. Once more, the value of NUMBER goes up by 1, and is checked with the final value. Now let's assume that we have gone through it 29 times already, and are at line 2. The value of NUMBER is printed (29), and we get to NEXT NUMBER again. NUMBER is incremented by one, so it is now 30. The program checks this value against the final value, which is also 30. In the rules of mathematics, 30 is not greater than 30 (they are equal), so the loop is executed once again, with NUMBER having a value of 30. That value gets printed out in line 2, and then we get to NEXT NUMBER again. Number goes up by one again, and is checked against the final value. Now that NUMBER is 31, QBASIC knows not to go back through the loop again.
Let's try some interesting things with the FOR...NEXT statement. Erase the current program (by selecting New from the File menu), and type this program in:

PRINT "Number","Square"

FOR NUM = 1 TO 50

     PRINT NUM, NUM * NUM

NEXT NUM

END

Now go ahead and run the program. Zips by quickly, doesn't it? Let me throw a monkey wrench into the works here. What happens if you wanted to count backwards from 50 to 1? Well, it sounds like all we have to do is change the second line. Well, that's half right. So far our new line 2 would look like this (don't type it in; it's wrong!):

FOR NUM = 50 TO 1

You already know that is wrong, but why? Let's trace through the program. On the first time around, NUM will have the value of 50. Ok, so it prints 50 and 2500 on the first pass of line 30. Now we get to line 40, and NUM goes up by one, now with a value of 51. Let's see, 51 is greater than 1, so we are finished with the loop. Execute the next line, which says END. Program finished. Hold on, here! It only went through it once! How do we get it to decrement the variable instead of incrementing the variable? Well, that is when we use an optional part of the FOR...NEXT statement. It is called the STEP size. Let's type in the correct line 2 first Replace the second line with:

FOR NUM = 50 TO 1 STEP -1

Normally when you get to the NEXT statement, the variable is incremented by 1. By using the STEP statement, we can set that increment to anything we want. In this case we tell the computer to add minus one to the variable at the NEXT statement, which causes it to go down by one instead of up by one. When a negative number is used for the STEP (as it is here), the rule for the final value changes just a bit. Instead of checking to see if the current value is greater than the final value, it checks to see if the current value is less than the final value. All else is unchanged. Go ahead and run the program. Notice that it counted down from 50 the way we wanted it to. Let's try something else.

Let's have the computer print out all the even numbers from one to 100. Well, we know that 1 is not an even number, but that 2 is. So we know that the loop will start with the value of two. We also know that every other number is even. Can you guess the STEP size? Don't look at the answer below until you guess!

Here's the new program:

FOR NUM = 2 TO 100 STEP 2

     PRINT NUM,

NEXT NUM

END

Did you notice our use if the comma in line 2? That way, you can see the entire output of the program on one screen. Notice that it printed out all of the even numbers from 2 to 100. What do you think would happen if you changed the final value in the first line to 99? What about a final value of 101? Type them in and see for yourself!.

Why do we have all of the stuff between the FOR and the NEXT lines indented? That makes it easier for us to see exactly what part of our program will be repeated many times! It doesn't mean much now, but you will find out that it is possible to put a FOR...NEXT loop inside of a bigger FOR...NEXT loop, which can be inside of an even bigger FOR...NEXT loop! You can see how this can get to be a bit confusing; the indentation just makes it a little bit easier to see just where you are.

That's all we'll do for this chapter. We thought you deserved an easy one for now, the last couple were kind of rough. Keep practicing on your PRINT command, your IF...THEN command and now your FOR...NEXT loops. Here are a few programs for you to try:

Print all the odd numbers from 1 to 250;
Compute the factorial of 9 using a FOR...NEXT loop;
Add the numbers from 1 to 750 using a FOR...NEXT loop.

We'll give you the answers to the last two problems in the next chapter. Meanwhile, you are wondering what a factorial is. Nine Factorial is 9 times 8 times 7 times 6 times 5 times 4 times 3 times 2 times 1. If you write that program correctly, you can find the factorial of any positive number up to 33 (the answer of which is in the numerical capability of the computer; 34 and over will give you an overflow error) by simply changing one number in the FOR...NEXT statement. The correct result for the second problem is 362880, and for the third problem is 281625.

Next chapter, we will show you how to place and use data in a program, and if you're nice, how to have the program ask you to type in data from the keyboard while it's running!! See you next time!!




Introduced In This Chapter:
Keywords: FOR...NEXT

Concepts: Retrieving a file from floppy or hard drive, a more efficient way of setting up a loop.




Previous Chapter Next Chapter