How to Write a Loop in Visual Basic

Written by norm dickinson Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Write a Loop in Visual Basic
Loops Repeat code until specific conditions are met. (panneau image by Palindra from Fotolia.com)

Three basic components of any computer algorithm are the sequential statements, conditional operations and iterative operations, or loops. Loops exist in two basic forms in Microsoft Visual Basic 2008, the "Do" loop and the "For Next" loop. Loops are meant to be used with rules that instruct the loop to repeat a certain number of times or until certain conditions are met. Loops that test for these conditions prior to running for the first time are called "Pre-test" loops, and those which test for these conditions after running once are called "Post-test" loops. Post-test loops always run at least once.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Declare variables that are to be used in the loop by using the "DIM" statement, as in:

    DIM counter as integer

    where "counter" is the variable and "integer" is the type of variable.

  2. 2

    Code the "Do" loop using a "While" condition at the beginning of the loop, for example:

    DIM counter as Integer

    Do While counter < 20

    MessageBox.Show(counter.ToString)

    counter = counter + 1

    Loop

  3. 3

    Code the "Do" loop using the "Until" condition at the end of the loop, such as:

    Dim counter as Integer

    Do

    MessageBox.Show(counter.ToString)

    counter = counter + 1

    Loop Until counter = 20

  1. 1

    Declare variables that are to be used in the loop by using the "DIM" statement, as in:

    DIM counter as integer

    where "counter" is the variable and "integer" is the type of variable, or declare variables dynamically as part of the "For-Next" loop statement.

  2. 2

    Code the "For-Next" loop by creating a conditional statement such as:

    DIM counter as integer

    For counter = 1 to 10

    MessageBox.Show(counter.ToString)

    Next counter

  3. 3

    Code the "For-Next" loop to follow a pattern using only certain numbers, as in the following example, which only displays odd numbers:

    DIM counter as integer

    For counter = 1 to 10 step 2

    MessageBox.Show(counter.ToString)

    Next counter

  4. 4

    Declare the variable dynamically within the loop as in the following example:

    For counter as integer = 1 to 10

    MessageBox.Show(counter.ToString)

    Next Counter

Tips and warnings

  • Variables in the "For-Next" loop should be declared as part of the loop syntax when they are not used anywhere else other than the loop to avoid errors that can occur when a variable has a greater scope, or range of use, than required. Loops can be nested within one another for several levels, allowing a wide range of customisation for conditional processing.
  • Loops are inherently prone to repeating indefinitely and care should be taken to eliminate a loop from running without end, as this will cause the program to freeze.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.