CyberiaPC.com Main Page




Learning VB

Working with Loops

If you want your program to execute something repetitively based on a condition that you specify, then that can be done with loops.  As the name suggests, a loop is nothing more than a block of code that goes on and on as long as the condition specified by you (the programmer) is either true or false.

There are mainly 2 types of looping structure in VB: 1. Do...Loop structures and 2. For...Next structures.  The Do...Loop structure has 4 flavors: Do...Loop While, Do...Loop Until, Do While...Loop and Do Until...Loop.

I'll first discuss the Do...Loop structure since it is usually the easiest for beginners to learn (to make it fair, that was the case with myself).

First, we have the Do...Loop While structure.  This simply checks the condition at the end of the block.  This makes it ideal for Login Screens, because when the screen pops up for the first time there wouldn't be anything in the password field, so if the condition were to be evaluated at the start then that would make the Login Screen confusing.  Here is the syntax of a typical Do...Loop While:

     Do
          (statements to be executed)
     Loop While condition

And here is a code segment that implements the Login Screen loop I talked about earlier:

     Do
          strLoginPassword = InputBox("Enter Your Password")
     Loop While strLoginPassword <> "Hello"

Don't be afraid by the InputBox command, it simply displays a message box asking for input from the user.  That input is then stored in the variable strLoginPassword.

*Second, we have the Do...Loop Until structure.  This time, the loop repeats until the expression is true.  The previous structure repeated until the expression became false.

     Do
          (statements to be executed)
     Loop Until (condition)

This time, the example code will loop until the No button is clicked in a message box:

 Dim intLoopCount As Integer

     Do
          intLoopCount = intLoopCount + 1
     Loop Until MsgBox("Loop?", vbYesNo) = vbNo

intLoopCount is simply a counter that is increment every time by 1.  The loop keeps on running until No is clicked on the message box.  vbNo is a VB command that means the No button of a message box.

Third, we have the Do While...Loop structure.  The expression is checked to be true before executing the code.  The code is then repeated until the expression becomes false.

     Do While (condition)
          (statements to be executed)
     Loop

This example asks the user for a password until the correct password is entered:

     Dim UserInput As String
     Dim Password As String

     Password = "hello"

     Do While UserInput <> Password
          UserInput = InputBox("Please Enter Login Password")
     Loop

As long as the incorrect password keeps being entered, the loop will display the input box.

* Fourth, is the Do Until...Loop structure.  The expression is checked to be false before executing the code.  The code is then repeated until the expression becomes true.

     Do Until (condition)
          (statements to be executed)
     Loop

This example asks the user for a secret word until the correct word is entered:

     Dim UserInput As String
     UserInput = "not ok"
     
     Do Until UserInput = "ok"
          UserInput = InputBox("Please Enter Secret Word")
     Loop

Finally, the For...Next loop comes into the picture.  Just like the previous lesson with conditions, this kind of loop makes certain looping procedures much easier and much more efficient.  This type of loop is used when both the number of times the loop will execute and the condition are known.  Generally, when dealing with numbers and counters, it is best to use For loops.  This is the general syntax of a For...Next loop:

     For counter = starting number To ending number
          statements
     Next counter

What happens is that the counter is incremented on the Next line after each loop until it is equal to ending_number.  This example below will make things clearer:

     Dim Counter As Integer
     Dim Max As Integer

     Max = 20

     For Counter = 1 To Max
          MsgBox Counter
     Next Counter

As you can see Counter is set to 1 and is incremented after each successive loop until it is equal to Max, which happens to be 20.  Each time, a message box pops up with the the current value of counter.  This means that you would get 20 message boxes displaying numbers from 1 to 20.  Remember to include the loop block in some sort of procedure, whether the Click event of a button or the Load event of a form!

Final tip: To exit a Do...Loop, simply enter the command: Exit Do.  And to Exit For...Next loops enter Exit For

{Go Back To VB Lessons Page}