Loops
Loops
Looping involves a procedure that runs iteratively 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.
You can also create a VB.net program that asks the user to enter data repeatedly until he or she
enters the word 'Finish'.
Key terms
Term Definition
Do Required. Starts the definition of the Do loop.
While Cannot be given if Until is used. Repeat the loop until condition is False.
Until Cannot be given if While is used. Repeat the loop until condition is True.
condition Optional. Boolean expression. If condition is Nothing, Visual Basic treats it as False.
statements Optional. One or more statements that are repeated while, or until, condition is True.
DO WHILE LOOP
Loop
Example
Dim count As Integer = 0
Do While (count < 5)
Lstresults.items.add("Count: " & count)
count += 1 ‘count-count+1
Loop
Example1
Private Sub btnlooping_Click(sender As Object, e As EventArgs) Handles
btnlooping.Click
Dim count As Integer = 0
Do While count < 5
lstanswer.Items.Add("Count: " & count)
count += 1
Loop
End Sub
DO LOOP WHILE
Do Loop While -Loops as long as the condition is true (checked after the loop starts).
Do
Inserted code runs as long as the expression is TRUE
Loop While (Expression)
Example
Dim count As Integer = 0
Do
Lstresults.items.add("Count: " & count)
count += 1 ‘count-count+1
Loop While (count < 5)
Example 2
Private Sub btnlooping_Click(sender As Object, e As EventArgs) Handles
btnlooping.Click
Dim count As Integer = 0
Do
lstanswer.Items.Add("Count: " & count)
count += 1
Loop While count < 5
End Sub
Note
The Do...Loop structure gives you more flexibility than the While...End While
Statement because it enables you to decide whether to end the loop when condition stops
being True or when it first becomes True. It also enables you to test condition at either the start
or the end of the loop.
Do Until… Loop
A "Do Until" loop statement runs until a logical statement is true.
This means that as long as your expression is false, the program will run until it is true.
Once the expression is true, the program stops running.
Syntax
Do until (Expression)
Loop
Example
Dim count As Integer = 0
Do until (count > 5)
Lstresults.items.add("Count: " & count)
count += 1 ‘count-count+1
Loop
Example1
Private Sub btnlooping_Click(sender As Object, e As EventArgs) Handles
btnlooping.Click
Dim count As Integer = 0
Do until count > 5
lstanswer.Items.Add("Count: " & count)
count += 1
Loop
End Sub
For..next loop
The for next statements begin with a “For” keyword and end with a “Next”
• 2 types of “For” Statements – “For… Next” and “For Each… Next”
• Primary use: – To automate a program to read a block of statements until the statement runs a
given number of times as determined by the start, end, and step values.
There may be a situation when you need to execute a block of code several number of times. In
general, statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and
following is the general form of a loop statement in most of the programming languages
You determine the range of values you wish to loop ("StartValue" to "EndValue") and allow the script to
run until values go from start to finish.
• Once the script runs its first variable, it is looped by the "Next" statement.
• This statement tells the program to proceed to the next variable until it reaches its EndValue.
SYNTAX
For variable = StartValue To EndValue
‘ Block of code inserted here
Next
Example
For x = 1 to 10
‘ Code to print file
MsgBox "Printing File " & x
Next
Private Sub btnlooping_Click(sender As Object, e As EventArgs) Handles
btnlooping.Click
For x = 1 To 10
lstanswer.Items.Add("x = " & x)
Next
end Sub
For Each… Next
“For Each… Next” Statements repeats a group of statements for each element in an array or collection
• With this command you tell the program to repeat a function command for each given element in a
group listing.
syntax
For Each element In group
‘ Insert statement here Next
‘ Display the list of field names in a message box
next
example
For Each fieldName In theList
MsgBox “The field name is “ & fieldName
Next fieldName
OTHER LOOPS
While... End While- It executes a series of statements as long as a given condition is
True.
EXAMPLE
Private Sub btnlooping_Click(sender As Object, e As EventArgs) Handles
btnlooping.Click
Dim a As Integer = 1
While a <= 10
lstanswer.Items.Add(a)
a = a + 1
End While
End Sub
With... End With-It is not exactly a looping construct. It executes a series of statements
that repeatedly refer to a single object or structure.
Dim person As New Person()
With person
.FirstName = "John"
.LastName = "Doe"
.Age = 30
End With
Nested loops-You can use one or more loops inside any another While, For or Do loop.
Loop Control Statements
Loop control statements
Loop control statements change execution from its normal sequence. When execution leaves a
scope, all automatic objects that were created in that scope are destroyed.
VB.Net provides the following control statements. Click the following links to check their
details.
Causes the loop to skip the remainder of its body and immediately retest
Continue statement
its condition prior to reiterating.