Site hosted by Angelfire.com: Build your free website today!

Loops in QBasic

By QBasicBoy



First of all, let's all define a loop as it will be used in this tutorial. A loop (in QBasic) is the repeating of code until a designated stop point is reached.

There are a couple of ways to loop in QBasic, they all use relatively the same code. Let's discuss the first way:

The Do and Loop statements:

CLS
X = 1
DO
PRINT X
SLEEP 1
CLS
X = X + 1
LOOP UNTIL X > 10


In that program, it will loop all of the code between the DO and the LOOP statements...it will count to ten....The X = X + 1 simply means that the variable X will be increased by one, every time the code loops, think about it for a second, and you will begin to understand.

The next way to loop code is with the WHILE AND WEND statements. They work in relatively the same way...

CLS
X = 1
WHILE X < 10
PRINT X
SLEEP 1
CLS
X = X + 1
WEND


There are many more ways to use loops in QBasic, here is the last one I will show you:

CLS
X = 1
FOR N = 1 TO 10
PRINT X
SLEEP 1
CLS
NEXT N


This one is a bit more complicated, all of the text between FOR N = 1 TO 10 and NEXT N get repeated. You can replace N with any variable that you want. Now, the 1 TO 10 tells QBasic how many times N will loop itself. The NEXT N tells QBasic to return to the line of code after FOR N = 1 TO 10.

You may not think that loops have any significance in QBasic right now, but once you get into graphics or programs that need to count something out, you will see how important loops are.

I hope that this tutorial was self explanitory, but if it wasn't, please e-mail me and I will try to answer any questions that you have about loops.

Created by QBasicBoy
Last modified 7-7-99