0% found this document useful (0 votes)
10 views

OOP-Module Lesson 6

This document contains a learning module on object-oriented programming in Visual Basic 2013. It discusses loops, including the for next loop, do loop, and while loop. The for next loop repeats a block of code a specified number of times based on a counter. The do loop and while loop repeat a block of code as long as or until a condition is true. Examples are provided to illustrate how to use each type of loop to iterate through values, calculate sums, and exit early.

Uploaded by

lexterjairus
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

OOP-Module Lesson 6

This document contains a learning module on object-oriented programming in Visual Basic 2013. It discusses loops, including the for next loop, do loop, and while loop. The for next loop repeats a block of code a specified number of times based on a counter. The do loop and while loop repeat a block of code as long as or until a condition is true. Examples are provided to illustrate how to use each type of loop to iterate through values, calculate sums, and exit early.

Uploaded by

lexterjairus
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Republic of the Philippines

OCCIDENTAL MINDORO STATE COLLEGE


Labangan, San Jose, Occidental Mindoro
website: www.omsc.edu.ph email address: [email protected]
Tele/Fax: (043) 457-0231 CERTIFIED TO ISO 9001:2015
CERT. NO.: 50500643 QM15

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

At the end of the lesson, students should be able to:


1. compare and contrast the different type of loops;
2. design a program using the for next, do, and while loop; and
3. create a functional program using for next, do, and while loop.

TOPIC 1: FOR NEXT LOOP


A procedure in a computer program that repeatedly runs until meeting a certain condition is
called looping. A loop can go on repetitively as long as the processor and memory could support
it. For example, a program that adds a series of numbers until the sum exceeds a certain value or
a program that prompts the user to enter data repeatedly until he or she enters the word ‘Finish’.
The three methods of Looping, the For…..Next loop, the Do loop, and the While…..End
While loop. All methods produce the same repetitive effects.

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.

Example 2 Possible output:


Dim counter, sum As Integer
For counter=1 to 100 step 10 The programme will calculate the
sum+=counter sum of the numbers as follows:
ListBox1.Items.Add (sum) sum=0+10+20+30+40+…….
Next

Example 3 Possible output:


Dim counter, sum As Integer
sum = 1000 *Notice that increment can be
negative. The program will
For counter = 100 To 5 Step -5
compute the subtraction as follow:
sum – = counter
1000-100-95-90-……….
ListBox1.Items.Add(sum)
Next

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

The above example can be rewritten as


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

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

TOPIC 3: WHILE LOOP


The structure of a While….End While Loop is very similar to the Do Loop. It takes the following
form:

While conditions
Visual Basic 2013 statements
End While

Example 7

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

Example 8

Private Sub Button1_Click(sender As Object, e As


EventArgs) Handles Button1.Click
Dim Counter As Integer Possible output:
Counter = 1
While (Counter < 10) * The above example will keep on
MsgBox("The Counter is now " & Counter) adding until counter reaches 10.
Counter = Counter + 1
End While
End Sub

43

You might also like