Lesson 9: Looping


Custom Search

 

Another procedure that involves decision making is looping. Visual Basic allows a procedure to be repeated many times until a condition or a set of conditions is fulfilled. This is generally called looping . Looping is a very useful feature of Visual Basic because it makes repetitive works easier. There are  two kinds of loops in Visual Basic,  the Do...Loop  and the For.......Next loop

9.1  Do Loop

 

The Do Loop statements have three different forms, as shown below:

 

a)   Do While condition

            Block of one or more VB statements

      Loop

 

b)   Do
            Block of one or more VB statements
      Loop While condition

 

c)    Do Until condition
              Block of one or more VB statements
       Loop

 

d)    Do
             Block of one or more VB statements

       Loop Until condition

 

 
   

 

 

9.2 Exiting the Loop

Sometime we need exit to exit a loop prematurely because of a certain condition is fulfilled. The syntax to use is known as Exit Do. You can examine Example 9.2 for its usage.

Example 9.1

 

       Do while counter <=1000

             num.Text=counter

             counter =counter+1

       Loop

* The above example will keep on adding until counter >1000.

The above example can be rewritten as

        Do

               num.Text=counter
               counter=counter+1

       Loop until counter>1000

 

 

Example 9.2

 

Dim sum, n As Integer

 Private Sub Form_Activate()

List1.AddItem "n" & vbTab & "sum"

Do

   n = n + 1

   Sum = Sum + n

 List1.AddItem n & vbTab & Sum

 If n = 100 Then

 Exit Do

 End If

  Loop

End Sub

 

Explanation

 

In the above  example, we compute the summation of 1+2+3+4+……+100.  In the design stage, you need to insert a ListBox into the form for displaying the output, named List1. The program uses the AddItem method to populate the ListBox. The statement List1.AddItem "n" & vbTab & "sum" will display the headings in the ListBox, where it uses the vbTab function to create a space between the headings n and sum.

9.3  For....Next Loop

The format is:
 

For counter=startNumber to endNumber (Step increment)

    One or more VB statements

Next

 

Please refer to example 9.3a,9.3b and 9.3 c for its usage.

Sometimes the user might want to get out from the loop before the whole repetitive process is executed, the command to use is Exit For. To exit a For….Next Loop, you can place the Exit For statement within the loop; and it is normally used together with the If…..Then… statement. Let’s examine example 9.3 d.

Example 9.3 a

 

For  counter=1 to 10  

display.Text=counter

  Next

 

Example 9.3 b

 

For counter=1 to 1000 step 10  

counter=counter+1

 Next

 

Example 9.3 c

  For counter=1000 to 5 step -5

  counter=counter-10

   Next

*Notice that increment can be negative

 

Example 9.3 d

 

Private Sub Form_Activate( )

For n=1 to 10

If n>6 then

Exit For

End If

 

Else

Print n

End If

End Sub

9.4 The While….Wend Loop

 

The structure of a While….Wend Loop is very similar to the Do Loop. it takes the following format:

 

While condition

        Statements

Wend

 The above loop means that while the condition is not met, the loop will go on. The loop will end when the condition is met. Let’s examine the program listed in example 9.6 where it produces the same result as example 9.4 and example 9.5 

Example 9.6

Dim sum, n  As Integer

Private Sub Form_Activate()

List1.AddItem "n" & vbTab & "sum"

While n <> 100

n = n + 1

Sum = Sum + n

 List1.AddItem n & vbTab & Sum

Wend

End Sub

 

 

 

 
  Bookmark and Share

<Previous Lesson> <<Home>> < Next Lesson>

Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: admin@vbtutor.net

[Privacy Policy]