Topics: For loop, Control flow, While loop Pages: 31 (7924 words) Published: February 3, 2013
Loop Structures (Visual Basic)

isual Basic loop structures allow you to run one or more lines of code repetitively. You can repeat the statements in a loop structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection. The following illustration shows a loop structure that runs a set of statements until a condition becomes true. Running a set of statements until a condition becomes true


Visual Basic allows a procedure to be repeated as many times as long as the processor could support. This is generally called looping . Visual Basic supports several versions of the Do statement. 

Do While loop

The Do While loop is perhaps the most common looping statement that you'll put in Visual Basic programs. 

Here is the format of the Do While loop:
Code: vb
Do While condition
    'Block of one or more VB statements
The block of code continues looping as long as condition is true. Whether you insert one or several lines of code for the block doesn't matter. It's vital, however, for the block of code to somehow change a variable used in condition .

The block of code keeps repeating as long as the Do While loop's condition continues to stay true. Eventually, condition must become false or your program will enter an infinite loop and the user will have to break the program's execution by pressing the Ctrl+Break key combination. An infinite loop is a loop that never terminates.

The Do While loop continues executing a block of Visual Basic statements as long as condition is true. As soon as condition becomes false, the loop terminates.

As long as condition is true, the block of code in the body of the loop continues executing. When condition becomes false, the loop terminates. After the loop terminates, Visual Basic begins program execution at the statement following the Loop statement because Loop signals the end of the loop. As soon as Do While's condition becomes false, the loop terminates and doesn't execute even one more time. The Do While's condition appears at the top of the loop. Therefore, if condition is false the first time the loop begins, the body of the loop will never execute.

Code: vb
Do while counter <=1000
    counter =counter+1
The above example will keep on adding until counter >1000. 

The Do Until Loop

Whereas the Do While loop continues executing the body of the loop as long as the condition is true, the Do Until loop executes the body of the loop as long as the condition is false. The program's logic at the time of the loop determines which kind of loop works best in a given situation.

Do Until works almost exactly like the Do While loop except that the Do Until loop continues executing the body of the loop until the condition is true. Like the Do While, the Do Until is a multiline looping statement that can execute a block of code that's one or more lines long.

Code: VB
Do Until (condition)
    Block of one or more Visual Basic statements
Remember that the condition must be false for the loop to continue. You can use the Do While or the Do Until for almost any loop. 

Code: vb
Do until counter>1000
Other loops

Another pair of Do loops work almost exactly like the two previous loops. Do...Loop While and Do...Loop Until look very much like their counterparts that you learned about earlier. But these new loop formats check their comparison tests at the bottom of the loop rather than at the top. To complete the loop statements, Visual Basic also supports a Do...Loop Until statement. Like the...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Counter Controlled Loops and While Loops Essay
  • Essay about Php Statement & Loop Samples
  • Phase-locked Loop and Frequency Essay
  • Fruit Loops Essay
  • Essay on The Ooda Loop & Hema
  • Pll( Phase-Locked Loop) Essay
  • Phase Locked Loop Essay
  • Casual Loop Essay

Become a StudyMode Member

Sign Up - It's Free