OOP-Module Lesson 6
OOP-Module Lesson 6
Learning Module in
Object-Oriented Programming
Compiled by:
MARICRIS M. USITA, Ed.D
AILA CHESKA V. FAJARDO
NICKO B. CABULING
The compiler does not own any of the contents of this learning module. Due credits and
acknowledgment are given to the authors, internet sources, and researchers listed on the
reference page. Such sources are reserved to further explain concepts and cannot be credited
to the compiler and the school. All diagrams, charts, and images are used for educational
purposes only. The sole objective of this instructional material is to facilitate independent
learning and not for monetary gains because this is NOT FOR SALE.
2020 Edition
LISTS AND LOOPS
6
TOPICS
1. The For Next Loop
2. The do Loop
3. The while Loop
LEARNING OUTCOMES
The most common looping method in VB 2013 is the For....Next loop. The structure of a
For...Next loop is as shown below:
For counter=startNumber to endNumber Step increment
One or more statements
Next
To exit a For..Next Loop, you can place the Exit For statement within the loop; it is normally
used together with the If….Then statement. For its application, you can refer to example 1.
Example 1
Dim counter as Integer Possible output:
For counter= 1 to 10
ListBox1.Items.Add (counter) * The program will enter numbers 1
Next to 10 into the list box.
41
Example 4
Possible output:
Dim n as Integer
For n=1 to 10 The process will stop when n is
If n>6 then greater than 6.
Exit For
End If
Else
ListBox1.Items.Add ( n)
Next
End If
Next
TOPIC 2: DO LOOP
The Do Loop structures are
a)
Do While condition
Block of one or more statements
Loop
b)
Do
Block of one or more statements
Loop While condition
c)
Do Until condition
Block of one or more statements
Loop
d)
Do
Block of one or more statements
Loop Until condition
Sometimes we need an exit to exit a loop prematurely because a certain condition is fulfilled. The
syntax to use is Exit Do. Let's examine the following examples:
Example 5
Possible output:
Do while counter <=1000
TextBox1.Text=counter * The above example will keep on
counter +=1 adding until counter >1000.
Loop
Example 6
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(“———————-”)
42
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
While conditions
Visual Basic 2013 statements
End While
Example 7
Example 8
43