Understanding For, Do, and While Loops - Visual Basic 6 (VB6)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Understanding For, Do, and while Loops

Level:

In our tutorial about if statements in Visual Basic 6 (Understanding if statements and conditions in VB6) we describe how conditional statements work - these can be used in loops as while so if you have not read that tutorial please do so now. Now continueing on with our original problem we described in our if statement tutoria how do we generate the Fibonacci series. Its formula is relatively simple: 1. Let x = 1, let y = 0. 2. Print x. 3. Let x = y + x 4. Let y = x - y 5. Repeat from 2. This pseudo code will print out: 1, 1, 2, 3, 5, 8, 13, 21, ... as expected. But now how do we tell a Visual Basic program how to do this? The answer lies in using loops. Loops provide the ability to repeatedly execute the same block of code, and to each time change values such that each run through the loop produces different results. Visual Basic provides four main kinds of loops: the classic Do-Loop, the Do-Until Loop, the Do-While Loop, and the For-Next Loop.

Do-Loops The most basic form of loop in Visual Basic is the Do-Loop. Its construct is very simple: Do (Code to execute) Loop This, quite simply, executes the block of code, and when it reaches Loop, returns to the beginning of the Do Loop and executes the same block of code again. The same block of code will be repeatedly executed until it is told to stop executing. So let's try to apply this to our problem of generating the Fibonacci series: D i mXA sI n t e g e r D i mYA sI n t e g e r D o D e b u g . P r i n tX X=Y+X Y=X-Y L o o p

And, believe it or not, this code works! Well, sorta. If you try to run this code, it will indeed generate the Fibonacci series; however, it will continually generate and print out the next number infinitely--or, in this case, until it reaches an overflow error. This is known as the problem of the infinite do-loop, one that all programmers will experience, and some quite frequently. Exit Do So we clearly need some way to escape from the Do-Loop. You could, of course, simply End the program once you have calculated enough values, but what if you still need to perform tasks after you're done calculating? The answer is to use the Exit Do statement. Whenever your program reaches an Exit Do statement within a loop, it will exit the current loop. So, let's try a somewhat different approach to the Fibonacci problem. We decide that we want to calculate only eight values of the Fibonacci series, so we'll keep a counter and increment it each time throughout the loop. Then, once the counter reaches eight, we'll exit the loop. P u b l i cS u bM a i n ( ) D i mXA sI n t e g e r D i mYA sI n t e g e r D i mc n tA sI n t e g e r' O u rc o u n t e r . c n t=1 D o D e b u g . P r i n tX X=Y+X Y=X-Y

I f

And now we're talking! This program successfully computes and prints out the first eight values of the Fibonacci series. Do Until As an alternative approach to nesting an If-Statement inside the loop, and invoking Exit Do once we're done looping, Visual Basic provides a Do Until statement. Its syntax is the following: Do Until (Expression) (Code to execute) Loop (Expression) can be any legal logical expression that we wish to evaluate to determine whether or not to exit the loop. Each time the program reaches Loop it will evaluate this expression. If the expression is True, it will exit the loop for us, but otherwise it will continue looping.. So let's try rewriting our Fibonacci program to use a Do-Until loop instead of Exit Do. P u b l i cS u bM a i n ( ) D i mXA sI n t e g e r D i mYA sI n t e g e r D i mc n tA sI n t e g e r' O u rc o u n t e r . c n t=1 D oU n t i lc n t> =8 D e b u g . P r i n tX

X=Y+X

Here we've replaced the hideous If cnt >= 8 Then ... Else: Exit Do with a very simple Until cnt >= 8. We must, however, still be sure to increment our counter every time through the loop, or else the Until expression will never be True, resulting in an infinite Do Loop. Do While

In the place of Do Until, you can also use Do While. Its syntax is the following: Do While (Expression) (Code to execute) Loop (Expression) can be any legal logical expression that we wish to evaluate to determine whether or not to exit the loop. Each time the program reaches Loop it will verify that this expression is True, and if it is False, it will exit the loop for us. Thus, instead of exiting when an expression is True, it now exits only once this expression is false. Let's try rewriting our Fibonacci program to use a Do-While loop instead of a Do-Until loop. P u b l i cS u bM a i n ( ) For-Next Loops In situations where you merely want to run the loop a predefined number of times, it can become quite tiresome to have to create and manage a counter for each loop, which is why we also have something called a For-Next Loop. This kind of loop allows you to specify a counter, to tell it to count from one number to another each time through the loop, and to exit once the counter has reached its upper limit. The syntax is as follow: Dim I As Integer For I = (Integer) To (Integer) (Code to execute) Next I We used the variable name "I" above, as it is the most common name used for For-Loops; however, you can use any variable name you want, so long as the variable is of the type Integer. Now, let's improve our Fibonacci program even further: P u b l i cS u bM a i n ( ) D i mXA sI n t e g e r D i mYA sI n t e g e r D i mc n tA sI n t e g e r' O u rc o u n t e r . F o rc n t=1T o8 D e b u g . P r i n tX X=Y+X Y=X-Y D i mXA sI n t e g e r D i mYA sI n t e g e r D i mc n tA sI n t e g e r' O u rc o u n t e r . c n t=1 D oW h i l ec n t<8 D e b u g . P r i n tX X=Y+X

Y=

L o

In the example above, we first dimensioned cnt as an Integer, and then, in the declaration of the For-Next loop, set its value to 1. Each time through the loop, the value of cnt was incremented by 1 until it reached 8, at which point the loop was executed. Exit For As with Do Loops, there is a statement that can be used to exit a For-Next loop, and it is called Exit For. Simply invoke this statement anywhere within a For-Next loop and the current loop will be exited. Step By default, the variable used in the declaration of the For-Next loop is incremented by 1 each time through the loop; however, if you want to increment this value by a different amount each time through the loop, you can simply append Step (Integer) to the end of the For-Next loop declaration. If, for instance, we wanted to print out every even number counting backward from 20 to 0, we could do this using the following code: D i mIA sI n t e g e r F o rI=2 0T o0S t e p2 D e b u g . P r i n tI N e x tI

So there you have it now you can use loops all over your Visaul Basic 6 programs. These are one of the most useful tools you have. You might want to bookmark this tutorial so that later you can reference back to this great VB6 loop examples. If you have any questions or comments please post them below.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy