VISUAL BASIC.
NET
CONTROL STRUCTURES
PART 2
Using Loops
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1
AFTER COMPLETING THIS
CHAPTER, YOU WILL BE ABLE TO:
Write a While…End While iteration
Write a Do While…Loop Iteration
Write a Do...Until Loop
Use a For … Next iterative construct
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 2
KEY WORDS USED IN THIS
LECTURE
1. While…End While
2. Do While…Loop
3. Do…Until…Loop
4. For…Next
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 3
DEFINITION
The iteration construct is defined as the
repetition of a task or a set of tasks to be
executed.
It is also commonly referred to as a loop in
programming.
In a loop, the programmer will decide what is
the starting value and the stopping condition.
There are 3 types of looping constructs:
For…Next
Deterministic loop
While…End While
Do…Loop Undeterministic loop
DETERMINISTIC VS. UNDETERMINISTIC
Deterministic
The starting value and the stopping value is known.
Programmer knows exactly how many times the loop
needs to be executed
Undeterministic
The starting value is known but stopping value is
unknown
However, the condition for the loop to stop execution is
known
LOOP STRUCTURE 1
WHILE…END WHILE
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 6
WHILE…END WHILE STATEMENT
Visual Basic 2005 uses While…End While
statements to repeat a set of statements as
long as the condition is true.
If the condition is true, the statements in the
body of the loop get executed.
Else it skips the body of the loop.
The End While statement marks the end of
the loop.
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 7
WHILE…END WHILE
While condition is True
execute statement(s)
End While
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 8
FLOWCHART – WHILE…END WHILE
Ctr = 1
Ctr <=10 ctr = ctr + 1
True
False
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 9
While…End While
‘Display numbers from 1 to 10
n=1
While n <=10
List1.Items.Add(n)
n=n+1
End While
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 10
LOOP STRUCTURE 2
DO WHILE LOOPS
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 11
DO WHILE LOOPS
Visual Basic 2005 uses a Do…while…Loop that
executes a group of statements until a
certain condition become False.
A Do…while…Loop has several formats,
depending on where and how the loop
condition is evaluated.
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 12
DO WHILE…LOOP
Do While condition is True
execute statement(s)
Loop
DO…LOOP WHILE
Do
execute statement(s)
Loop While condition is True
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 13
EX: DO WHILE…LOOP
n1 = 1
Do While n1 <= 10
ListBox1.Items.Add(n1)
n1 = n1 + 1
Loop
EX: DO…LOOP WHILE
n1 = 1
Do
ListBox1.Items.Add(n1)
n1 = n1 + 1
Loop While n1 <= 10
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 14
DO WHILE…LOOP
Ctr = 1 If the condition at
the top of the loop
isn’t True when the
Do statement first
True evaluated, the Do
Ctr <=10 ctr = ctr + 1 loop is never
executed.
False
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 15
DO…LOOP WHILE
• If you want the loop to
Ctr = 1 run at least once in a
program, put the
conditional test at the
ctr = ctr + 1 bottom of the loop.
• Note that the test of the
True entered test is case
Ctr <=10 sensitive, which means
“Done” and “DONE”
False are not same in the
condition
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 16
AVOIDING AN ENDLESS LOOP
When you use looping constructs in your
programs, it’s very important to design your
test conditions so that each loop has a true
exit point.
If a loop test never evaluates to False, the
loop executes endlessly.
So you should provide some method to take
out the control from the loop. Following
example shows you how to use a character to
break the loop.
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 17
HOW TO BREAK A LOOP BY
ENTERING A VALUE
Dim Num, n1 as Double
Do
Num=InputBox(“Enter a Number. Type -1 to quit.”)
n1 =Num * Num
Textbox1.Text=n1
Loop While Num>= 0
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 18
LOOP STRUCTURE 3
DO UNTIL LOOPS
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 19
DO UNTIL LOOPS
Visual Basic 2005 uses a Do…until…Loop that
executes a group of statements until a certain
condition becomes true.
If the Condition of the Loop is evaluated to
true, then the Loop would not enter to the
loop construct, which is opposite to the Do…
While loop.
A Do loop has several formats, depending on
where and how the loop condition is evaluated.
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 20
DO UNTIL…LOOP
Do Until condition becomes True
execute statement(s)
Loop
DO…LOOP UNTIL
Do
execute statement(s)
Loop Until condition becomes True
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 21
EX: DO UNTIL …LOOP
n1 = 1
Do Until n1>10
ListBox1.Items.Add(n1)
n1 = n1 + 1
Loop
EX:
n1 = 1
Do
ListBox1.Items.Add(n1)
n1 = n1 + 1
Loop Until n1>10
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 22
DO UNTIL…LOOP
Ctr = 1 If the condition at
the top of the loop
isn’t evaluated to
false, when the Do
False statement first
Ctr > 10 ctr = ctr + 1 evaluated, the Do
loop is never
executed.
True
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 23
DO…LOOP UNTIL
Ctr = 1
• If you want the loop to run
at least once in a program,
put the conditional test at the
ctr = ctr + 1
bottom of the loop.
False
Ctr > 10
True
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 24
COMPARING DO WHILE AND
DO UNTIL
• While … End While and Do .. While
loop will execute statements in the loop
when the condition is True
• Do until loop will execute statements in
the loop when the condition is False
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 25
LOOP STRUCTURE 4
FOR … NEXT
ITERATIVE CONSTRUCT
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 26
FOR…NEXT LOOPS
The For…Next loop is ideal for situations that
require a counter because it initializes, test and
auto-increments a counter variable.
For counter = startvalue To endvalue [step increment]
Statements(s)
Next
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 27
FOR…NEXT LOOP - SYNTAX
For x = initialvalue to endvalue Step increment
execute statement (s)
Next x
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 28
EXAMPLE
TO USE A FOR…NEXT LOOP TO PRINT ALL ODD
NUMBERS FROM 1 TO 10
Dim n1 as integer
For n1 = 1 To 10 Step 2
ListBox1.Items.Add(n1)
Next n1
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 29
FOR…NEXT LOOPS
The variable after “Next” is optional.
You replace initialvalue and endvalue with
numeric values representing starting and
stopping points for the loop.
Step increment is also optional. By default
Next will increment the loop count by 1.
But if you need to change the Step increment
to another value, then you should make use of
keyword Step followed by the loop counter.
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 30
EXAMPLE - TO USE A FOR…NEXT LOOP
TO PRINT ALL NUMBERS FROM 1 TO 10
Dim n1 as integer
For n1 = 1 To 10
ListBox1.Items.Add(n1)
Next n1
Dim n1 as integer
For n1 = 1 To 10 Step 1
ListBox1.Items.Add(n1)
Next n1
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 31
WRITE A PROGRAM TO DISPLAY
1 TO 10 IN A LISTBOX
1. Make use of a While…End While iteration.
2. Code for the form Load Event
3. Use While…End While Loop
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 32
Public Class Q1From1To10
Dim n1 As Integer
Private Sub Q1From1To10_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
n1 = 1
While n1 <= 10
List1.Items.Add(n1)
n1 = n1 + 1
End While
End Sub
End Class
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 33
WRITE A PROGRAM TO DISPLAY THE SUM
OF DIGITS FROM 1 TO 10
I.E 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
Show your answer using the Do
While…Loop
Use a MessageBox to display the Sum
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 34
Public Class Q4Sum1to10
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim i, sum As Integer
i=1
Do While i <= 10
sum = sum + i
i=i+1
Loop
MessageBox.Show("Sum is " & sum)
End Sub
End Class
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 35
EXERCISE
Write a program which lets you enter the
price of several articles.
The user will enter a price of zero to indicate
that there are no more articles to be added.
Once all the articles have been entered, the
program must display the total price of all
the articles.
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 36
EXERCISE
*
* *
* * *
* * * *
* * * * *
A.P.I.I.T - Revised by Nadeera Ahangama 3/11/2009 1- 37