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.
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:
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.
Example:
The above example will keep on adding until counter >1000.
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.
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.
Example:
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 Do...Loop While, the Do...Loop Until statement tests condition at the bottom of the loop. Therefore, the body of the loop executes at least once, no matter what comparison test turns out to be. The loop continues as long as the comparison test result stays false
If a loop begins with a single Do statement, the loop ends with either Loop While or Loop Until. Here is the format of Do...Loop While:
Example:
Here is the format of Do...Loop Until
Example:
The For loop (sometimes referred to as the For...Next loop) also creates a loop. Unlike the Do loops, however, the For loop repeats for a specified number of times. The format of the For loop looks a little more daunting than that of the Do loops, but after you master the format, you'll have little trouble implementing For loops when your code needs to repeat a section of code for a specified number of times. "For....Next" Loop the format is:
Example:
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
Loop
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.
Example:
Code: vb
Do while counter <=1000
num.Text=counter
counter =counter+1
Loop
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
Loop
Example:
Code: vb
Do until counter>1000
num.Text=counter
counter=counter+1
Loop
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 Do...Loop While, the Do...Loop Until statement tests condition at the bottom of the loop. Therefore, the body of the loop executes at least once, no matter what comparison test turns out to be. The loop continues as long as the comparison test result stays false
If a loop begins with a single Do statement, the loop ends with either Loop While or Loop Until. Here is the format of Do...Loop While:
Code: vb
Do
'Block of one or more VB statements
Loop While condition
Code: vb
Do
num.Text=counter
counter =counter+1
Loop while counter <=1000
Code: vb
Do
'Block of one or more Visual Basic statements
Loop Until condition
Code: vb
Do
num.Text=counter
counter =counter+1
Loop Until counter <=1000
The For Loop
The For loop (sometimes referred to as the For...Next loop) also creates a loop. Unlike the Do loops, however, the For loop repeats for a specified number of times. The format of the For loop looks a little more daunting than that of the Do loops, but after you master the format, you'll have little trouble implementing For loops when your code needs to repeat a section of code for a specified number of times. "For....Next" Loop the format is:
Code: vb
For counter=startNumber to endNumber (Step increment)
One or more VB statements
Next
Code: vb
'(a)
For counter=1 to 10
display.Text=counter
Next
'(b)
For counter=1000 to 5 step -5
counter=counter-10
Next
Prince_Royce
like this

