VB.NET For Loops

Array Class Collections File String Cast Console Data Directive Enum Exception If Interface Lambda LINQ Loop Nothing Number Process Property Regex Sort StringBuilder Structure Sub Time Windows

Arrow indicates looping

A loop executes statements repeatedly. It often has an upper and lower bound. The For-loop proceeds through a range of values. A step indicates the progression.


Loop

Conditions. Other loops,
such as While,
continue until a condition is met. In most places, a For-loop is clearest.
But in complex loops,
where the end is not yet known,
While is best.


Important

Constructs. Here are the looping constructs. The For-Each construct is probably the least error-prone but is not always available. A collection is required.

For EachWhileDo WhileDo Until
For loop

For-loop. This example uses the lower bound 0 and the upper bound 5 in the For-loop statement. Please remember that the two bounds are inclusive.

Inclusive:This means all the numbers in the range, including 0 and 5, will be reached.

Tip:In this example, we see the Exit For statement. This statement breaks out of the enclosing For-loop.

Tip 2:Exit works in all loops and Subs.
Please see the detailed information about Exit.

Exit
Program that uses For: VB.NET

Module Module1
    Sub Main()
	' This loop goes from 0 to 5.
	For value As Integer = 0 To 5
	    ' Exit condition if the value is three.
	    If (value = 3) Then
		Exit For
	    End If
	    Console.WriteLine(value)
	Next
    End Sub
End Module

Output

0
1
2

Steps

For step. Here we want to decrement the counter variable instead of increment it. In other words, we go down not up. We start at 10 and then continued own in steps of 2 to 0.

Also:The Step keyword is used near the end of the For-statement. A step is the delta each loop iteration will have.

So:If you want to decrement by 1 each time, you can use -1. If you want to increment by 1, use 1.

Program that uses For Step: VB.NET

Module Module1
    Sub Main()
	' This loop uses Step to go down 2 each iteration.
	For value As Integer = 10 To 0 Step -2
	    Console.WriteLine(value)
	Next
    End Sub
End Module

Output

10
8
6
4
2
0

Copyright

Nested loops. In many programs, nested loops are essential. In a For-loop, we uniquely name the iteration variable. And we reference all iteration variables (row, column) in an inner block.

Program that uses nested loops: VB.NET

Module Module1
    Sub Main()
	' Use a nested For-loop.
	For row As Integer = 0 To 2
	    For column As Integer = 0 To 2
		Console.WriteLine("{0},{1}", row, column)
	    Next
	Next
    End Sub
End Module

Output

0,0
0,1
0,2
1,0
1,1
1,2
2,0
2,1
2,2

String face

String loops. The For and For-Each loops can be used on String variables. This gives us a way to check, or process, each character in the Object data.

Loop Over String

Regex:String loops are an alternative to using Regex for modifying and creating Strings.

Regex.Replace
Keyword: keywords direct program behavior

Code. The For-loop is a core looping construct in the VB.NET language. It provides a way to explicitly specify the loop bounds. And thus it often leads to more robust code.


The VB.NET programming language

Fewer bugs. With the for-keyword, fewer careless bugs may occur. Boundaries are clear, well-defined. Errors are often caused by incorrect loop boundaries.

VB.NET