Looping in Visual Basic

Discussion in 'Visual Basic [VB]' started by Sanskruti, Mar 31, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    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:
    Do While condition 
    	'Block of one or more VB statements 
    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:
    Code:
    Do while counter <=1000 
    	num.Text=counter 
    	counter =counter+1 
    Loop 
    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:
    Do Until (condition)
    	Block of one or more Visual Basic statements
    Loop
    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:
    Code:
    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:
    Do 
    	'Block of one or more VB statements 
    Loop While condition 
    Example:
    Code:
    Do
    	num.Text=counter 
    	counter =counter+1 
    Loop while counter <=1000 
    Here is the format of Do...Loop Until

    Code:
    Do 
    	'Block of one or more Visual Basic statements
    Loop Until condition 
    Example:

    Code:
    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:
    For counter=startNumber to endNumber (Step increment) 
        One or more VB statements 
    Next 
    Example:

    Code:
    '(a)
    For  counter=1 to 10 
    	display.Text=counter 
    Next 
    '(b)
    For counter=1000 to 5 step -5 
    	counter=counter-10 
    Next 
     
    Prince_Royce likes this.
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    How to break from a loop?? Or continue??
     
  3. Steel9561

    Steel9561 New Member

    Joined:
    Apr 26, 2008
    Messages:
    14
    Likes Received:
    1
    Trophy Points:
    0
    That's very easy, for For loops, use: Exit For and for Do Loops use: Exit Do.

    Luis Lazo
     
    Last edited by a moderator: May 1, 2008

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice