|
"Loops" make it easier to do an action multiple times. There are at least four types of loops: IF...GOTO, WHILE...WEND, DO...LOOP, and FOR...NEXT. IF...GOTO This program uses IF...GOTO to create a loop:
start: PRINT x x = x + 1 (This adds 1 to x) IF x < 15 THEN GOTO start
11 12 13 14 WHILE...WEND The WHILE...WEND commands continue a loop until a specified expression is false. To use WHILE...WEND:
Run the following:
WHILE x < 15
x = x + 1 WEND
11 12 13 14 DO...LOOP DO...LOOP is exactly the same as WHILE...WEND, except it has at least two slight advantages. With DO...LOOP you can:
To use DO...LOOP:
The following uses the WHILE statement:
DO WHILE x < 15
x = x + 1 LOOP This program uses the UNTIL statement:
DO UNTIL x = 15
x = x + 1 LOOP They both output:
11 12 13 14 If you place the expression at the end of the loop instead, the program goes through the loop at least once.
DO
x = x + 1 LOOP WHILE x < 5
FOR...NEXT FOR...NEXT provides an easier way to create a loop.
NEXT x
2 3 4 5
Also, you can use the STEP attribute to specify how much X will be increased each time through the loop.
NEXT x
3 5 STOPPING LOOPS To stop a loop prematurely, use the EXIT command, followed by either FOR or DO.
IF (x = 3) THEN EXIT FOR NEXT x
2 3 Feel free to distribute this tutorial, upload it to your website, link to it from your site, etc. http://www.geocities.com/progsharehouse/qbtutor Mirror: http://development.freeservers.com/qbtutor |