The For Loop

You have seen how to selectively execute statements based on some conditions. Another frequently used feature in programming is to repeat a group of statements for different values of one or more variables. For example, to sum all the values in an array of numbers into one variable, you could use code like this:

Dim vals As Integer() = {1, 2, 3}
Dim total As Integer
total = vals(0) + vals(1) + vals(2)

If you know the size of the array (the number of elements in the array) that is a very easy task. What happens if you do not know the size of the array when you write the program, but rather this information will be supplied to you when you are at runtime? In this case this method would not work anymore. You need a new type of statement ...

Get Visual Basic® .NET by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.