loops.gif (2411 bytes)

Exclamtiub.gif (188 bytes) Loops are used to loop around .. duh, well they are really used when you want to keep repeating something, until a condition.

Here are some loop structures visual basic provides you with:

loop name how to set it up
Do Do
Stuff you want to do
Loop
Do While Do While Condtion
Stuff you want to do
Loop
Loop Until Do
stuff you want to do
Loop Until
Condition
For For Condtion = Value to Condtion = Value
stuff you want to do
Next
Condtion

thedo.gif (806 bytes)

Exclamtiub.gif (188 bytes) The do loop is the most basic loop visual basic
provides us with, it keeps going until you stop it.

Do
Call Msgbox("Hello",vbOkOnly,"nauts-crib.com")
Loop

 

1. We Told Visual Basic that a DO loop is being started, by entering Do.
2. We tell Visual Basic, what we want to do when it beggins looping, we told it to send a message box with the prompt "Hello".
3. We tell Visual Basic that were done inserting things to do, and we want it to loop back to the do part.

while.gif (1247 bytes)

Exclamtiub.gif (188 bytes) The do while loop is very useful when you want to start a
loop, and continue it if the condition is met.

Do While X < 5
Call Msgbox("Hello",vbOkOnly,"nauts-crib.com")
X = X + 1

Loop

 

1. We tell Visual Basic that we want to loop as long as the variable X is less then 5.
2. We tell Visual Basic what we want to do when the loop is executed, well tell vb to send a message box up.
3. We add one (1) to the variable X, because if we didnt increase X, the condition will always be less then 5, therefore, the loop will loop endlessly.
4. We tell visual basic that we are done inserting what to do, and we want it to loop back to do now.

loopuntil.gif (1218 bytes)

Exclamtiub.gif (188 bytes) The loop until loop is basically the same as the do while
loop, instead it tests the condition at the end of the loop.

 

Do
Call Msgbox("Hello",vbOkOnly,"nauts-crib.com")
X = X + 1

Loop Until X = 5

 

1. We tell Visual Basic that we want to start a Loop.
2. We tell VB that we want to send a message box up, everytime the loop is looped.
3. We increment the variable X by 1, because we want the loop to end sometime this century.
4. We tell the loop to stop when the counter variable X = 5.

fornext.gif (906 bytes)

Exclamtiub.gif (188 bytes) This is one of the more advanced loops, but also one of the most widely used ones...

For X = 1 to X <= 5
Call Msgbox("Hello",vbOkOnly,"nauts-crib.com")
X = X + 1

Next X

 

1. We declare what the counter variable is (x=1), and tell vb to loop while its less then or equal to 5. (x <= 5)
2. We tell vb that we want to send a message box up, everytime the loop is looped.
3. We increment the counter variable X by 1, so the loop will stop once the condition is met.
4. We tell VB to loop. Now, when it loops x is incremented so we want vb to do the next x. It's kind of confusing to explain the logic behind the statement "Next x". But, as X increments or decrements, the Next is telling the loop to goto the Next "x" in the loop, dont worry you don't need to know that!

 

VB_back.gif (1347 bytes)

1999-2000 © Erik Helgesen All Rights Reserved.
Only permitted Images and Material may be downloaded from this web site. 
PhotoShop, Flash, and Microsoft Visual Basic are registered trademarks!