QBasic Tutorials: QBasic for Beginners - Chapter 4

Chapter IV

Looping with QBasic

To make interesting and efficient programs, you can make QBasic to execute a part of a program more than once. This is called looping, when QBasic goes through a part of a program over and over again. This can be done with the GOTO command, but in QBasic there are some good ways to loop the program. One of them is FOR...NEXT command.

FOR...NEXT

This command allows you to execute a part of a program a certain number of times. It looks like this:

FOR i = 1 TO 4 PRINT "I am looping!" NEXT i

This little stupid program will print on the screen:

I am looping! I am looping! I am looping! I am looping!

The letter i can be any other letter, c for example. It is actually a variable, which changes its value each time the program loops (in this example - from 1 to 4). So, if you make a program like this:

FOR a = 1 TO 5 PRINT "This is loop number"; a NEXT a

this will print:

This is loop number 1 This is loop number 2 This is loop number 3 This is loop number 4 This is loop number 5

With FOR...NEXT you can use the STEP command, which tells QBasic how to count from one number to another. If you type:

FOR j = 0 TO 12 STEP 2 ~ ~ ~ NEXT j

it will count by two:
0, 2, 4, 6, 8, 10, 12

FOR j = 0 TO 6 STEP 1.5 ~ ~ ~ NEXT j

This will count:
0, 1.5, 3, 4.5, 6

You can also count backwards: FOR d = 10 TO 1 STEP -1 ~ ~ ~ NEXT d

When you want QBasic to count backwards, always put STEP -1 (or -whatever)!


DO...LOOP

Imagine that you have a program that works like an ordinary calculator: you enter numbers, QBasic calculates and shows the result, and the program ends. The program may be good, but one problem is that you have to run the program each time you want to calculate! That’s where the handy DO...LOOP comes in. It’s a block of comands, where the program doesn’t have to loop a certain number of times, like in FOR...NEXT. It can loop indefinitely, while the condition is met (and when it’s not met, the loop stops), or until the condition is met (so, when it’s met, the loop stops). Condition is basically the same as an argument, for example f < 20

Here is an example:

DO PRINT "Enter a number." PRINT "When you want to quit, press 0." INPUT n r = n / 2 PRINT n; "/2 ="; r LOOP WHILE n > 0 END

When you run this program, you can enter numbers and get the result as many times as you like. The program loops while numbers you enter are more than 0. Once you’ve entered 0, the program ends. The condition WHILE n > 0 is put by the LOOP command but you can stick it to the DO command, like that:

DO WHILE n > 0 ~~~ LOOP

Or you can use the word UNTIL instead, and put it either by DO or LOOP, like that:

DO UNTIL n = 0 ~~~ LOOP

All these examples have the same effect: the program loops while numbers you enter are more than 0 (or, you can say - until the number you’ve entered is 0). Then QBasic stops looping and goes to execute commands you put after the DO...LOOP block (if it’s END command, the program just ends).