Javatpoint Logo
Javatpoint Logo

VB.NET For Each Loop

In the VB.NET, For Each loop is used to iterate block of statements in an array or collection objects. Using For Each loop, we can easily work with collection objects such as lists, arrays, etc., to execute each element of an array or in a collection. And when iteration through each element in the array or collection is complete, the control transferred to the next statement to end the loop.

Syntax:

For Each loop is used to read each element from the collection object or an array. The Data Type represents the type of the variable, and var_name is the name of the variable to access elements from the array or collection object so that it can be used in the body of For Each loop.

Flowchart of For Each loop

The following flowchart represents the For Each Next loop's functioning to iterate through array elements in the VB.NET programming language.

VB.NET For Each Loop

The first step is to initialize an array or collection object to execute each element of the array with the help of variables in For Each loop. A variable is used in For Each loop to checks whether the element is available or not. If the element is available in the collection object, the For Each block will be executed until the condition remains true. After the execution of each element of an array, the control transfer to the end statement.

Examples of For Each Loop

Write a simple program to understand the uses of For Each Next loop in VB.NET.

For_Each_loop.vb

Output:

VB.NET For Each Loop

In the above example, we create an integer array with the name An_array (), and For Each loop is used to iterate each element of the array with the help of defined variable 'i'.

Example 2: Write a simple program to print fruit names using For Each loop in VB.NET.

For_each.vb

Output:

VB.NET For Each Loop

In this example, str() is a String type array that defines different fruits names. And fruit is the name of a variable that is used to iterate each element of the str() array using For Each loop in the program. If all the element is read, control passes to the Main() function to terminate the program.






Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA