TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

VB.NET For Loop Examples

These VB.NET example programs use the For-loop. The loops increment, decrement and can be nested.

Loops. 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.

Other loops, such as While, continue until a condition is met. And For-Each requires a collection. In complex loops, where the end is not yet known, While is best.

For EachWhileDo WhileDo Until

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

Based on:

.NET 4.5

VB.NET program that uses For

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

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.

VB.NET program that uses For Step

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

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.

VB.NET program that uses nested loops

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 Chars. 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.

For each: The For-Each loop can also be used on Strings. When you do not need the index, For-Each is a better, cleaner choice.

Loop Over String

VB.NET program that uses For loop on String

Module Module1
    Sub Main()
	Dim value As String = "cat"

	' Start at zero and proceed until final index.
	For i As Integer = 0 To value.Length - 1
	    ' Get character from string.
	    Dim c As Char = value(i)

	    ' Test and display character.
	    If c = "c" Then
		Console.WriteLine("***C***")
	    Else
		Console.WriteLine(c)
	    End If
	Next
    End Sub
End Module

Output

***C***
a
t

Adjacent indexes. Often in For-loops, we need adjacent indexes. We can start at the second index (1) and then access the previous element each time, getting all pairs.

Tip: With this For-loop, we could check for duplicates in a sorted array, or repeated values in any array.

VB.NET program that uses For, adjacent indexes

Module Module1
    Sub Main()
	Dim value As String = "abcd"

	' Start at one so the previous index is always valid.
	For i As Integer = 1 To value.Length - 1
	    ' Get adjacent characters.
	    Dim c As Char = value(i - 1)
	    Dim c2 As Char = value(i)
	    Console.WriteLine(c & c2)
	Next
    End Sub
End Module

Output

ab
bc
cd

Long, non-integer indexes. The For-loop supports non-integer indexes like Long, Short and UShort. Char is not supported. Here we use For with a Long index.

VB.NET program that uses Long index

Module Module1
    Sub Main()
	' Loop over last three Longs.
	For d As Long = Long.MaxValue To Long.MaxValue - 2 Step -1
	    Console.WriteLine(d)
	Next
    End Sub
End Module

Output

9223372036854775807
9223372036854775806
9223372036854775805

Iterator, yield. A function can be marked as an Iterator. In an Iterator, we yield values. We call an Iterator in a loop and it sequentially returns new values.

Iterator

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.

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


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf