Visual Basic (VB) For Each Loop

In Visual Basic, For Each loop is useful to loop through items in array or collection object to execute the block of statements repeatedly.

 

Generally, in Visual Basic For Each loop will work with the collection objects such as an array, list, etc. to execute the block of statements for each element in the array or collection.

 

After completion of iterating through each element in the collection, the control will be transferred to the next statement following the For Each block.

 

In Visual Basic, we can use Exit, Continue statements within For Each loop to exit or continue to the next iteration of the loop based on our requirements.

Visual Basic For Each Loop Syntax

Following is the syntax of defining the For Each loop in Visual Basic programming language.

 

For Each var_name As [Data_TypeIn Collection_Object 

// Statements to Execute

Next

If you observe the above syntax, we defined For Each loop with collection object and required variable name to access an elements from the collection object.

 

Here, Data_Type is a built in data type or custom class type and var_name is a variable name to access an elements from collection object (Collection_Object) to use it in the body of For Each loop.

Visual Basic Foreach Loop Flow Chart

Following is the pictorial representation of For Each loop process flow diagram in Visual Basic programming language.

 

Visual Basic For Each Loop Flow Chart Diagram

 

Now, we will see how to use For Each loop in visual basic programming language with examples.

Visual Basic Foreach Loop with Array Example

Following is the example of using For Each loop in visual basic programming language to iterate or loop through array elements.

 

Module Module1

    Sub Main()

        Dim names As String() = New String(2) {"Suresh Dasari", "Rohini Alavala", "Trishika Dasari"}

        For Each name As String In names

            Console.WriteLine(name)

        Next

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created a string array object “names” and looping through each element of array object using For Each loop and assigning array elements to the string variable “name”.

 

To know more about arrays in a visual basic programming language, check Visual Basic arrays with examples.

 

When we execute the above Visual Basic program, we will get the result like as shown below.

 

Visual Basic For Each Loop with Arrays Example Result

 

If you observe the above result, we iterated through each element of the array and printed those values on the console window based on our requirements.

Visual Basic Foreach Loop with List Example

Same as For Each with arrays, we can use For Each loop with list object to process each element in the list object but inside of For Each loop it won’t allow us to modify (add or delete) the list object items.

 

To learn more about lists in visual basic programming, check Visual Basic lists with examples.

 

Following is the example of using For Each loop in a visual basic programming language to iterate or loop through list elements.

 

Module Module1

    Sub Main()

        Dim names As List(Of String) = New List(Of String)() From {

                "Suresh Dasari",

                "Rohini Alavala",

                "Trishika Dasari"

            }

        For Each name As String In names

            Console.WriteLine(name)

        Next

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created and added the string elements to the names list object. Here, we used For Each to loop through the items in the list to print it on the console window.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic For Each Loop with List Example Result

 

This is how we can use For Each loop in visual basic programming language to loop through each element in the array or collection objects based on our requirements. 

Topics Covered