O'Reilly logo

Visual Basic® .NET by Example by Bob Donald, Gabriel Oancea

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required