0% found this document useful (0 votes)
3 views21 pages

Looping

about vb.net looping

Uploaded by

gomezmark186
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views21 pages

Looping

about vb.net looping

Uploaded by

gomezmark186
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VB.

Net - Loops

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 −

[Link] provides following types of loops to handle looping requirements. Click the following links to
check their details.
Loop Type Description

Do Loop It repeats the enclosed block of statements while a Boolean


condition is True or until the condition becomes True. It could be
terminated at any time with the Exit Do statement.

For...Next It repeats a group of statements a specified number of times and a


loop index counts the number of loop iterations as the loop
executes.

For Each...Next It repeats a group of statements for each element in a collection. This
loop is used for accessing and manipulating all elements in an array or
a [Link] collection.

While... End While It executes a series of statements as long as a given condition is


True.

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.

Nested loops You can use one or more loops inside any another While, For or Do
loop.

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.

[Link] provides the following control statements. Click the following links to check their details.
Control Statement Description

Exit statement Terminates the loop or select case statement and transfers
execution to the statement immediately following the loop or select
case.

Continue statement Causes the loop to skip the remainder of its body and immediately
retest its condition prior to reiterating.

GoTo statement Transfers control to the labeled statement. Though it is not advised
to use GoTo statement in your program.
[Link] - Do Loop

It repeats the enclosed block of statements while a Boolean condition is True or until the condition
becomes True. It could be terminated at any time with the Exit Do statement.

The syntax for this loop construct is −

Do { While | Until } condition


[ statements ]
[ Continue Do ]
[ statements ]
[ Exit Do ]
[ statements ]
Loop
-or-
Do
[ statements ]
[ Continue Do ]
[ statements ]
[ Exit Do ]
[ statements ]
Loop { While | Until } condition

Flow Diagram
Example
 Live Demo
Module loops
Sub Main()
' local variable definition
Dim a As Integer = 10
'do loop execution
Do
[Link]("value of a: {0}", a)
a = a + 1
Loop While (a < 20)
[Link]()
End Sub
End Module

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

The program would behave in same way, if you use an Until statement, instead of While −

 Live Demo
Module loops
Sub Main()
' local variable definition
Dim a As Integer = 10
'do loop execution

Do
[Link]("value of a: {0}", a)
a = a + 1
Loop Until (a = 20)
[Link]()
End Sub
End Module

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
[Link] - For...Next Loop

It repeats a group of statements a specified number of times and a loop index counts the number of
loop iterations as the loop executes.

The syntax for this loop construct is −

For counter [ As datatype ] = start To end [ Step step ]


[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]

Flow Diagram
Example
 Live Demo
Module loops
Sub Main()
Dim a As Byte
' for loop execution
For a = 10 To 20
[Link]("value of a: {0}", a)
Next
[Link]()
End Sub
End Module

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20

If you want to use a step size of 2, for example, you need to display only even numbers, between 10
and 20 −

 Live Demo
Module loops
Sub Main()
Dim a As Byte
' for loop execution
For a = 10 To 20 Step 2
[Link]("value of a: {0}", a)
Next
[Link]()
End Sub
End Module
When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 12
value of a: 14
value of a: 16
value of a: 18
value of a: 20
[Link] - Each...Next Loop

It repeats a group of statements for each element in a collection. This loop is used for accessing and
manipulating all elements in an array or a [Link] collection.

The syntax for this loop construct is −

For Each element [ As datatype ] In group


[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]

Example
 Live Demo
Module loops
Sub Main()
Dim anArray() As Integer = {1, 3, 5, 7, 9}
Dim arrayItem As Integer
'displaying the values

For Each arrayItem In anArray


[Link](arrayItem)
Next
[Link]()
End Sub
End Module

When the above code is compiled and executed, it produces the following result −

1
3
5
7
9
[Link] - While... End While Loop

It executes a series of statements as long as a given condition is True.

The syntax for this loop construct is −

