LOOP CONTROL STRUCTURES
Cont.
• Visual Basic loop structures allow you to run one or more lines of
code repetitively.
• You can repeat the statements in a loop structure until a condition is
True, or until a condition is False, a specified number of times, or
once for each element in a collection.
Flow chart for loop Structures
While…End loop
• Runs a series of statements as long as a given condition is True.
• Use a While...End While structure when you want to repeat a set of
statements an indefinite number of times, as long as a condition remains
True.
• Syntax:
While condition
[ statements ]
[ Continue While ]
[ statements ]
[ Exit While ]
[ statements ]
End While
Example: A while loop program to output
numbers 0-10
Dim index As Integer = 0
While index <= 10
Console.Write(index.ToString & " ")
index += 1
End While
Console.WriteLine("")
Console.ReadKey ()
Output
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
While Loop in a Windows Form Application
• Think of a windows form application that has button. When the
button is clicked it executes the while loop to output values from 1-10
as shown in the figure below.
The code for the button
Dim index As Integer = 0 • The first line declares the starting
Dim output As String point.
While index <= 10 • Second line creates a variable
known as output to store the
output = output & index & output.
vbNewLine • Third line starts the while block
index += 1 • Fourth line assigns the output after
End While each loop to the output variable
MessageBox.Show(output) through concatenation.
• Fifth line increments
• Seventh line is the message box for
outputting
Do…Loop
• It executes the statement at least once before checking the condition
Syntax
Method 1 Method 2
Do { While | Until } condition Do
[ statements ] [ statements ]
[ Continue Do ] [ Continue Do ]
[ statements ] [ statements ]
[ Exit Do ] [ Exit Do ]
[ statements ] [ statements ]
Loop Loop { While | Until } condition
Example: using Method 1
Dim a As Integer = 0
Do While a <= 10
Console.WriteLine("value of a: {0}", a)
a=a+1
Loop
Console.WriteLine("")
Console.ReadKey()
' Output: 0 1 2 3 4 5 6 7 8 9 10
Example: Using Method 2
Dim a As Integer = 0
Do
Console.WriteLine("value of a: {0}", a)
a=a+1
Loop While (a < =10)
Console.ReadLine()
' Output: 0 1 2 3 4 5 6 7 8 9 10
Do…While using Windows Form Application
• Use the form design used in while loop
• The code
Dim index As Integer = 0
Dim output As String
Do
output = output & index & vbNewLine
index += 1
Loop While (index <= 10)
MessageBox.Show(output)
For…Next Statement
• Repeats a group of statements a specified number of times.
• Very useful in defining ranges of elements to be considered
• Syntax:
For counter [ As datatype ] = start To end [ Step step ]
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]
Flow Chart
Example
• In the following example, the index variable starts with a value of 1
and is incremented with each iteration of the loop, ending after the
value of index reaches 5.
For index As Integer = 1 To 5
Console.WriteLine(index.ToString & " ")
Next
Console.WriteLine("")
Console.ReadKey()
• Output: 1 2 3 4 5
For…Next Windows form Application
• The design and output of the program
The Code
Dim output As String
For index As Integer = 1 To 5
output = output & index & vbNewLine
Next
MessageBox.Show(output)
For…Each control structure
• Repeats a group of statements for each element in a collection.
• Mainly used when dealing with collection of elements such as in an
array.
• Arrays will be discussed in subsequent topics
For Each element [ As datatype ] In group
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]
Example: Printing values of an array
Dim anArray() As Integer = {1, 3, 5, 7, 9}
Dim arrayItem As Integer
For Each arrayItem In anArray
Console.WriteLine(arrayItem)
Next
Console.ReadLine()
• it will output 1,3, 5, 7, 9 which are the values in the array called
“anArray”
More Advanced Loop Operation using Windows
Form Applications
• Think of a situation in which rather than declaring the starting points
and end points of the loops in the code you allow the user to input
them from a G.U.I.
• The benefit of such a program is that it would be more dynamic since
you can adjust the ranges while the program is still executing.
• How would you go about creating the program?
• All you need is to add two textboxes: one to handle the starting point and the
other one to handle the ending point.
• Then create local variables for the starting points and end points.
• Use those local variables as your ranges in the loop
The Design and the Code of the program
The design • The Code
Dim startpoint As Integer = TextBox1.Text
Dim endpoint As Integer =
TextBox2.Text
Dim output As String
While startpoint <= endpoint
output = output & startpoint &
vbNewLine
startpoint += 1
End While
MessageBox.Show(output)
The Execution and Output
Research Task
• Sometimes an application user may accidentally click the Exit button
of an application and it is therefore wise to provide the application
with a facility like the dialog box shown below. Study it and develop
an event procedure of the exit button that triggers the dialog box.
Conclusion
Practice more on each of these control structures.