Visual Basic 2010 (Console) Guide
For Each Loops

For Each loops are used when you want to examine all of the values stored in a data structure. There are lots of built-in data structures in .NET - this example uses the generic List collection.

Dim myList As List(Of Integer) = New List(Of Integer)
Dim numEntered As Integer
Console.Write("Enter a number or 999 to finish> ")
numEntered = Console.ReadLine()
Do While numEntered <> 999
   Console.Write("Enter a number or 999 to finish> ")
   numEntered = Console.ReadLine()
Dim total As Integer = 0
For Each itm As Integer In myList
   Console.Write("{0} ", itm)
   total += itm
Console.WriteLine("Total of numbers entered is {0}", total)

Having A Go

  1. Use the List data structure to store numbers entered at the keyboard by the user. When they enter 999, stop accepting numbers and report the largest, smallest, mean and total of the numbers entered.