While condition
[ statements ]
[ Continue While ]
[ statements ]
[ Exit While ]
[ statements ]
End While

Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is logical true. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the
loop.

Flow Diagram
Here, key point of the While loop is that the loop might not ever run. When the condition is tested
and the result is false, the loop body will be skipped and the first statement after the while loop will
be executed.

Example
 Live Demo
Module loops
Sub Main()
Dim a As Integer = 10
' while loop execution '

While a < 20
[Link]("value of a: {0}", a)
a = a + 1
End While
[Link]()
End Sub
End Module

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
[Link] - Nested Loops

[Link] allows using one loop inside another loop. Following section shows few examples to
illustrate the concept.

Syntax
The syntax for a nested For loop statement in [Link] is as follows −

For counter1 [ As datatype1 ] = start1 To end1 [ Step step1 ]


For counter2 [ As datatype2 ] = start2 To end2 [ Step step2 ]
...
Next [ counter2 ]
Next [ counter 1]

The syntax for a nested While loop statement in [Link] is as follows −

While condition1
While condition2
...
End While
End While

The syntax for a nested Do...While loop statement in [Link] is as follows −

Do { While | Until } condition1


Do { While | Until } condition2
...
Loop
Loop

A final note on loop nesting is that you can put any type of loop inside of any other type of loop. For
example, a for loop can be inside a while loop or vice versa.

Example
The following program uses a nested for loop to find the prime numbers from 2 to 100 −

 Live Demo
Module loops
Sub Main()
' local variable definition
Dim i, j As Integer
For i = 2 To 100
For j = 2 To i
' if factor found, not prime
If ((i Mod j) = 0) Then
Exit For
End If
Next j
If (j > (i \ j)) Then
[Link]("{0} is prime", i)
End If
Next i
[Link]()
End Sub
End Module

When the above code is compiled and executed, it produces the following result −

2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
[Link] - Exit Statement

The Exit statement transfers the control from a procedure or block immediately to the statement
following the procedure call or the block definition. It terminates the loop, procedure, try block or the
select block from where it is called.

If you are using nested loops (i.e., one loop inside another loop), the Exit statement will stop the
execution of the innermost loop and start executing the next line of code after the block.

Syntax
The syntax for the Exit statement is −

Exit { Do | For | Function | Property | Select | Sub | Try | While }

Flow Diagram

Example
 Live Demo
Module loops
Sub Main()
' local variable definition
Dim a As Integer = 10
' while loop execution '
While (a < 20)
[Link]("value of a: {0}", a)
a = a + 1
If (a > 15) Then
'terminate the loop using exit statement
Exit While
End If
End While
[Link]()
End Sub
End Module

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
[Link] - Continue Statement

The Continue statement causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating. It works somewhat like the Exit statement. Instead of forcing
termination, it forces the next iteration of the loop to take place, skipping any code in between.

For the For...Next loop, Continue statement causes the conditional test and increment portions of
the loop to execute. For the While and Do...While loops, continue statement causes the program
control to pass to the conditional tests.

Syntax
The syntax for a Continue statement is as follows −

Continue { Do | For | While }

Flow Diagram

Example
 Live Demo
Module loops
Sub Main()
' local variable definition
Dim a As Integer = 10
Do
If (a = 15) Then
' skip the iteration '
a = a + 1
Continue Do
End If
[Link]("value of a: {0}", a)
a = a + 1
Loop While (a < 20)
[Link]()
End Sub
End Module

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
[Link] - GoTo Statement

The GoTo statement transfers control unconditionally to a specified line in a procedure.

The syntax for the GoTo statement is −

GoTo label

Flow Diagram

Example
 Live Demo
Module loops
Sub Main()
' local variable definition
Dim a As Integer = 10
Line1:
Do
If (a = 15) Then
' skip the iteration '
a = a + 1
GoTo Line1
End If
[Link]("value of a: {0}", a)
a = a + 1
Loop While (a < 20)
[Link]()
End Sub
End Module

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

You might also like