How to Use Loop in VB.NET With Examples [ Do, Do Until, For, and For Each ]
This tutorial will discuss how to use different loops in vb.net with examples specifically the Do While, Do Until, For, and For Each loop.
Do While Loop
Do While loop can have one or more conditions in its expression. In performing it, you have to use a special syntax form. This syntax form gives a way to continue performing while one or more conditions are true.
This is the syntax in doing Do While Loop.
Do While [counter] >= [start #] And [counter] <= [start #]
Dim i As Integer = 100 Dim z As Integer = 0 Do While i >= 0 And z <= 20 i = i - 10 z = z + 3 MsgBox(i & " " & 3) Loop
Do Until Loop
In Visual Basic, all loops have a similar purpose that makes a block of statements to repeat a number of times.
The Do Until and the Do While loop is a bit similar to each other. The only difference of the Do Until loop is, it uses a slightly different syntax.
Take note that if you are not careful with the syntax of Do Until it will result in an infinite Loop
This is the example in doing Do Until Loop.
Dim i As Integer = 0 Do Until i = 10 i += 1 MsgBox(i) Loop
The For Loop repeats a group of statements in a specified number of times while a loop index counts the number of loop iterations as the loop performs.
This is the syntax in constructing a loop.
For [counter variable] as [datatype] = [start #] to [end #]
nex [counter variable]
For i As Integer = 1 To 10 i = +i MsgBox(i) Next i
For Each Loop
One way to loop over the elements in an array using visual basic is to use For each loop. Because it automatically loops over all elements in the array or collections. And it works well when you can associate each iteration of a loop with a control variable and determine that variable’s initial and final values. And you don’t have to worry about getting the indices in order to get all the elements.
For Each Loop Syntax
For Each element [ As datatype ] In group
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]
The For Each loop is always the best choice when you are dealing with every single element or an item in a group. For example, your looping every single file in a folder or any character in a string.
This time, let’s Open visual basic and Create a New Project then save it as “Foreach”. Then, add a button on the windows form and change the Text property to “Go”.
Next, we’re going to add functionality to an application. To do this double click the button and add the following code:
'declare array as a string with six index Dim letter(6) As String 'here, we assign values to specific index of an array letter(0) = "VISUAL" letter(1) = "BASIC" letter(2) = "or" letter(3) = "VB.NET" letter(4) = "Progamming" letter(5) = "is" letter(6) = "FUN" ' Iterate through the list. For Each i As String In letter ' Display the letter MsgBox(i) Next 'Taking the Next step
When you run this code, you will see a series of outputs like as shown below.
VISUAL BASIC or VB.NET Progamming is FUN
In summary, we have discussed the different types of loop in vb.net and we have presented each loop with an example.
Anyway, the learning we have in this vb.net tutorial will vary in different situations, therefore, it is necessary for us to know the different used of loops in vb.net so that we will be able to apply it effectively in our individual systems.
The previous article is about VB.Net Data Types and variable Declaration with DIM.
For any questions or suggestions about loops in vb.net with examples, please feel free to leave a comment below.