Fovux
Humor Commander
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
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:
Output:
A
B
C
D
E
F
G
We can also loop through the array and display the output in descending order like Z-A. Above the For each statement, add the following code:
When completing, the code should look like as shown below.
You can now test you application by pressing “F5”.
Book traversal links for For each Loop
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
)
=
"A"
- letter(
1
)
=
"B"
- letter(
2
)
=
"C"
- letter(
3
)
=
"D"
- letter(
4
)
=
"E"
- letter(
5
)
=
"F"
- letter(
6
)
=
"G"
- ' Iterate through the list.
- For
Each
i As
String
In
letter
- ' Display the letter
- MsgBox
(
i)
- Next
'Taking the Next step
Output:
A
B
C
D
E
F
G
We can also loop through the array and display the output in descending order like Z-A. Above the For each statement, add the following code:
- Array.
Reverse
(
letter)
When completing, the code should look like as shown below.
- '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
)
=
"A"
- letter(
1
)
=
"B"
- letter(
2
)
=
"C"
- letter(
3
)
=
"D"
- letter(
4
)
=
"E"
- letter(
5
)
=
"F"
- letter(
6
)
=
"G"
- Array.
Reverse
(
letter)
- ' Iterate through the list.
- For
Each
i As
String
In
letter
- ' Display the letter
- MsgBox
(
i)
- Next
'Taking the Next step
You can now test you application by pressing “F5”.
Book traversal links for For each Loop
- ‹ For Loops
- Up
- If Statements ›