## QBASIC Looping

QBASIC Looping Tutorial

Section 1 – The FOR loop
Programming generally has two ways to control program flow. The first (which you should have learned) is a control statement. Control statements include things such as: IF/THEN SELECT/CASE and GOTO. All of these are loops stripped down to bare-bones. Also a subroutine can be continuously called, creating a loop. Consider the following:

CLS
start:
PRINT “Hello”
GOTO start

Obviously, this program will never end. It will just keep printing the string, “Hello”. But what if you only wanted the program to print “Hello” ten times? What probably comes to mind is something similar to the code below:

x = 0
CLS
start:
IF x &lt; 10 THEN
PRINT "hello"
x = x + 1
GOTO start
END IF
END

This may seem like a viable solution to the problem, but it is not the best, nor the quickest. When it comes to a program like the one above, performance may not be on your mind. But when developing large scale applications, speed and stability are essential to the success of the software. Below, I will write the same program, but using a FOR loop:

CLS
FOR x = 1 TO 10
PRINT "hello"
NEXT x

As you can see, this program is much smaller. By viewing this, I’m sure you have already figured out the FOR loop, but I will discuss it anyways. You will notice that aside from the declaration of the loop itself, there are a few other main components. One is what is called “the counter.” The counter is a numeric variable, x, in this case. The other two work together (start and end) to create a value range for the counter. x = 1 TO 10 means that x will start at 1 and increase everytime NEXT x is encountered. You may also use x = 0 TO 9 to get the same effect.

Traditionally the variable increases by one for every instance of the loop, then returns to the first statement in the loop…but this can be modified. The following program will also display “hello” ten times, but will use the increment operator STEP:

CLS
FOR x = 1 TO 20 STEP 2
PRINT "hello"
NEXT x

The STEP statement means that x will increase by 2 every time NEXT x is encountered. The STEP statement can also be used to decrease a variable:

CLS
FOR x = 10 TO 1 STEP -1
PRINT "hello"
NEXT x

But what if you had a loop that you wanted to exit if a certain condition held true? This is where EXIT FOR comes into play:

CLS
FOR x = 1 TO 10
PRINT "hello"
IF x = 8 THEN EXIT FOR
NEXT x

Notice that the program will only print “hello” eight times due to the conditions set by the IF/THEN statement.

Section 2 – The WHILE loop
Another loop that is commonly used is large scale applications, especially with other programming languages, is the WHILE loop. A WHILE loop will loop forever, as long as an initially set condition proves true. First, I will show you a simple use of the WHILE loop:

CLS
WHILE x &lt; 10
PRINT “hello”
x = x + 1
WEND

The WHILE loop is an easier way to do constant testing of a value. Nested IF/THEN statements would make the code’s logic very difficult to follow, and make it messy as well. Below is a program that gives you ten “lives”, or chances, to complete a task, much like a game would. The program generates a random number 1-100 and asks you to guess it. Every guess, it reprints your health status and evaluates if you are alive or dead.

CLS
RANDOMIZE TIMER
num = INT(RND * 100)
health\$ = "alive"
health = 10
PRINT "(Enter 0 to quit)"

PRINT "You are "; health\$; " with"; health; "lives left."
PRINT ""
INPUT "Guess a number 1 - 100"; guess

IF guess = num THEN
PRINT "One Up!"
health = health + 1
IF health &gt; 5 THEN health\$ = "alive"
num = INT(RND * 100)
ELSEIF guess = 0 THEN
END
ELSE
health = health – 1
IF health = 0 THEN health\$ = "dead"
IF health &lt; 6 THEN health\$ = "dying"
END IF
WEND
PRINT "You are "; health\$
END

Aside from the RND and RANDOMIZE TIMER statements, this program should be easy to follow for you. The WHILE loop’s logic is as follows; as long as you are not dead, continue with the game as stated below, but if you are dead, end the guessing by ending the WHILE loop.

Section 3 – Using DO…LOOP

Though native to the BASIC language, DO...LOOP is one of the most versatile loops available. The DO has the ability to do a task while a condition is true, or until a condition is true, and it can test for this condition at the beginning or the end of the loop. The following programs accomplish the same task:

CLS
DO WHILE x &lt; 10
PRINT "hello"
x = x + 1
LOOP

CLS
DO UNTIL x = 10
PRINT "hello"
x = x + 1
LOOP

CLS
DO
PRINT "hello"
x = x + 1
LOOP UNTIL x = 10

CLS
DO
PRINT "hello"
x = x + 1
LOOP WHILE x &lt; 10

Notice the similarities to the WHILE loop. There is also a statement to exit the DO loop prematurely if it were needed: EXIT DO. Make a loop of your own. You can combine loops of same/different types to create nested loops, much like nested IF/THEN statements.