For Next Loops in Visual Basic 6.0

A For Next Loop lets you execute a specific group of program statements a  set of number times in an event procedure. This is useful if you are performing several related calculations, working with elements on screen ot processing several pieces for user inputs.

A For Next Loop is just a shortcut of writing a long list of statements. Because each group of statements in such a list would do essentially the same thing, VB lets you define just one group of statements and request that it would be executed as many times as you want.

In a For Next Loop, start and end determine how long the loop runs

Check the syntax below:


for your_variable = start To end
     code block
     or
     statements to be repeated
Next your_variable

Example:
The following For Next Loop sounds four beeps in rapid succession from the computer's speakers:

For x =  1 To 4
     Beep
Next x

so the output of that would be:

Beep
Beep
Beep
Beep



Based from the book Step by Step Microsoft Visual Basic

Until Keyword in Do [While] Loops

Visual Basic lets you try the Until keyword in Do [While] Loops until a certain condition is true. The Until keyword can be used at the top or the bottom of a Do [While] Loop to test a condition (similar to the While Loop).

Check this example below
Do
     InputName = InputBox("Enter item or type Finish to quit.")
     If InputName <> "Finish" Then Print InputName
Loop Until InputName = "Finish"


Based from the book Step by Step Microsoft Visual Basic

Do Loops on Visual Basic

A Do Loop executes a line, or lines of code until a specific condition is met. It is sometimes referred as Do While Loops to some.

For example, you want to let the user enter names in a database until the user types the word Finish in an input box, or maybe until the user clicks the Done button. In that case, you can use a Do [While] Loop cycle until the next string Finish is entered or until the user clicks the Done button.

A Do [While] Loop has several formats, depending on where and how the loop condition is evaluated. The most common syntax used is:

Do While (condition)
       code block
       or
       block of statements
       to be executed
Loop




More examples will be posted soon.




Based on the book, Step by Step Microsoft Visual Basic 6.0

Extended Search

Custom Search
Hello World