Visual Basic 2017 Lesson 15: Looping

[Lesson 14] << [Contents] >> [Lesson 16]

In Visual Basic 2017, looping involves a  procedure that runs repetitively until a certain condition is met. For example, we can design a program that adds a series of numbers until the sum exceeds a certain value or a program that asks the user to enter data repeatedly until he or she enters the word ‘Finish’.  There are three types of Loops in Visual Basic 2017,  namely the For…..Next loop, the Do loop, and the While…..End While loop



15.1 For….Next Loop

In Visual Basic 2017, the structure of a For…Next loop is as shown below:

For counter=startNumber to endNumber (Step increment)

One or more Visual Basic 2017 statements

Next
In order to exit a For…..Next Loop, you can place the Exit For statement within the loop. It is usually used together with the If….Then statement. For its application, you can refer to example 15.1 d.

Example 15.1 a

Dim counter as Integer
For counter=1 to 10
ListBox1.Items.Add (counter)
Next

* The program will enter number 1 to 10 into the list box.

Example 15.1b

The following program will calculate the sum of the numbers 0+10+20+30+40+……+100

Dim counter , sum As Integer
For counter=1 to 100 step 10
sum+=counter
ListBox1.Items.Add (sum)
Next



Example 15.1c

This program will compute a series of subtractions as follow:
1000-100-95-90-………-5. In this case, the increment is negative.

Dim counter, sum As Integer
sum = 1000
For counter = 100 To 5 Step -5
sum – = counter
ListBox1.Items.Add(sum)
Next

Example 15.1d

This program uses Exit …For to escape the loop when n is greater than 6.

Dim n as Integer
For n=1 to 10
If n>6 then
Exit For
End If
Else
ListBox1.Items.Add ( n)
Next
End If
Next

15.2 Do Loop

In Visual Basic 2017, there are several  Do Loop structures, as shown below:
a) Do While condition
Block of one or more Visual Basic 2017 statements
Loop

b) Do
Block of one or more Visual Basic 2017 statements
Loop While condition

c) Do Until condition
Block of one or more Visual Basic 2017 statements
Loop

d) Do
Block of one or more Visual Basic 2017 statements
Loop Until condition

* Exiting the Loop

We can also use Exit Do to escape the loop.




Let’ s examine the following examples:

Example 15.2(a)

In this example, the procedure will keep on adding the initial number by 1 until it exceeds 1000.

Do while counter <=1000
TextBox1.Text=counter
counter +=1
Loop

We can rewrite the procedure above and achieve the same result. The code is shown as follows:

Do
TextBox1.Text=counter
counter+=1
Loop until counter>1000

Example 15.2(b)

In this example, the procedure will keep on adding a number by 1 and display the results in a list box. The process stops when it has repeated 100 times.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sum, n As Integer
ListBox1.Items.Add(“n” & vbTab & “Sum”)
ListBox1.Items.Add(“———————-”)
Do
n += 1
sum += n
ListBox1.Items.Add(n & vbTab & sum)
If n = 100 Then
Exit Do
End If
Loop
End Sub

* The loop in the above example can be replaced by the following loop:

Do Until n = 10
n += 1
sum += n
ListBox1.Items.Add(n & vbTab & sum)
Loop

The output is as shown in Figure 15.1

vb2013_figure15.1

                                              Figure 15.1

15.3 While….End While Loop

In Visual Basic 2017, the structure of a While….End While Loop is very similar to the Do Loop. it takes the following form:

While conditions
Visual Basic 2017 statements
End While

Example 15.3

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sum, n As Integer
ListBox1.Items.Add("n" & vbTab & "sum")
ListBox1.Items.Add("———————-")

While n <> 10
n += 1
sum += n
ListBox1.Items.Add(n & vbTab & sum)
End While
End Sub








[Lesson 14] << [Contents] >> [Lesson 16]

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone